/*
*Created on 12月8日
*Copyright Yong Cai Limited crop. All Rights Reserved
*
*72896**80@
*/
package ****;
import java.awt.MenuItem;
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.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class ShowSystemTray {
public void show(JFrame frame){
if (SystemTray.isSupported()) {// 如果系统支持托盘
// 获得系统托盘
SystemTray tray = SystemTray.getSystemTray();
// 图标
java.awt.Image image = Toolkit.getDefaultToolkit().getImage(
"src/image/SystemTrayIcon_16.png");
// 创建弹出式菜单
java.awt.PopupMenu popupMenu = new java.awt.PopupMenu();
// 创建菜单项
MenuItem openItem = new MenuItem("打开");
openItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// setVisible(true);
frame.setVisible(true);
}
});
MenuItem exitItem = new MenuItem("关闭");
exitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
popupMenu.add(openItem);
popupMenu.add(exitItem);
// 创建托盘图标
TrayIcon trayIcon = new TrayIcon(image, "***系统", popupMenu);
trayIcon.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
// setVisible(true);
frame.setVisible(true);
}
}
});
try {
tray.add(trayIcon);
} catch (Exception e) {
e.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "您的系统不支持系统托盘!", "提示",
JOptionPane.WARNING_MESSAGE);
return;
}
}
}