Java1.5.0下的系統托盤和批處理啟動命令

來源:互聯網
上載者:User

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
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.