代码分享:实现系统托盘效果

开发 后端
这段代码是能够在桌面任务栏上实现托盘效果,初学Java的朋友可以研究。

代码说明:

在桌面工具栏上显示托盘


代码片段:

  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. import java.awt.event.MouseEvent;  
  11. import java.io.UnsupportedEncodingException;  
  12. import java.net.URL;  
  13. import javax.swing.ImageIcon;  
  14. import javax.swing.JFrame;  
  15. import javax.swing.JLabel;  
  16.  
  17. public class SystemTrayDemo extends JFrame{  
  18.     private TrayIcon trayIcon = null;     
  19.     public SystemTrayDemo(){  
  20.         this.setTitle("系统托盘");  
  21.         jbInit();  
  22.         this.setSize(200250);  
  23.         this.setLocation(300300);  
  24.     }  
  25.     public void jbInit(){  
  26.         if(SystemTray.isSupported()){ //检查当前系统是否支持系统托盘  
  27.              SystemTray tray = SystemTray.getSystemTray();//获取表示桌面托盘区的 SystemTray 实例。  
  28.              Image image  = Toolkit.getDefaultToolkit().getImage("newrobot1_256.png");  
  29.              PopupMenu popupMenu = new PopupMenu();   
  30.                
  31.              MenuItem  exitItem  = new MenuItem("exit");  
  32.              System.out.println(exitItem.getFont());  
  33.              MenuItem  menuItema  = new MenuItem("menu a");   
  34.              MenuItem  menuItemb = new MenuItem("menu b");   
  35.              MenuItem  menuItemc  = new MenuItem("menu c");   
  36.              MenuItem  menuItemd = new MenuItem("menu d");   
  37.              exitItem.addActionListener(new  ActionListener(){  
  38.                  public void actionPerformed(ActionEvent e)     {     
  39.                      try{       
  40.                           System.exit(0);       
  41.                        }catch(Exception   ex)   {     
  42.                            ex.printStackTrace();     
  43.                        }     
  44.                  }  
  45.              });        
  46.              popupMenu.add(menuItema);   
  47.              popupMenu.add(menuItemb);   
  48.              popupMenu.add(menuItemc);   
  49.              popupMenu.add(menuItemd);   
  50.              popupMenu.add(exitItem);    
  51.              trayIcon = new TrayIcon(image, "系统托盘{thu}",  popupMenu);     
  52.              trayIcon.addMouseListener(new java.awt.event.MouseAdapter(){  
  53.                 @Override 
  54.                 public void mouseClicked(MouseEvent e) {  
  55.                    if(e.getClickCount()==2){     
  56.                          
  57.                        //注意下面的API调用,这个可以给用户提示信息  
  58.                        trayIcon.displayMessage("message""double click",   
  59.                                TrayIcon.MessageType.INFO);  
  60.                        showIT(true);     
  61.                     }  
  62.                 }       
  63.              });  
  64.                
  65.              //注意下面这个API调用,能够保证使用的图标被缩放到合适的比例  
  66.              trayIcon.setImageAutoSize(true);  
  67.               
  68.                
  69.              try{     
  70.                   tray.add(trayIcon);  // 将 TrayIcon 添加到 SystemTray。   
  71.              } catch   (AWTException   e)     {     
  72.                   System.err.println(e);     
  73.              }  
  74.         }else{  
  75.             System.out.println("你的系统不支持系统托盘");  
  76.         }  
  77.           
  78.         //下面这个部分是具体的业务逻辑  
  79.         ImageIcon icon = new ImageIcon();  
  80.         try{  
  81.             URL url = new URL("newrobot1_256.png");  
  82.             icon = new ImageIcon(url);  
  83.         }catch(Exception e){  
  84.               
  85.         }      
  86.         JLabel label = new JLabel();  
  87.         label.setIcon(icon);  
  88.         this.add(label);  
  89.     }  
  90.     public void showIT(boolean visable){  
  91.         if(this.isVisible() != visable)  
  92.             this.setVisible(visable);  
  93.     }  
  94.     public static void main(String[] args) {  
  95.         javax.swing.SwingUtilities.invokeLater(new Runnable(){  
  96.             public void run(){  
  97.                 new SystemTrayDemo().setVisible(true);  
  98.             }  
  99.         });  
  100.     }  

 


【效果图】

原文链接:http://www.oschina.net/code/snippet_188162_8173

【编辑推荐】

  1. 代码分享:模拟售票学习多线程
  2. 代码分享:心算24小游戏
  3. 代码分享:Swing外观抗锯齿字体设置
责任编辑:林师授 来源: 开源中国社区
相关推荐

2010-01-20 15:00:09

Visual C++开

2011-04-21 13:46:54

Ubuntu Unit

2009-02-22 09:11:16

Windows音量显示

2011-04-18 15:33:33

Ubuntu 11.0

2011-08-23 09:33:19

Ubuntu系统托盘

2011-01-19 14:40:45

Thunderbird

2020-03-06 08:00:02

Multipass系统虚拟机

2022-12-17 17:00:21

Windows 11微软

2023-11-30 08:26:01

微软Windows

2023-03-24 13:11:17

微软Windows

2022-03-29 10:10:12

微软Windows 11

2023-04-20 08:09:32

微软Windows

2023-10-22 06:58:25

2021-12-25 00:03:08

Windows 11Windows微软

2024-03-06 14:30:35

Windows 11

2023-06-23 14:01:45

2023-02-27 17:45:49

Windows 11微软

2023-03-25 20:08:32

微软Windows 11

2009-12-29 11:01:46

WPF淡入效果

2022-05-04 11:15:57

微软Windows 11
点赞
收藏

51CTO技术栈公众号