/** * @(#)MyTray.java * * * @author Xie Xiaojin * @version 1.00 2009/11/9 */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MyTray implements ActionListener { private MenuItem item1; private MenuItem item2; private MenuItem item3; private String tip = "谢小进于/n2009年11月9日/n凌晨2:52"; public MyTray(){ if(SystemTray.isSupported()){ SystemTray tray = SystemTray.getSystemTray(); PopupMenu popup = new PopupMenu(); item1 = new MenuItem("我的菜单"); item2 = new MenuItem("关于"); item3 = new MenuItem("退出"); item2.addActionListener(this); item3.addActionListener(this); popup.add(item1); popup.add(item2); popup.add(item3); TrayIcon icon = new TrayIcon(getIcon("rss.png").getImage(), tip, popup); icon.displayMessage("系统提示", tip, TrayIcon.MessageType.INFO); try{ tray.add(icon); } catch(AWTException ex){ System.err.println("无法向这个托盘添加新菜单项"); } } else{ System.out.println("无法使用系统托盘"); } } public static void main(String[] args){ new MyTray(); } public ImageIcon getIcon(String url){ return new ImageIcon(getClass().getResource(url)); } public void actionPerformed(ActionEvent e){ if(e.getSource() == item2){ JFrame f = new JFrame("系统托盘"); JLabel label = new JLabel(tip); f.getContentPane().add(label); f.setSize(300, 200); f.setLocationRelativeTo(null); f.setVisible(true); } if(e.getSource() == item3){ System.exit(0); } } }