JAVA桌面程序啓動時最小化到托盤(未測試:需jdk6.0)

 

import java.awt. * ;
import java.awt.event. * ;
import javax.swing.JFrame;
public class SystemTrayTest extends JFrame {
private TrayIcon trayIcon; // 托盤圖標
private SystemTray systemTray; // 系統托盤
public SystemTrayTest() {
// super("系統托盤圖標");
systemTray = SystemTray.getSystemTray(); // 獲得系統托盤的實例
// setSize(150, 150);
this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this .setVisible( true );
try {
// 定義托盤圖標的圖片
String filePath = " com/test/images/001.jpg " ;
String path
= ClassLoader.getSystemResource(filePath).getFile();
Image image
= Toolkit.getDefaultToolkit().getImage(path);
trayIcon
= new TrayIcon(image);
systemTray.add(trayIcon);
// 設置托盤的圖標,0.gif與該類文件同一目錄
this .dispose();
}
catch (AWTException e2) {
e2.printStackTrace();
}
this .addWindowListener(
new WindowAdapter() {
public void windowIconified(WindowEvent e) {
dispose();
// 窗口最小化時dispose該窗口
}
});
trayIcon.addMouseListener(
new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2 ) // 雙擊托盤窗口再現
{
setExtendedState(Frame.NORMAL);
}
setVisible(
true );
}
});
}
public static void main(String args[]) {
new SystemTrayTest();
}
}

轉自:http://topic.csdn.net/u/20091027/17/07d5e3db-012a-453c-9b4e-72aadbe4c072.html?57326
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章