import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;
/**
 * <p>Title: TrayIcon</p>
 * <p>Description: TrayIconUtil</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author <a href="mailto:[email protected]">SunKing</a>
 * @version 1.0
 */public class TrayIconUtil{
    private JPopupMenu rightPopup;
    private JWindow popupWindow;
    private JFrame mainFrame;
    private static TrayIconUtil staticInstance;    private TrayIconUtil(){}
    public static TrayIconUtil getDefaultTrayIconUtil(){
        if(staticInstance == null){
            staticInstance = new TrayIconUtil();
            staticInstance.initTrayIcon(staticInstance,"JavaTrayIconApp",1000);
        }
        return staticInstance;
    }
    private void setMainJFrame(final JFrame frame){
        this.mainFrame=frame;
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                closeTrayIcon();
            }
            public void windowIconified(WindowEvent e) {
                frame.setVisible(false);
            }
            public void windowDeactivated(WindowEvent e) {
                if(rightPopup!=null)
                    rightPopup.setVisible(false);
            }
        });
    }
    public void setTrayIconJPopupMenu(JPopupMenu popup){
        this.rightPopup=popup;
        rightPopup.show(getPopupWindow(),0,0);
        rightPopup.setVisible(false);
    }
    private void processTrayIconMouseEvent(int button,int x,int y){
        if(button == -1)return;//mouse over.
        if(button == 0 ||button ==5 ||button == 7){
            if(rightPopup != null && button == 5){
                rightPopup.show(getPopupWindow(),x,y);
                rightPopup.requestFocus();
                rightPopup.repaint();
            }
        }else{
            if(button == 3){
                if(rightPopup!=null)
                    rightPopup.setVisible(false);
                if(!mainFrame.isVisible()){
                    mainFrame.setVisible(true);
                    mainFrame.toFront();
                    mainFrame.requestFocus();
                    if(mainFrame.isVisible()){
                        if(mainFrame.getExtendedState()==JFrame.ICONIFIED){
                            mainFrame.setExtendedState(JFrame.NORMAL);
                        }
                    }
                }else{
                    mainFrame.setVisible(false);
                }
            }
        }
    }
    private Window getPopupWindow(){
        if(popupWindow == null){
            popupWindow = new JWindow();
            popupWindow.setBounds(0,0,1,1);
            popupWindow.show();
        }
        return popupWindow;
    }
    private boolean isFirst=true;
    public void initTrayIcon(String tip,File iconfile,JFrame frame,JPopupMenu popup){
        if(isFirst){
            if(iconfile!=null){
                ImageIcon icon=new ImageIcon(iconfile.getAbsolutePath());
                int w=icon.getIconWidth(),h=icon.getIconHeight();
                Image image=icon.getImage();
                try {
                    int[] pixels = new int[w * h];
                    PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
                    pg.grabPixels();
                    if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
                        throw new Exception("Error loading icon image");
                    }
                    setTrayIconData(w, h, pixels);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            if(tip!=null){
                setTrayIconTip(tip);
            }
            setTrayIconJPopupMenu(popup);
            setMainJFrame(frame);
            isFirst=false;
        }
    }
    private native void initTrayIcon(TrayIconUtil instance,String appName,int id);
    private native void setTrayIconTip(String tip);
    private native void setTrayIconData(int w, int h, int pixels[]);
    public native void showTrayIcon();
    public native void closeTrayIcon();
    static{
        System.loadLibrary("TrayIcon");
    }
}

解决方案 »

  1.   

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.event.*;
    import javax.swing.text.html.*;/**
     * <p>Title: TrayIcon</p>
     * <p>Description: TrayIconTest</p>
     * <p>Copyright: Copyright (c) 2003</p>
     * <p>Company: </p>
     * @author <a href="mailto:[email protected]">SunKing</a>
     * @version 1.0
     */public class TrayIconTest extends JFrame{
        TrayIconUtil util=TrayIconUtil.getDefaultTrayIconUtil();
        private JPopupMenu popup = new JPopupMenu("Test PopupMenu");
        private JMenuItem item_SH = new JMenuItem();
        private JMenuItem item_Exit = new JMenuItem(new ExitAction());
        private JMenuBar bar = new JMenuBar();
        private JMenu menuFile = new JMenu();
        private JMenuItem item_New = new JMenuItem();
        private JScrollPane sp1 = new JScrollPane();
        private BorderLayout borderLayout1 = new BorderLayout();
        private JEditorPane editor = new JEditorPane();
        private JLabel lbStatus = new JLabel();    public TrayIconTest() {
            try {
                jbInit();
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
        public static void main(String[] args) {
            TrayIconTest trayIconTest1 = new TrayIconTest();
            setFrameAtScreenCenter(trayIconTest1,400,300);
            trayIconTest1.show();
        }
        public static void setFrameAtScreenCenter(Window frame,int width,int height){
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            frame.setBounds((screenSize.width - width)/2,(
                    screenSize.height - height) / 2,width,height);
        }
        private void jbInit() throws Exception {
            this.setTitle("Java Tray Icon Test---SunKing");
            this.getContentPane().setLayout(borderLayout1);
            item_SH.setText("Hide");
            item_SH.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    item_SH_actionPerformed(e);
                }
            });
            popup.addPopupMenuListener(new PopupMenuListener() {
                public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                    item_SH.setText(isVisible()?"Hide":"Show");
                    item_SH.setMnemonic(isVisible()?'H':'S');
                }
                public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                }
                public void popupMenuCanceled(PopupMenuEvent e) {
                }
            });        menuFile.setText("File");
            item_New.setText("New");
            item_New.setEnabled(false);
            lbStatus.setBorder(BorderFactory.createLoweredBevelBorder());
            lbStatus.setText(" ");
            this.setJMenuBar(bar);
            popup.add(item_SH);
            popup.addSeparator();
            popup.add(item_Exit);
            bar.add(menuFile);
            menuFile.add(item_New);
            menuFile.addSeparator();
            menuFile.add(new ExitAction());
            this.getContentPane().add(sp1, BorderLayout.CENTER);
            this.getContentPane().add(lbStatus,  BorderLayout.SOUTH);
            sp1.getViewport().add(editor, null);        try {
                editor.setContentType("text/html; charset=gb2312");
    //            editor.read(new FileReader(new File("readme.html")),null);
                editor.setPage("file:"+new File("readme.html").getAbsolutePath());
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }
            util.initTrayIcon("Test trayicon program have tip text!",new File("tray.gif"),this,popup);
            util.showTrayIcon();
        }
        protected void processWindowEvent(WindowEvent e) {
            if (e.getID() == WindowEvent.WINDOW_CLOSING) {
                this.setExtendedState(ICONIFIED);
                return;
            }
            super.processWindowEvent(e);
        }    void item_SH_actionPerformed(ActionEvent e) {
            this.setVisible(!this.isVisible());
            if(isVisible()){
                if(this.getExtendedState()==ICONIFIED){
                    this.setExtendedState(NORMAL);
                }
            }
        }
        static {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            }
            catch(Exception e) {
            }
        }
    }
    class ExitAction extends AbstractAction{
        public ExitAction(){
            super("Exit");
            super.putValue(Action.MNEMONIC_KEY,new Integer('X'));
        }
        public void actionPerformed(ActionEvent e) {
            TrayIconUtil.getDefaultTrayIconUtil().closeTrayIcon();
            System.exit(0);
        }
    }
      

  2.   

    转贴:以上是一个示例程序,下面是程序简介。
    JAVA系统托盘图标控制程序说明:
      这是一个用Java JNI技术写的系统托盘图标控制程序.暂时还是一个测试版本,带有一个显示我的mail信息的对话框.
    配置和运行: 
      该程序运行于JDK1.3以上,首先你的机器必须支持在DOS窗口下直接输入java运行jar文件,如果不行,请在sun公司的网站java.sun.com进行JDK的最新版的下载和安装.
      通常你可以直接双击TrayIconTest.jar运行本程序,如果不能直接双击运行,请使用run.bat文件运行.
      

  3.   

    up &  & study
      

  4.   

    这里有你需要的资料:
    http://www.chinajavaworld.net/doc/sunking/TrayIcon.htm
    http://jeans.studentenweb.org/java/trayicon/trayicon.html
      

  5.   

    这个程序我试过了,在运行
    staticInstance.initTrayIcon(staticInstance,"JavaTrayIconApp",1000);的时候出错,我已经和作者联系过了,
    还在等待回音.....
      

  6.   

    有没有人用这个成功过啊(不是用demo,而是自己新建一个工程,把trayIcon加进去!)???我运行下面这句的时候出错!!
    staticInstance.initTrayIcon(staticInstance,"JavaTrayIconApp",1000);错误为UnsatisfiedLinkError