1、要用到 SysTray for Java (http://systray.sourceforge.net/)
2、请把引用的 dll 文件放在工程的根目录下
3、请把图片资源文件夹 icons 放在工程根目录下面
4、引入 systray4j.jar 包
5、编写如下java代码
package com.tray; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Vector; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.UIManager; import snoozesoft.systray4j.CheckableMenuItem; import snoozesoft.systray4j.SubMenu; import snoozesoft.systray4j.SysTrayMenu; import snoozesoft.systray4j.SysTrayMenuEvent; import snoozesoft.systray4j.SysTrayMenuIcon; import snoozesoft.systray4j.SysTrayMenuItem; import snoozesoft.systray4j.SysTrayMenuListener; /** * Example for the systray4j package. */ public class Example extends JFrame implements ActionListener, SysTrayMenuListener { // frame dimension static final int INIT_WIDTH = 220; static final int INIT_HEIGHT = 140; private static final String[] toolTips = { "SysTray for Java rules!", "brought to you by/nSnoozeSoft 2004" }; // create icons static final SysTrayMenuIcon[] icons = { // the extension can be omitted new SysTrayMenuIcon( "icons/duke" ), new SysTrayMenuIcon( "icons/duke_up" ) }; SysTrayMenu menu; int currentIndexIcon; int currentIndexTooltip; public Example() { super( "SysTray for Java Example" ); setIconImage( new ImageIcon( getClass().getResource( "rocket.gif" ) ).getImage() ); Dimension dimScreen = Toolkit.getDefaultToolkit().getScreenSize(); int xPos = ( dimScreen.width - INIT_WIDTH ) / 2; int yPos = ( dimScreen.height - INIT_HEIGHT ) / 2; setBounds( xPos, yPos, INIT_WIDTH, INIT_HEIGHT ); // don�t forget to assign listeners to the icons icons[ 0 ].addSysTrayMenuListener( this ); icons[ 1 ].addSysTrayMenuListener( this ); // change this according to the number of buttons used getContentPane().setLayout( new GridLayout( 4, 1 ) ); addButton( "change icon" ); addButton( "change tooltip" ); addButton( "show/hide icon" ); addButton( "enable/disable submenu" ); // create the menu createMenu(); //show(); } public static void main( String[] args ) { try{ UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() ); } catch( Exception e ) {} new Example(); } public void actionPerformed( ActionEvent e ) { if( e.getActionCommand().equals( "change tooltip" ) ) { if( currentIndexTooltip == 0 ) currentIndexTooltip = 1; else currentIndexTooltip = 0; menu.setToolTip( toolTips[ currentIndexTooltip ] ); } else if( e.getActionCommand().equals( "change icon" ) ) { if( currentIndexIcon == 0 ) currentIndexIcon = 1; else currentIndexIcon = 0; menu.setIcon( icons[ currentIndexIcon ] ); } else if( e.getActionCommand().equals( "show/hide icon" ) ) { if( menu.isIconVisible() ) menu.hideIcon(); else menu.showIcon(); } else if( e.getActionCommand().equals( "enable/disable submenu" ) ) { SysTrayMenuItem item = menu.getItem( "Communication" ); if( item.isEnabled() ) item.setEnabled( false ); else item.setEnabled( true ); } } public void menuItemSelected( SysTrayMenuEvent e ) { if( e.getActionCommand().equals( "exit" ) ) System.exit( 0 ); else if( e.getActionCommand().equals( "about" ) ) { JOptionPane.showMessageDialog( this, "SysTray for Java v" + SysTrayMenu.VERSION ); } else JOptionPane.showMessageDialog( this, e.getActionCommand() ); } public void iconLeftClicked( SysTrayMenuEvent e ) { if( isVisible() ) hide(); else show(); } public void iconLeftDoubleClicked( SysTrayMenuEvent e ) { JOptionPane.showMessageDialog( this, "You may prefer double-clicking the icon." ); } void createMenu() { // create some labeled menu items SysTrayMenuItem subItem1 = new SysTrayMenuItem( "Windows 98", "windows 98" ); subItem1.addSysTrayMenuListener( this ); // disable this item subItem1.setEnabled( false ); SysTrayMenuItem subItem2 = new SysTrayMenuItem( "Windows 2000", "windows 2000" ); subItem2.addSysTrayMenuListener( this ); SysTrayMenuItem subItem3 = new SysTrayMenuItem( "Windows XP", "windows xp" ); subItem3.addSysTrayMenuListener( this ); SysTrayMenuItem subItem4 = new SysTrayMenuItem( "GNOME", "gnome" ); subItem4.addSysTrayMenuListener( this ); subItem4.setEnabled( false ); SysTrayMenuItem subItem5 = new SysTrayMenuItem( "KDE 3", "kde 3" ); subItem5.addSysTrayMenuListener( this ); Vector items = new Vector(); items.add( subItem1 ); items.add( subItem2 ); items.add( subItem3 ); items.add( subItem4 ); items.add( subItem5 ); // create a submenu and insert the previously created items SubMenu subMenu = new SubMenu( "Supported", items ); // create some checkable menu items CheckableMenuItem chItem1 = new CheckableMenuItem( "IPC", "ipc" ); chItem1.addSysTrayMenuListener( this ); CheckableMenuItem chItem2 = new CheckableMenuItem( "Sockets", "sockets" ); chItem2.addSysTrayMenuListener( this ); CheckableMenuItem chItem3 = new CheckableMenuItem( "JNI", "jni" ); chItem3.addSysTrayMenuListener( this ); // check this item chItem2.setState( true ); chItem3.setState( true ); // create another submenu and insert the items through addItem() SubMenu chSubMenu = new SubMenu( "Communication" ); // disable this submenu chSubMenu.setEnabled( false ); chSubMenu.addItem( chItem1 ); chSubMenu.addItem( chItem2 ); chSubMenu.addItem( chItem3 ); // create an exit item SysTrayMenuItem itemExit = new SysTrayMenuItem( "Exit", "exit" ); itemExit.addSysTrayMenuListener( this ); // create an about item SysTrayMenuItem itemAbout = new SysTrayMenuItem( "About...", "about" ); itemAbout.addSysTrayMenuListener( this ); // create the main menu menu = new SysTrayMenu( icons[ 0 ], toolTips[ 0 ] ); // insert items menu.addItem( itemExit ); menu.addSeparator(); menu.addItem( itemAbout ); menu.addSeparator(); menu.addItem( subMenu ); menu.addItem( chSubMenu ); } void addButton( String label ) { JButton btn = new JButton( label ); btn.setActionCommand( label ); btn.addActionListener( this ); getContentPane().add( btn ); } }
6、把源文件到处为JAR包 tray.jar (可以指定Main Class) 7、使用JSmooth包装或使用批处理文件运行 c:/jdk1.5.0_09/bin/java -cp lib/systray4j.jar;lib/tray.jar; -Djava.library.path=. com.tray.Example c:/jdk1.5.0_09/bin/java 指定使用的虚拟机 lib/systray4j.jar;lib/tray.jar; 指定类路径 -Djava.library.path=. 指定可执行文件或dll库文件的搜索路径 com.tray.Example 指定运行主文件
8、直接运行jar包的批处理命令
MANIFEST.MF 文件内容
Manifest-Version: 1.0 Main-Class: com.tray.Example Class-Path: systray4j.jar
run.bat 文件内容
c:/jdk1.5.0_09/bin/java -Djava.library.path=. -jar lib/tray.jar