java创建系统托盘实现(swt和jdk6分别实现)

由于jdk6之前并不支持
所以使用swt
jdk6可以支持了 建议使用jdk6的版本 因为它才是真正的跨平台
swt
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;

public class TrayBySwt {

	public static void main(String[] args) {
		Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setText("系统托盘BySwt");
		shell.setLayout (new FillLayout());
		Button button = new Button(shell, SWT.PUSH);
		button.setText("隐藏窗口显示托盘图标");
		
		ImageData imageData = new ImageData("E:\\workspace\\myeclipse66\\ra\\Tray\\src\\16.gif");
		Image image = new Image(display, imageData);
		//取得系统托盘
		final Tray tray = display.getSystemTray();
		if (tray == null) {
			System.out.println("当前操作系统不支持托盘图标");
		} else {
			final TrayItem item = new TrayItem(tray, SWT.NONE);
			
			item.setToolTipText("我是托盘图标");
			item.addListener (SWT.Show, new Listener () {
			public void handleEvent (Event event) {
				System.out.println("show");
			}
			});
			item.addListener (SWT.Hide, new Listener () {
				public void handleEvent (Event event) {
					System.out.println("hide");
				}
			});
			item.addListener (SWT.Selection, new Listener () {
				public void handleEvent (Event event) {
					System.out.println("selection");
				}
			});
		
			final Menu menu = new Menu (shell, SWT.POP_UP);
			for (int i = 0; i < 8; i++) {
				MenuItem mi = new MenuItem (menu, SWT.PUSH);
				mi.setText ("Item" + i);
				mi.addListener (SWT.Selection, new Listener () {
					public void handleEvent (Event event) {
						System.out.println("selection " + event.widget);
					}
				});
				if (i == 0) menu.setDefaultItem(mi);
			}
			item.addListener (SWT.MenuDetect, new Listener () {
				public void handleEvent (Event event) {
					menu.setVisible (true);
				}
			});

			item.addListener(SWT.DefaultSelection, new Listener() {
				public void handleEvent(Event event) {
					shell.setVisible(true);
					item.setVisible(false);
				}
			});
			item.setImage(image);
			button.addListener(SWT.Selection, new Listener(){
				public void handleEvent(Event event) {
					shell.setVisible(false);
					item.setVisible(true);
				}
			});
			item.setVisible(false);
		}
		
		shell.setBounds(50, 50, 300, 200);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		image.dispose();
		display.dispose();
	}
}



jdk6

import java.awt.AWTException;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;

/**
 *
 * @author Mr.LiuTao
 */
public class TrayByJdk extends JFrame {

    private JPanel pane = null;
    private JButton button = null; // 启动托盘图标的按钮
    private JLabel label = null; // 用来显示系统是否支持托盘的信息
    private TrayIcon trayIcon = null; // 托盘图标
    private SystemTray tray = null; // 本操作系统托盘的实例
    //采用jdk1.6的托盘技术 实现跨平台的应用
    public TrayByJdk() {
        super("托盘技术演示");
        try {
            // 将LookAndFeel设置成Windows样式
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        pane = new JPanel();
        button = new JButton("缩小到托盘");
        button.setEnabled(false);
        label = new JLabel("本操作系统不支持托盘");
        pane.add(label);
        pane.add(button);
        //判断是否支持托盘
        if (SystemTray.isSupported()) {
            this.tray();
        }
        this.getContentPane().add(pane);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(300, 200);
        this.setVisible(true);
    }

    /**
     * 托盘相关代码
     */
    private void tray() {
        label.setText("本操作系统支持托盘");
        button.setEnabled(true);
        tray = SystemTray.getSystemTray(); // 获得本操作系统托盘的实例
        //ImageIcon icon = new ImageIcon("tray.gif"); // 将要显示到托盘中的图标
        ImageIcon icon = new ImageIcon("E:\\workspace\\myeclipse66\\ra\\Tray\\src\\16.gif"); // 将要显示到托盘中的图标
        PopupMenu pop = new PopupMenu(); // 构造一个右键弹出式菜单
        MenuItem show = new MenuItem("显示窗口");
        MenuItem exit = new MenuItem("退出演示");
        MenuItem author = new MenuItem("Author");
        /**
         * TrayIcon有三个构造
         * TrayIcon(Image image) 用“图标”来构造
         * TrayIcon(Image image, String tooltip) 用“图标”和“ToolTip”构造
         * TrayIcon(Image image, String tooltip, PopupMenu popup) 用“图标”,“ToolTip”,“弹出菜单”来构造一个托盘图标
         */
        trayIcon = new TrayIcon(icon.getImage(), "托盘技术演示", pop);
        // 点击本按钮后窗口被关闭,托盘图标被添加到系统的托盘中
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    tray.add(trayIcon); // 将托盘图标添加到系统的托盘实例中
                    setVisible(false); // 使窗口不可视
                } catch (AWTException ex) {
                    ex.printStackTrace();
                }
            }
        });
        /**
         * 添加鼠标监听器,当鼠标在托盘图标上双击时,默认显示窗口
         */
        trayIcon.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) { // 鼠标双击
                    tray.remove(trayIcon); // 从系统的托盘实例中移除托盘图标
                    setVisible(true); // 显示窗口
                }
            }
        });
        show.addActionListener(new ActionListener() { // 点击“显示窗口”菜单后将窗口显示出来

            public void actionPerformed(ActionEvent e) {
                tray.remove(trayIcon); // 从系统的托盘实例中移除托盘图标
                setVisible(true); // 显示窗口
            }
        });
        exit.addActionListener(new ActionListener() { // 点击“退出演示”菜单后推出程序

            public void actionPerformed(ActionEvent e) {
                System.exit(0); // 退出程序
            }
        });
        author.addActionListener(new ActionListener() { // 点击“退出演示”菜单后推出程序

            public void actionPerformed(ActionEvent e) {
                showMessage();
            }
        });
        pop.add(show);
        pop.add(exit);
        pop.add(author);
    }

    /**
     * 显示信息
     */
    private void showMessage() {
        JOptionPane.showMessageDialog(this, new JLabel("这是一个系统托盘"), "消息", JOptionPane.INFORMATION_MESSAGE);
    }

    public static void main(String[] args) {
        new TrayByJdk();
    }
}

你可能感兴趣的:(java,eclipse,windows,swing,UP)