import java.awt.Dimension;import java.awt.GridLayout;import java.awt.Image;import java.awt.Toolkit;import java.awt.Window;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.File;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import com.jeans.trayicon.SwingTrayPopup;import com.jeans.trayicon.TrayBalloonEvent;import com.jeans.trayicon.TrayBalloonListener;import com.jeans.trayicon.TrayIconCallback;import com.jeans.trayicon.TrayIconException;import com.jeans.trayicon.WindowsTrayIcon;public class TestTrayIcon extends JFrame...{ WindowsTrayIcon icon; public TestTrayIcon() throws TrayIconException, InterruptedException...{ super("test tray icon"); WindowsTrayIcon.setWindowsMessageCallback(new WindowsMessageCallback()); // 托盘图标显示Create the icon setIconImage(loadImage("Duke16.gif")); icon = new WindowsTrayIcon(loadImage("Duke16.gif"),16,16); //生成监听 RestoreListener listener = new RestoreListener(false); TestMouseListener mouse = new TestMouseListener(); BalloonMessageListener balloon = new BalloonMessageListener(); //添加托盘图标监听 icon.addActionListener(listener);//设置左单击为显示,右单击为菜单, icon.addMouseListener(mouse);//鼠标事件监听,与actionListener有重叠,双击时才有效. icon.addBalloonListener(balloon);//点击气球时的监听 icon.setVisible(true);//显示在托盘里 makeSwingPopup().setTrayIcon(icon); //swing菜单设置,如果为非swing菜单,则使用icon.setPopup(new TrayIconPopup()); //主面板设置 JPanel panel = new JPanel(); JPanel buttons = new JPanel(); JButton m_ExitButton,m_HideButton,m_BalloonButton; buttons.setLayout(new GridLayout(1, 0, 3, 3)); buttons.add(m_ExitButton = new JButton("Exit")); m_ExitButton.addActionListener(new ExitListener()); buttons.add(m_HideButton = new JButton("Hide")); m_HideButton.addActionListener(new HideListener()); buttons.add(m_BalloonButton = new JButton("showBalloon")); m_BalloonButton.addActionListener(new BalloonListener()); panel.add(buttons); panel.setBorder(new EmptyBorder(3, 3, 3, 3)); setContentPane(panel); addWindowListener(new WindowClosingListener()); pack(); } // Load a gif image (used for loading the 16x16 icon gifs) //载入图形文件 public static Image loadImage(String fileName) ...{ return Toolkit.getDefaultToolkit().getImage( "demo" + File.separator + "images" + File.separator + fileName); } public static void main(String[] args)...{ try...{ if(WindowsTrayIcon.isRunning("TestTrayIcon"))...{ System.out.println("TestTrayIcon is running"); }else...{ WindowsTrayIcon.initTrayIcon("TestTrayIcon"); TestTrayIcon test = new TestTrayIcon(); test.setSize(300,200); centerDialog(test); test.setVisible(true); } }catch(Exception e)...{ e.printStackTrace(); } } // Center a dialog on screen public static void centerDialog(Window frame) ...{ Dimension dialogSize = frame.getSize(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation(screenSize.width / 2 - dialogSize.width / 2, screenSize.height / 2 - dialogSize.height / 2); } private class WindowsMessageCallback implements TrayIconCallback ...{ public int callback(int param) ...{ // Param contains the integer value send with sendWindowsMessage(appName,param) System.out.println("[Other instance started (parameter: " + param + ")]."); setVisible(true); toFront(); requestFocus(); // Return integer value to other process return 4321; } } private class RestoreListener implements ActionListener ...{ protected boolean from_menu; public RestoreListener(boolean fromMenu) ...{ from_menu = fromMenu; } public void actionPerformed(ActionEvent evt) ...{ System.out.println("evt parm=="+evt.paramString()); System.out.println("from_menu="+from_menu); if (from_menu) ...{ setVisible(true); toFront(); requestFocus(); } else if (evt.getActionCommand().equals("Left")) ...{ if (!isVisible()) ...{ // Make main window visible if it was hidden setVisible(true); // Request input focus toFront(); requestFocus(); } else ...{ doHide(false); } } } } // Callback listener handles balloon messages private class BalloonMessageListener implements TrayBalloonListener ...{ public void balloonChanged(TrayBalloonEvent evt) ...{ if ((evt.getMask() & TrayBalloonEvent.SHOW) != 0) ...{ System.out.println("Balloon shown"); } if ((evt.getMask() & TrayBalloonEvent.HIDE) != 0) ...{ System.out.println("Balloon hidden"); } if ((evt.getMask() & TrayBalloonEvent.TIMEOUT) != 0) ...{ System.out.println("Balloon time out"); } if ((evt.getMask() & TrayBalloonEvent.CLICK) != 0) ...{ System.out.println("Balloon clicked by user"); } } } public class TestMouseListener extends MouseAdapter ...{ public void mousePressed(MouseEvent evt) ...{ System.out.println("mouse event=="+evt.paramString());// if ((evt.getModifiers() & MouseEvent.BUTTON1_MASK) != 0// && evt.getClickCount() == 2) {// System.out.println("[Tray icon double clicked].");// } if (evt.getClickCount() == 2) ...{ System.out.println("[Tray icon double clicked]."); } } } // Called when app must be hidden public void doHide(boolean exitOnFail) ...{ System.out.println("[Hide selected]."); // Check if there's a Tray Icon visible // Hide works only when there's an icon in the system tray boolean visible = true; if(icon.isVisible())...{ visible = false; } setVisible(visible); if (visible == true) ...{ System.out.println("[Hide works only when there's an icon in the system tray]."); if (exitOnFail) doExit(); } } // Called when app exits public void doExit() ...{ System.out.println("[Exit selected / Close requested]."); // Free all Tray Icon resources - always call this on exit WindowsTrayIcon.cleanUp(); // Exit application System.exit(0); } private class ExitListener implements ActionListener ...{ public void actionPerformed(ActionEvent evt) ...{ doExit(); } } // Called when close button in window title bar is selected public class WindowClosingListener extends WindowAdapter ...{ public void windowClosing(WindowEvent e) ...{ doHide(true); } } // Callback listener for hide button private class HideListener implements ActionListener ...{ public void actionPerformed(ActionEvent evt) ...{ doHide(false); } } //托盘气球弹出事件 private class BalloonListener implements ActionListener ...{ public void actionPerformed(ActionEvent evt) ...{ int flags = 0; int timeout = 10000; boolean unicode_cnv = false; //气球类型 flags = WindowsTrayIcon.BALLOON_INFO; //flags = WindowsTrayIcon.BALLOON_WARNING; //flags = WindowsTrayIcon.BALLOON_ERROR; //flags |= WindowsTrayIcon.BALLOON_NOSOUND; unicode_cnv = true; timeout = 1000;//弹出时间,好像无效,超时也不会消失; String title = "show this title..";//气球标题; String message = "show this message..";//气球消息内容; WindowsTrayIcon ticon = icon;//要显示在哪个托盘图标上?; try ...{ WindowsTrayIcon.enableUnicodeConversion( WindowsTrayIcon.UNICODE_CONV_BALLOON, unicode_cnv); ticon.showBalloon(message, title, timeout, flags); } catch (TrayIconException e) ...{ System.out.println("Error showing balloon message"); } } } public SwingTrayPopup makeSwingPopup() ...{ SwingTrayPopup popup = new SwingTrayPopup(); // popup.setFont(m_Font); JMenuItem item = new JMenuItem("show", new ImageIcon(loadImage("Duke16.gif"))); // Each menu item can have it's own ActionListener item.addActionListener(new RestoreListener(true)); // item.setFont(m_Font); popup.add(item); item = new JMenuItem("about", new ImageIcon(loadImage("About.gif"))); popup.add(item); return popup; } }