SystemTray(转帖)

        SystemTray意为桌面的系统托盘,在Windows下表现为任务状态栏的一个图标,在很多的应用中都有体现,如及时通讯软件(MSN、QQ),监控类软件(杀毒软件)等。 
在 Java 6的java.awt包下提供了SystemTray和TrayIcon类,这两个类可以实现系统托盘功能,下面让我们看看如何去实现系统托盘。实现的流 程:捕获关闭窗口事件、隐藏窗口、显示托盘图标,鼠标右击托盘图标、显示弹出菜单(自动)、选择菜单的显示窗口项,再显示窗口、隐藏托盘图标。

先 了解一下SystemTray和TrayIcon这两个类。每一个Java应用都会包含一个SystemTray实例,这个不需要你去实例化,你可以通过 SystemTray.getSystemTray()获取,但是不是所有的系统和平台支持系统托盘的,因此我们必须先判断系统是否支持系统托盘,检测是 否支持托盘的方法是 isSupported() 。

接下来我们就要在SystemTray中添加TrayIcon,这样在托盘区就会显示一个新的托盘图标。TrayIcon有四部分组成:名称、图标、弹出菜单和事件,所以我们将这四方面做好就可以完成其既定的功能。



下面是例子程序:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Frame;

/**
 *
 * @author max
 */
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class TrayIconAdvanced extends JFrame {
    private TrayIcon ti;
    public static void main(String[] args) {
        TrayIconAdvanced frame = new TrayIconAdvanced();
        frame.setTitle("TrayIconAdvanced");
        frame.setVisible(true);
        frame.setSize(200, 65);
        frame.setLocation(300, 200);
    }

    public TrayIconAdvanced() {
        setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
            if (SystemTray.isSupported()) {
                setVisible(false);//隐藏窗口
                minimizeToTray();//如果系统支持托盘图标,将窗口放入系统托盘区
            } else {
                System.exit(0);//否则结束程序
                }
            }
        });

        JPanel root = new JPanel();
        JButton exitButton = new JButton("Exit");
        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);//结束程序
            }
        });
        root.add(exitButton);
        getContentPane().add(root);
        pack();
        initTi();
    }
    private void initTi() {
        Image image = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("TrayIcon.gif"));
        PopupMenu popupTi = new PopupMenu();//弹出菜单
        MenuItem showItem = new MenuItem("Show");//菜单项
        ActionListener showListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(true);//重新显示窗口
                SystemTray.getSystemTray().remove(ti);//从系统托盘中移出
            }
        };
        showItem.addActionListener(showListener);//菜单行听众
        popupTi.add(showItem);
        ti = new TrayIcon(image, "TrayIcon", popupTi);//图标,标题,右键弹出菜单
        ti.addActionListener(showListener);//增加一个双击事件听众

    }

    public void minimizeToTray() {
        SystemTray tray = SystemTray.getSystemTray();
        try {
            tray.add(ti);//在系统托盘区中增加图标
        } catch (AWTException e) {
            System.err.println(e);
        }
    }
}

 (2)

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Frame;

/**
 *
 * @author max
 */
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//import java.net.URL;
//import java.net.URLClassLoader;

import javax.swing.JFrame;
import javax.swing.JLabel;
public class TestSystemTray
{
    public static void main(String args[])
    {
        TrayIcon trayIcon = null;
        if (SystemTray.isSupported()) //判断系统是否支持系统托盘
        {
            SystemTray tray = SystemTray.getSystemTray(); //获取系统托盘
            Image image = Toolkit.getDefaultToolkit().getImage("TrayIcon.gif");//载入图片
            ActionListener listener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    //创建一个窗体
                    JFrame frame = new JFrame();
                    frame.setBounds(400,400,200,200);
                    JLabel label =new JLabel("welcome JDK1.6");
                    frame.add(label);
                    frame.setVisible(true);

                }
            };

        //创建托盘图标的右键弹出菜单,open与exit.
        PopupMenu popup = new PopupMenu();
        MenuItem defaultItem = new MenuItem("open");
        defaultItem.addActionListener(listener);
        MenuItem exitItem = new MenuItem("exit");
        exitItem.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e) {
                System.exit(0);

            }
        });
        popup.add(defaultItem);
        popup.add(exitItem);
        trayIcon = new TrayIcon(image, "My System Tray ", popup);//创建托盘图标

        trayIcon.addActionListener(listener);//双击托盘图标时打开窗体

        try {
            tray.add(trayIcon);//将图标加入到系统托盘区
        } catch (AWTException e1) {
            e1.printStackTrace();
        }
        }
    }
}
 

你可能感兴趣的:(java,windows,.net,qq,swing)