java最小化到托盘显示图标实现

//设置软件在托盘上显示的图标
        Toolkit tk = Toolkit.getDefaultToolkit();
        Image img = tk.getImage("smile.gif");//*.gif与该类文件同一目录
        SystemTray systemTray = SystemTray.getSystemTray(); //获得系统托盘的实例
        TrayIcon trayIcon = null;
       
        try {
            trayIcon = new TrayIcon(img, "前20名网站排名点击器");
            systemTray.add(trayIcon); //设置托盘的图标,*.gif与该类文件同一目录
            this.setIconImage(img);
             trayIcon.setImageAutoSize(true);
        } catch (AWTException e2) {
            e2.printStackTrace();
        }
      
        //窗口最小化时软件dispose
        this.addWindowListener(new WindowAdapter() {
            //图标化窗口时调用事件
            public void windowIconified(WindowEvent e) {
                dispose(); //窗口最小化时dispose该窗口
            }
        });
   
        //双击托盘图标,软件正常显示
        trayIcon.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) //双击托盘窗口再现
                    //置此 frame 的状态。该状态表示为逐位掩码。
                    setExtendedState(Frame.NORMAL); //正常化状态
                setVisible(true);
            }
        });

posted @ 2012-10-10 09:50  jack_ou  阅读(3487)  评论(0编辑  收藏  举报