代码说明:
在桌面工具栏上显示托盘
代码片段:
- import java.awt.AWTException;
- import java.awt.Image;
- import java.awt.MenuItem;
- import java.awt.PopupMenu;
- import java.awt.SystemTray;
- import java.awt.Toolkit;
- import java.awt.TrayIcon;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.io.UnsupportedEncodingException;
- import java.net.URL;
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- public class SystemTrayDemo extends JFrame{
- private TrayIcon trayIcon = null;
- public SystemTrayDemo(){
- this.setTitle("系统托盘");
- jbInit();
- this.setSize(200, 250);
- this.setLocation(300, 300);
- }
- public void jbInit(){
- if(SystemTray.isSupported()){ //检查当前系统是否支持系统托盘
- SystemTray tray = SystemTray.getSystemTray();//获取表示桌面托盘区的 SystemTray 实例。
- Image image = Toolkit.getDefaultToolkit().getImage("newrobot1_256.png");
- PopupMenu popupMenu = new PopupMenu();
- MenuItem exitItem = new MenuItem("exit");
- System.out.println(exitItem.getFont());
- MenuItem menuItema = new MenuItem("menu a");
- MenuItem menuItemb = new MenuItem("menu b");
- MenuItem menuItemc = new MenuItem("menu c");
- MenuItem menuItemd = new MenuItem("menu d");
- exitItem.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- try{
- System.exit(0);
- }catch(Exception ex) {
- ex.printStackTrace();
- }
- }
- });
- popupMenu.add(menuItema);
- popupMenu.add(menuItemb);
- popupMenu.add(menuItemc);
- popupMenu.add(menuItemd);
- popupMenu.add(exitItem);
- trayIcon = new TrayIcon(image, "系统托盘{thu}", popupMenu);
- trayIcon.addMouseListener(new java.awt.event.MouseAdapter(){
- @Override
- public void mouseClicked(MouseEvent e) {
- if(e.getClickCount()==2){
- //注意下面的API调用,这个可以给用户提示信息
- trayIcon.displayMessage("message", "double click",
- TrayIcon.MessageType.INFO);
- showIT(true);
- }
- }
- });
- //注意下面这个API调用,能够保证使用的图标被缩放到合适的比例
- trayIcon.setImageAutoSize(true);
- try{
- tray.add(trayIcon); // 将 TrayIcon 添加到 SystemTray。
- } catch (AWTException e) {
- System.err.println(e);
- }
- }else{
- System.out.println("你的系统不支持系统托盘");
- }
- //下面这个部分是具体的业务逻辑
- ImageIcon icon = new ImageIcon();
- try{
- URL url = new URL("newrobot1_256.png");
- icon = new ImageIcon(url);
- }catch(Exception e){
- }
- JLabel label = new JLabel();
- label.setIcon(icon);
- this.add(label);
- }
- public void showIT(boolean visable){
- if(this.isVisible() != visable)
- this.setVisible(visable);
- }
- public static void main(String[] args) {
- javax.swing.SwingUtilities.invokeLater(new Runnable(){
- public void run(){
- new SystemTrayDemo().setVisible(true);
- }
- });
- }
- }
【效果图】
原文链接:http://www.oschina.net/code/snippet_188162_8173
【编辑推荐】