首先需要在工程中導包,需要用到JRE中的rt.jar包,我的JDK安裝在C盤,目錄如下:C:\Program Files\Java\jre7\lib\rt.jar。
我們用到的類是rt包中的com.sun.awt.AWTUtilities。(遺憾的是導入該包後,程序就不具有跨平台性了)
程序代碼如下:
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRootPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import com.sun.awt.AWTUtilities;
/**
* @author zhenyu tan
* 2014年4月2日
* 使用到了JDK1.6中新特性的透明窗體,所以必須要使用JDK1.6及其以上版本
* 功能如下:
* 1.窗體出現時逐漸清晰
* 2.停留一會時間之後會自動逐漸模糊直至消失
* 3.點擊關閉按鈕後逐漸模糊直至消失
*/
public class TipWindow {
JFrame frame;
JLabel label;
JEditorPane editorPane;
private int width;//窗體寬度
private int height;//窗體高度
private int stayTime;//休眠時間
private String title;//消息標題
private String message;//窗體內容
private int style;//窗體樣式
static {
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
/**
* @param width 提示框寬度
* @param height 提示框高度
* @param stayTime 提示框停留時間
* @param style 提示框的樣式
* @param title 提示框標題
* @param message 提示框內容(支持HTML標簽)
*/
public TipWindow(int width, int height, int stayTime, int style, String title, String message) {
this.width = width;
this.height = height;
this.stayTime = stayTime;
this.style = style;
this.title = title;
this.message = message;
}
public TipWindow(int stayTime, int style, String title, String message) {
this.width = 300;
this.height = 100;
this.stayTime = stayTime;
this.style = style;
this.title = title;
this.message = message;
}
public void initialize() {
frame = new JFrame();
editorPane = new JEditorPane();
editorPane.setEditable(false);
editorPane.setContentType("text/html");
editorPane.setText(message);
frame.add(editorPane);
frame.setTitle(title);
//設置窗體的位置及大小
Point location = MouseInfo.getPointerInfo().getLocation();
frame.setBounds((int)location.getX(), (int)location.getY(), width, height);
frame.setUndecorated(true);//去掉窗口的裝飾
frame.getRootPane().setWindowDecorationStyle(style);//設置窗體樣式
AWTUtilities.setWindowOpacity(frame, 0);//初始化透明度
frame.setVisible(true);
frame.setAlwaysOnTop(true);//窗體置頂
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
hide();
}
});
}
//窗體逐漸變清晰
public void show() {
for (int i = 1; i < 20; i++) {
try {
Thread.sleep(50);
} catch (Exception e) {
AWTUtilities.setWindowOpacity(frame, i * 0.05F);
}
}
}
//窗體逐漸變淡甚至消失
public void hide() {
float opacity = 100;
while(true) {
if (opacity < 2) {
break;
}
opacity -= 2;
AWTUtilities.setWindowOpacity(frame, opacity / 100);
try {
Thread.sleep(150);
} catch (Exception e) {
e.printStackTrace();
}
}
frame.dispose();
}
public void run() {
initialize();
show();
try {
Thread.sleep(stayTime * 1000);
} catch (Exception e) {
e.printStackTrace();
}
hide();
}
public static void main(String[] args) {
String title = "友情提示!";
String message = "主人!<br />該休息了!";
TipWindow tipWindow = new TipWindow(2, JRootPane.QUESTION_DIALOG, title, message);
tipWindow.run();
}
}