1 package swingtest;
2
3
4 import javax.swing.*;
5 import java.awt.*;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.awt.event.MouseAdapter;
9 import java.awt.event.MouseEvent;
10 import java.io.UnsupportedEncodingException;
11 import java.net.URL;
12 import java.util.Random;
13
14
15 public class MainFrame extends JFrame implements ActionListener {
16
17 private static final long serialVersionUID = -7078030311369039390L;
18 private JMenu menu;
19 private JMenuBar jmenuBar;
20 private String[] jmItemName = {"置于托盘", "系统退出"};
21
22 private MainFrame() throws UnsupportedEncodingException {
23 super("电话薄");
24 init();
25 this.setSize(500, 400);
26 this.setJMenuBar(jmenuBar);
27 this.setLocationRelativeTo(null);
28
29 JButton jbutton = new JButton("test");
30 add(jbutton);
31
32
33 systemTray(jbutton); //系统托盘
34 }
35
36 /**
37 * 初始化界面
38 */
39 private void init() {
40 menu = new JMenu("系统窗体");
41 for (String s : jmItemName) {
42 JMenuItem menuItem = new JMenuItem(s);
43 menuItem.addActionListener(this);
44 menu.add(menuItem);
45 }
46 this.jmenuBar = new JMenuBar();
47 this.jmenuBar.add(menu);
48 }
49
50 @Override
51 public void actionPerformed(ActionEvent e) {
52 String actions = e.getActionCommand();
53 if ("置于托盘".equals(actions)) {
54 this.setVisible(false);
55 }
56 if ("系统退出".equals(actions)) {
57 System.exit(0);
58 }
59
60 }
61
62 /**
63 * 系统托盘图标处理.
64 *
65 * @param jbutton
66 */
67 private void systemTray(JButton jbutton) throws UnsupportedEncodingException {
68 if (SystemTray.isSupported()) { //判断系统是否支持托盘功能.
69 // URL resource = this.getClass().getResource("car.jpg"); //获得图片路径
70
71 URL resource = getClass().getClassLoader().getResource("car.jpg");
72 // File targetFile = new File("files/8k.wav");
73
74 // ClassPathResource classPathResource = new ClassPathResource("static/something.txt");
75 //
76 // InputStream inputStream = classPathResource.getInputStream();
77 ImageIcon icon = new ImageIcon(resource); //创建图片对象
78 JPopupMenu popupMenu = new JPopupMenu(); //创建弹出菜单对象
79 JMenuItem itemExit = new JMenuItem("退出系统"); //创建弹出菜单中的退出项
80 JMenuItem itemShow = new JMenuItem("显示窗体"); //创建弹出菜单中的显示主窗体项.
81
82
83 popupMenu.add(itemExit);
84 popupMenu.add(itemShow);
85 TrayIcon trayIcon = new TrayIcon(icon.getImage(), "电话薄系统");
86 SystemTray sysTray = SystemTray.getSystemTray();
87
88 try {
89 sysTray.add(trayIcon);
90 } catch (AWTException ignored) {
91 }
92 trayIcon.addMouseListener(new MouseAdapter() {
93 @Override
94 public void mouseReleased(MouseEvent e) {
95 if (e.isPopupTrigger()) {
96 popupMenu.setLocation(e.getX(), e.getY());
97 popupMenu.setInvoker(popupMenu);
98 popupMenu.setVisible(true);
99 }
100 }
101 });
102 //给窗体最小化添加事件监听.
103 itemShow.addActionListener(e -> {
104 setVisible(true);
105
106 });
107 //给退出像添加事件监听
108 itemExit.addActionListener(e -> {
109 // System.exit(0);
110 sysTray.remove(trayIcon);
111 dispose();
112 });
113 int count = 0;
114 jbutton.addActionListener(e -> {
115 TrayIcon trayIcon4 = new TrayIcon(icon.getImage(), "电话薄系统");
116 try {
117 sysTray.add(trayIcon4);
118 } catch (AWTException ex) {
119 ex.printStackTrace();
120 }
121 });
122
123 }
124 }
125
126 /**
127 * 主方法
128 *
129 * @param args sdf
130 */
131 public static void main(String[] args) throws UnsupportedEncodingException {
132 new MainFrame().setVisible(true);
133 }
134 }