Java Swing系统托盘程序
项目结构
展开/折叠
E:\workspacne_JDK8Tomcat8.5\SystemTrayDemo
└─src\
├─com\
│ └─lan\
│ ├─networking.png
│ ├─SystemTrayDemo.java
│ └─SystemTrayFrame.java
└─sun.png
托盘图标
networking.png
到https://www.iconfont.cn/search/index?q=network下载一个图标,和.java文件放在同一个包下即可.
sun.png
到https://www.iconfont.cn/search/index?searchType=icon&q=sun下载一个图标,放在src目录下。
SystemTrayDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| package com.lan;
import java.awt.Image; import java.awt.MenuItem; import java.awt.PopupMenu; import java.awt.SystemTray; import java.awt.TrayIcon; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.net.URL;
import javax.swing.ImageIcon; import javax.swing.JOptionPane;
public class SystemTrayDemo { public static void main(String[] args) { if (SystemTray.isSupported()) { URL url = SystemTrayDemo.class.getResource("networking.png"); ImageIcon icon = new ImageIcon(url); Image image = icon.getImage(); TrayIcon trayIcon = new TrayIcon(image); trayIcon.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { JOptionPane.showMessageDialog(null, "SystemTrayDemo"); } } }); trayIcon.setToolTip("本地连接\r\n速度:100.0 Mbps\r\n状态:已连接上"); PopupMenu popupMenu = new PopupMenu(); popupMenu.add(new MenuItem("禁用(D)")); popupMenu.add(new MenuItem("状态(S)")); popupMenu.add(new MenuItem("修复(P)")); popupMenu.addSeparator(); popupMenu.add(new MenuItem("更改 Windows 防火墙设置(C)")); popupMenu.addSeparator(); popupMenu.add(new MenuItem("打开网络连接(O)"));
trayIcon.setPopupMenu(popupMenu); SystemTray systemTray = SystemTray.getSystemTray(); try { systemTray.add(trayIcon); } catch (Exception e) { e.printStackTrace(); } } else { JOptionPane.showMessageDialog(null, "not support"); } } }
|
运行效果
乱码解决
1. 设置项目的编码为GBK
在项目上右键,选择Properties
,然后在对话框的右侧中的Text file encoding
选择框中选择Other
,然后选择GBK
。
2. 添加运行参数-Dfile.encoding=GBK
在编辑区域,右键,Run As
,Run Configurations...
,然后在弹出的对话框的左侧列表中展开Java Application
,选择SystemTrayDemo
,然后点击右边的Arguments
选项卡,在VM arguments
:文本框中输入-Dfile.encoding=GBK
SystemTrayFrame.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| package com.lan;
import java.awt.AWTException; import java.awt.BorderLayout; import java.awt.EventQueue; 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.net.URL; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder;
public class SystemTrayFrame extends JFrame { private static final long serialVersionUID = 6708808934167400534L; private JPanel contentPane;
public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { SystemTrayFrame frame = new SystemTrayFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); }
public SystemTrayFrame() { setTitle("测试系统面板"); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); initSystemTray(); }
private void initSystemTray() { if (SystemTray.isSupported()) { URL resource = this.getClass().getResource("/sun.png"); ImageIcon icon = new ImageIcon(resource); PopupMenu pop = new PopupMenu(); MenuItem displayJFrameItem = new MenuItem("显示"); displayJFrameItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setVisible(true); } }); pop.add(displayJFrameItem);
MenuItem exitItem = new MenuItem("退出"); exitItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); pop.add(exitItem);
TrayIcon tray = new TrayIcon(icon.getImage(), "HelloWorld", pop); SystemTray systemTray = SystemTray.getSystemTray(); try { systemTray.add(tray); } catch (AWTException e1) { e1.printStackTrace(); } } } }
|
运行效果
关闭窗体后,程序并不会退出,在系统托盘上的太阳图标上右键,
- 选择显示,可以重新显示窗体;
- 选择退出,则退出程序;
参考资料
https://blog.csdn.net/jianggujin/article/details/50577491
https://blog.csdn.net/qq_36761831/article/details/81516535
https://www.docs4dev.com/docs/zh/java/java8/tutorials/uiswing-misc-systemtray.html
https://blog.csdn.net/xietansheng/article/details/78389278