該代碼實現了在系統右下角的任務欄中顯示程序的圖標,并且最小化程序后單擊圖標可以顯示出來這個程序窗口
- import java.awt.Color;
- import java.awt.Image;
- import java.awt.MenuItem;
- import java.awt.PopupMenu;
- import java.awt.Toolkit;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- public class test extends JFrame
- {
- long setTime = 30*1000;
- JLabel jl = new JLabel("剩余時間:");
- JLabel jl1 = new JLabel();
- PopupMenu popupMenu1 = new PopupMenu();
- MenuItem menuItem1 = new MenuItem();
- public examTime(){
- this.setLocation(200, 200);
- this.setSize(300, 200);
- isTray();
- this.setVisible(true);
- addWindowListener(new WindowAdapter()
- {
- public void windowIconified(WindowEvent evt)
- {
- unVisible();
- }
- });
- popupMenu1.setLabel("PopupMenu");
- menuItem1.setLabel("打開");
- menuItem1.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent evt)
- {
- showw();
- }
- });
- popupMenu1.add(menuItem1);
- }
- public void unVisible(){
- this.setVisible(false);
- }
- public void showw(){
- this.setVisible(true);
- }
- public void isTray()
- {
- try
- {
- if (SystemTray.isSupported())
- {// 判斷當前平臺是否支持系統托盤
- SystemTray st = SystemTray.getSystemTray();
- Image image = Toolkit.getDefaultToolkit().getImage(
- "E:/eclipse/workspace/test/test.gif");//定義托盤圖標的圖片
- TrayIcon ti = new TrayIcon( image);
- ti.setToolTip ( "test ");
- ti.setPopupMenu ( this.popupMenu1);
- st.add(ti);
- }
- }
- catch (Exception e)
- {
- }
- }
- public static void main(String[] args)
- {
- new test();
- }
- }
dm520