import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.TrayIcon;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.*;
import java.awt.event.*;public class MyFrame extends JFrame {
    private TrayIcon trayIcon;    public MyFrame() throws AWTException {
        setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                if (SystemTray.isSupported()) {
                    System.out.println("1:" );
                    setVisible(false);
                    try {
                        minimizeToTray();
                    } catch (AWTException ex) {
                        ex.printStackTrace();
                    }
                } else {
                    System.exit(0);
                }
            }
        });
        JPanel root = new JPanel();
        JButton exitButton = new JButton("ok");
        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        root.add(exitButton);
        getContentPane().add(root);
        pack();
        initTrayIcon();
        minimizeToTray();    }    public void minimizeToTray() throws AWTException {
        SystemTray tray = SystemTray.getSystemTray();
        tray.add(trayIcon);
    }    private void initTrayIcon() {
        Image image = null;
                try {
                     image = Toolkit.getDefaultToolkit().getImage(this.getClass().
                            getResource("/aaa.gif"));
                } catch (Exception ex) {
                    System.out.println("aaa:"+ex.getMessage()) ;
                }
                System.out.println("MyFrame:" + image);
        PopupMenu popup = new PopupMenu();
        MenuItem exitItem = new MenuItem("Show");
        ActionListener showListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(true);
                SystemTray.getSystemTray().remove(trayIcon);
            }
        };
        exitItem.addActionListener(showListener);
        popup.add(exitItem);
        trayIcon = new TrayIcon(image, "MyTray", popup);
        trayIcon.addActionListener(showListener);
    }    public static void main(String[] args) throws AWTException {
        MyFrame frame = new MyFrame();
        frame.setTitle("MyFrame");
        frame.setVisible(true);
    }
}在系统托盘那只显示空白,右键点击能出菜单,请高手指点!谢谢

解决方案 »

  1.   

    把aaa.gif放在classes下。Image trayImage = Toolkit.getDefaultToolkit().getImage("firefox.png");
                this.trayIcon = new TrayIcon(trayImage, "Java抓屏", this.getSytemTrayMenu());
      

  2.   

    还是没有,是不是跟操作系统有关系?我的是winxp sp2
      

  3.   

    我改了一下就可以了
    image = new ImageIcon("images/leaf.gif").getImage();
    应该是路径问题。
      

  4.   

    java6都已经出来了,我真土了,我还在用很老的版本呢,我都不知道java也可以支持托盘功能了。
      

  5.   

    以前的托盘偶都是用dll实现的- -落伍了T_T