上代碼:
package v2;
import javax.swing.*;
import com.sun.awt.AWTUtilities;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
/**
*
*
*
*/
public class MessageBox {
/**
* swing彈窗提示,
*
* @param fontSize
* @param msg
* @return
*/
public static JFrame alert(int fontSize, String msg) {
JFrame frame = new JFrame("");// 新建窗體
Window win = new Window(frame);// 設置圓角
// JFrame.setDefaultLookAndFeelDecorated(true);//設置為swing默認窗體
AWTUtilities.setWindowShape(win,
new RoundRectangle2D.Double(0.0D, 0.0D, win.getWidth(), win.getHeight(), 26.0D, 26.0D));
Color color = new Color(0, 0, 0, 50);// 黑色背景,透明度為50的color;透明的取值范圍0~255;
frame.setAlwaysOnTop(true);// 設置窗口置頂
frame.setLayout(new GridBagLayout());// 設置網格包布局
frame.setUndecorated(true);// 設置無邊框
frame.setBackground(color);// 設置背景色
JLabel label = new JLabel(msg);
label.setForeground(Color.white);
label.setFont(new Font("黑體", 0, fontSize));
frame.setSize(msg.length() * fontSize, fontSize + 50);
// 長度為字符大小*字符數量,寬度為字體大小+50像素
frame.add(label);// 添加到窗體
frame.setLocationRelativeTo(null);
frame.setVisible(true);
return frame;
}
public static void main(String[] args) {
alert(36, "!!!!!!!!");
}
}

