private static final long serialVersionUID = 1L;
private static TrayIcon trayIcon = null;
static JFrame mf = new JFrame();
static SystemTray tray = SystemTray.getSystemTray();
public static void myFrame() { // 窗体
mf.setLocation(300, 100);
mf.setSize(500, 300);
mf.setTitle("hello world");
mf.setVisible(true);// 使窗口可见
mf.addWindowListener(new WindowAdapter() { // 窗口关闭事件
public void windowClosing(WindowEvent e) {
System.exit(0);
};
public void windowIconified(WindowEvent e) { // 窗口最小化事件
mf.setVisible(false);
MServer.miniTray();
}
});
}
private static void miniTray() { // 窗口最小化到任务栏托盘
ImageIcon trayImg = new ImageIcon("");// 托盘图标
trayIcon = new TrayIcon(trayImg.getImage(), "test", new PopupMenu());
trayIcon.setImageAutoSize(true);
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {// 单击 1 双击 2
tray.remove(trayIcon);
mf.setVisible(true);
mf.setExtendedState(JFrame.NORMAL);
mf.toFront();
}
}
});
try {
tray.add(trayIcon);
} catch (AWTException e1) {
e1.printStackTrace();
}
}
java Swing最小化到任务栏图标,单击显示
最新推荐文章于 2021-03-30 14:58:42 发布