swing托盘使用

  1. import java.awt.AWTException;  
  2. import java.awt.Image;  
  3. import java.awt.MenuItem;  
  4. import java.awt.PopupMenu;  
  5. import java.awt.SystemTray;  
  6. import java.awt.Toolkit;  
  7. import java.awt.TrayIcon;  
  8. import java.awt.event.ActionEvent;  
  9. import java.awt.event.ActionListener;  
  10.   
  11. import javax.swing.JFrame;  
  12.   
  13.   
  14. public class MySystemTray extends JFrame{  
  15.       
  16.     public MySystemTray() {  
  17.         init();  
  18.     }  
  19.       
  20.     public void init() {  
  21.           
  22.         this.setSize(300200);  
  23.         this.setLocationRelativeTo(null);  
  24.         this.setTray();  
  25.         this.setVisible(true);  
  26.     }  
  27.       
  28.     //添加托盘显示:1.先判断当前平台是否支持托盘显示  
  29.     public void setTray() {  
  30.           
  31.         if(SystemTray.isSupported()){//判断当前平台是否支持托盘功能  
  32.             //创建托盘实例  
  33.             SystemTray tray = SystemTray.getSystemTray();  
  34.             //创建托盘图标:1.显示图标Image 2.停留提示text 3.弹出菜单popupMenu 4.创建托盘图标实例  
  35.             //1.创建Image图像  
  36.             Image image = Toolkit.getDefaultToolkit().getImage("trayIconImage/clientIcon.jpg");  
  37.             //2.停留提示text  
  38.             String text = "MySystemTray";  
  39.             //3.弹出菜单popupMenu  
  40.             PopupMenu popMenu = new PopupMenu();  
  41.             MenuItem itmOpen = new MenuItem("打开");  
  42.             itmOpen.addActionListener(new ActionListener(){  
  43.                 public void actionPerformed(ActionEvent e) {  
  44.                     Show();  
  45.                 }                 
  46.             });  
  47.             MenuItem itmHide = new MenuItem("隐藏");  
  48.             itmHide.addActionListener(new ActionListener(){  
  49.                 public void actionPerformed(ActionEvent e) {  
  50.                     UnVisible();  
  51.                 }  
  52.             });  
  53.             MenuItem itmExit = new MenuItem("退出");  
  54.             itmExit.addActionListener(new ActionListener(){  
  55.                 public void actionPerformed(ActionEvent e) {  
  56.                     Exit();  
  57.                 }  
  58.             });  
  59.             popMenu.add(itmOpen);  
  60.             popMenu.add(itmHide);  
  61.             popMenu.add(itmExit);  
  62.               
  63.             //创建托盘图标  
  64.             TrayIcon trayIcon = new TrayIcon(image,text,popMenu);  
  65.             //将托盘图标加到托盘上  
  66.             try {  
  67.                 tray.add(trayIcon);  
  68.             } catch (AWTException e1) {  
  69.                 e1.printStackTrace();  
  70.             }  
  71.         }  
  72.     }  
  73.       
  74.     //内部类中不能直接调用外部类(this不能指向)  
  75.     public void UnVisible() {  
  76.         this.setVisible(false);  
  77.     }  
  78.     public void Show() {  
  79.         this.setVisible(true);  
  80.     }  
  81.     public void Exit() {  
  82.         System.exit(0);  
  83.     }  
  84.       
  85.       
  86.     public static void main(String[] args) {  
  87.         new MySystemTray();  
  88.     }  
  89. }  

你可能感兴趣的:(swing,托盘)