天天看點

java swing螢幕右下角出現漸隐漸顯的提示框

原文: java swing螢幕右下角出現漸隐漸顯的提示框 源代碼下載下傳位址:http://www.zuidaima.com/share/1550463300586496.htm

1.窗體出現時逐漸清晰 2.停留一會兒時間之後會自動逐漸模糊直至消失 3.點選關閉按鈕後逐漸模糊直至消失 4.提示内容支援html标簽

java swing螢幕右下角出現漸隐漸顯的提示框
package com.zuidaima.util.TranslucentFrame;

import java.awt.Toolkit;
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 com.sun.awt.AWTUtilities;

/**
 * 螢幕右下角出現漸隐漸顯的提示框
 * 使用到了JDK1.6中新特性的透明窗體,是以必須要使用JDK1.6或以上版本的JDK
 * 功能如下:
 * 1.窗體出現時逐漸清晰
 * 2.停留一會兒時間之後會自動逐漸模糊直至消失
 * 3.點選關閉按鈕後逐漸模糊直至消失
 * 4.提示内容支援html标簽
 * @author www.zuidaima.com
 */
public class TranslucentFrame implements Runnable {
	JFrame frame;
	JLabel label1;
	JEditorPane editorPane1;
	private int width;//窗體寬度
	private int height;//窗體高度
	private int stayTime;//休眠時間
	private String title, message;//消息标題,内容
	private int style;//窗體樣式
	static {
		try {
			UIManager
					.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	/**
	 * 漸隐漸顯的提示框
	 * 
	 * @param width     提示框寬度
	 * @param height    提示框高度
	 * @param stayTime  提示框停留時間
	 * @param style     提示框的樣式
	 * 以下為樣式可選值:
	 * 0    NONE                  無裝飾(即去掉标題欄)
	 * 1    FRAME                 普通視窗風格
	 * 2    PLAIN_DIALOG          簡單對話框風格
	 * 3    INFORMATION_DIALOG    資訊對話框風格
	 * 4    ERROR_DIALOG          錯誤對話框風格
	 * 5    COLOR_CHOOSER_DIALOG  拾色器對話框風格
	 * 6    FILE_CHOOSER_DIALOG   檔案選擇對話框風格
	 * 7    QUESTION_DIALOG       問題對話框風格
	 * 8    WARNING_DIALOG        警告對話框風格
	 * @param title     提示框标題
	 * @param message   提示框内容(支援html标簽)
	 */
	public TranslucentFrame(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;
	}

	/**
	 * 漸隐漸顯的提示框
	 * 
	 * @param style     提示框樣式同上
	 * @param title     提示框标題
	 * @param message   提示框内容
	 */
	public TranslucentFrame(int style, String title, String message) {
		this.width = 250;
		this.height = 180;
		this.stayTime = 5;
		this.style = style;
		this.title = title;
		this.message = message;
	}

	public static void main(String[] args) {
		String title = "友情提示!";
		String message = "<strong>JDK1.6新特性測試</strong><br>《透明窗體》<br>www.oschina.net<br>CSDN";
		//      Runnable translucent=new TranslucentFrame(250,180,10,4,title,message);
		Runnable translucent = new TranslucentFrame(2, title, message);
		Thread thread = new Thread(translucent);
		thread.start();
	}

	public void print() {
		frame = new JFrame();
		editorPane1 = new JEditorPane();
		editorPane1.setEditable(false);//不可編輯
		editorPane1.setContentType("text/html");//将編輯框設定為支援html的編輯格式
		editorPane1.setText(message);
		frame.add(editorPane1);
		frame.setTitle(title);
		//設定窗體的位置及大小
		int x = Toolkit.getDefaultToolkit().getScreenSize().width
				- Toolkit.getDefaultToolkit().getScreenInsets(
						frame.getGraphicsConfiguration()).right - width - 5;
		int y = Toolkit.getDefaultToolkit().getScreenSize().height
				- Toolkit.getDefaultToolkit().getScreenInsets(
						frame.getGraphicsConfiguration()).bottom - height - 5;
		frame.setBounds(x, y, width, height);
		frame.setUndecorated(true); // 去掉視窗的裝飾
		frame.getRootPane().setWindowDecorationStyle(style); //窗體樣式 
		frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); //窗體樣式 
		AWTUtilities.setWindowOpacity(frame, 0.01f);//初始化透明度
		frame.setVisible(true);
		frame.setAlwaysOnTop(true);//窗體置頂
		//添加關閉視窗的監聽
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				hide();
			}
		});
	}

	/**
	 * 窗體逐漸變清晰
	 *
	 */
	public void show() {
		for (int i = 0; i < 50; i++) {
			try {
				Thread.sleep(50);
			} catch (Exception e) {
			}
			AWTUtilities.setWindowOpacity(frame, i * 0.02f);
		}
	}

	/**
	 * 窗體逐漸變淡直至消失
	 *
	 */
	public void hide() {
		float opacity = 100;
		while (true) {
			if (opacity < 2) {
				System.out.println();
				break;
			}
			opacity = opacity - 2;
			AWTUtilities.setWindowOpacity(frame, opacity / 100);
			try {
				Thread.sleep(20);
			} catch (Exception e1) {
			}
		}
		frame.hide();
		System.exit(0);
	}

	public void run() {
		print();
		show();
		try {
			Thread.sleep(stayTime * 1000);
		} catch (Exception e) {
		}
		hide();
	}
}
           

 标簽: swing 提示框 漸隐漸顯 螢幕 java話題: Swing和AWT開發