import java.awt.AWTException;
4.import java.awt.Image;
5.import java.awt.MenuItem;
6.import java.awt.PopupMenu;
7.import java.awt.SystemTray;
8.import java.awt.TrayIcon;
9.import java.awt.event.ActionEvent;
10.import java.awt.event.ActionListener;
11.import java.awt.event.MouseEvent;
12.import java.awt.event.MouseListener;
13.import java.awt.event.WindowEvent;
14.import java.awt.event.WindowListener;
15.
16.import javax.swing.GroupLayout;
17.import javax.swing.JFrame;
18.import javax.swing.GroupLayout.ParallelGroup;
19.import javax.swing.GroupLayout.SequentialGroup;
20.
21.public class ToTrayIcon extends JFrame implements ActionListener,
22. WindowListener {
23.
24. private static final long serialVersionUID = 1L;
25. // Variables declaration - do not modify
26. private javax.swing.JLabel L_img;
27. private javax.swing.JLabel L_img2;
28. private PopupMenu pop;
29. private MenuItem open, close;
30. private TrayIcon trayicon;
31.
32. // End of variables declaration
33.
34. /** Creates new form MainFrame */
35. public ToTrayIcon() {
36. setTitle("Java实现系统托盘示例");
37. setLocation(300, 300);
38. initComponents();
39. addWindowListener(this);
40. }
41.
42. /**
43. * This method is called from within the constructor to initialize the form.
44. * WARNING: Do NOT modify this code. The content of this method is always
45. * regenerated by the Form Editor.
46. */
47. // <editor-fold defaultstate="collapsed" desc="Generated Code">
48. private void initComponents() {
49.
50. // L_img = new javax.swing.JLabel(new
51. // ImageIcon((MainFrame.class).getResource("com/topking/tray/images/netbean1.png")));
52. // L_img2 = new javax.swing.JLabel(new
53. // ImageIcon((MainFrame.class).getResource("com/topking/tray/images/netbean2.png")));
54. L_img = new javax.swing.JLabel();
55. L_img2 = new javax.swing.JLabel();
56.
57. pop = new PopupMenu();
58. open = new MenuItem("打开");
59. open.addActionListener(this);
60.
61. close = new MenuItem("关闭");
62. close.addActionListener(this);
63.
64. pop.add(open);
65. pop.add(close);
66.
67. if (SystemTray.isSupported()) {
68. SystemTray tray = SystemTray.getSystemTray();
69. Image icon = getToolkit().getImage(
70. getClass().getResource("/user.png"));
71. trayicon = new TrayIcon(icon, "系统托盘示例(java)", pop);
72. trayicon.addMouseListener(new MouseListener() {
73.
74. public void mouseClicked(MouseEvent e) {
75. if (e.getClickCount() == 2) {
76. if (getExtendedState() == JFrame.ICONIFIED) {
77. openFrame();// 还原窗口
78. } else {
79. // 设置窗口状态(最小化至托盘)
80. setExtendedState(JFrame.ICONIFIED);
81. }
82. }
83. }
84.
85. public void mouseEntered(MouseEvent e) {
86.
87. }
88.
89. public void mouseExited(MouseEvent e) {
90.
91. }
92.
93. public void mousePressed(MouseEvent e) {
94.
95. }
96.
97. public void mouseReleased(MouseEvent e) {
98.
99. }
100.
101. });
102.
103. try {
104. tray.add(trayicon);
105. } catch (AWTException e) {
106. e.printStackTrace();
107. }
108. }
109. /** *设置界面布局,可以不用理睬它 */
110. GroupLayout layout = new GroupLayout(getContentPane());
111. getContentPane().setLayout(layout);
112. ParallelGroup parg = layout
113. .createParallelGroup(GroupLayout.Alignment.LEADING);
114. SequentialGroup seqg = layout.createSequentialGroup();
115. ParallelGroup parg2 = layout
116. .createParallelGroup(GroupLayout.Alignment.TRAILING);
117.
118. ParallelGroup parg3 = parg2.addComponent(L_img2,
119. GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 380,
120. Short.MAX_VALUE);
121. ParallelGroup parg4 = parg3.addComponent(L_img,
122. GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 380,
123. Short.MAX_VALUE);
124. SequentialGroup seqg2 = seqg.addContainerGap();
125. SequentialGroup seqg3 = seqg2.addGroup(parg4);
126. SequentialGroup seqg4 = seqg3.addContainerGap();
127. ParallelGroup parg5 = parg.addGroup(GroupLayout.Alignment.TRAILING,
128. seqg4);
129. layout.setHorizontalGroup(parg5);
130. parg = layout.createParallelGroup(GroupLayout.Alignment.LEADING);
131. layout.setVerticalGroup(parg.addGroup(layout.createSequentialGroup()
132. .addContainerGap().addComponent(L_img).addGap(29, 29, 29)
133. .addComponent(L_img2, GroupLayout.PREFERRED_SIZE, 222,
134. GroupLayout.PREFERRED_SIZE).addContainerGap(39,
135. Short.MAX_VALUE)));
136.
137. pack();
138. }
139.
140. /**
141. * @param args
142. * the command line arguments
143. */
144. public static void main(String args[]) {
145. java.awt.EventQueue.invokeLater(new Runnable() {
146. public void run() {
147. new ToTrayIcon().setVisible(true);
148. }
149. });
150. }
151.
152. public void actionPerformed(ActionEvent e) {
153. if (e.getSource() == open) {
154. openFrame();
155. }
156. if (e.getSource() == close) {
157. System.exit(-1);
158. }
159. }
160.
161. public void openFrame() {
162. setVisible(true);// 设置为可见
163. setAlwaysOnTop(true);// 设置置顶
164. // 设置窗口状态(在最小化状态弹出显示)
165. setExtendedState(JFrame.NORMAL);
166. }
167.
168. public void windowActivated(WindowEvent arg0) {
169.
170. }
171.
172. public void windowClosed(WindowEvent arg0) {
173. dispose();
174. }
175.
176. public void windowClosing(WindowEvent arg0) {
177.
178. }
179.
180. public void windowDeactivated(WindowEvent arg0) {
181.
182. }
183.
184. public void windowDeiconified(WindowEvent arg0) {
185.
186. }
187.
188. // 窗口最小化
189. public void windowIconified(WindowEvent arg0) {
190. setVisible(false);// 设置为不可见
191. }
192.
193. public void windowOpened(WindowEvent arg0) {
194.
195. }
196.
197.}
这段程序怎么加一个 快捷键,在界面最小化后 按快捷键就让界面还原? 谢谢
4.import java.awt.Image;
5.import java.awt.MenuItem;
6.import java.awt.PopupMenu;
7.import java.awt.SystemTray;
8.import java.awt.TrayIcon;
9.import java.awt.event.ActionEvent;
10.import java.awt.event.ActionListener;
11.import java.awt.event.MouseEvent;
12.import java.awt.event.MouseListener;
13.import java.awt.event.WindowEvent;
14.import java.awt.event.WindowListener;
15.
16.import javax.swing.GroupLayout;
17.import javax.swing.JFrame;
18.import javax.swing.GroupLayout.ParallelGroup;
19.import javax.swing.GroupLayout.SequentialGroup;
20.
21.public class ToTrayIcon extends JFrame implements ActionListener,
22. WindowListener {
23.
24. private static final long serialVersionUID = 1L;
25. // Variables declaration - do not modify
26. private javax.swing.JLabel L_img;
27. private javax.swing.JLabel L_img2;
28. private PopupMenu pop;
29. private MenuItem open, close;
30. private TrayIcon trayicon;
31.
32. // End of variables declaration
33.
34. /** Creates new form MainFrame */
35. public ToTrayIcon() {
36. setTitle("Java实现系统托盘示例");
37. setLocation(300, 300);
38. initComponents();
39. addWindowListener(this);
40. }
41.
42. /**
43. * This method is called from within the constructor to initialize the form.
44. * WARNING: Do NOT modify this code. The content of this method is always
45. * regenerated by the Form Editor.
46. */
47. // <editor-fold defaultstate="collapsed" desc="Generated Code">
48. private void initComponents() {
49.
50. // L_img = new javax.swing.JLabel(new
51. // ImageIcon((MainFrame.class).getResource("com/topking/tray/images/netbean1.png")));
52. // L_img2 = new javax.swing.JLabel(new
53. // ImageIcon((MainFrame.class).getResource("com/topking/tray/images/netbean2.png")));
54. L_img = new javax.swing.JLabel();
55. L_img2 = new javax.swing.JLabel();
56.
57. pop = new PopupMenu();
58. open = new MenuItem("打开");
59. open.addActionListener(this);
60.
61. close = new MenuItem("关闭");
62. close.addActionListener(this);
63.
64. pop.add(open);
65. pop.add(close);
66.
67. if (SystemTray.isSupported()) {
68. SystemTray tray = SystemTray.getSystemTray();
69. Image icon = getToolkit().getImage(
70. getClass().getResource("/user.png"));
71. trayicon = new TrayIcon(icon, "系统托盘示例(java)", pop);
72. trayicon.addMouseListener(new MouseListener() {
73.
74. public void mouseClicked(MouseEvent e) {
75. if (e.getClickCount() == 2) {
76. if (getExtendedState() == JFrame.ICONIFIED) {
77. openFrame();// 还原窗口
78. } else {
79. // 设置窗口状态(最小化至托盘)
80. setExtendedState(JFrame.ICONIFIED);
81. }
82. }
83. }
84.
85. public void mouseEntered(MouseEvent e) {
86.
87. }
88.
89. public void mouseExited(MouseEvent e) {
90.
91. }
92.
93. public void mousePressed(MouseEvent e) {
94.
95. }
96.
97. public void mouseReleased(MouseEvent e) {
98.
99. }
100.
101. });
102.
103. try {
104. tray.add(trayicon);
105. } catch (AWTException e) {
106. e.printStackTrace();
107. }
108. }
109. /** *设置界面布局,可以不用理睬它 */
110. GroupLayout layout = new GroupLayout(getContentPane());
111. getContentPane().setLayout(layout);
112. ParallelGroup parg = layout
113. .createParallelGroup(GroupLayout.Alignment.LEADING);
114. SequentialGroup seqg = layout.createSequentialGroup();
115. ParallelGroup parg2 = layout
116. .createParallelGroup(GroupLayout.Alignment.TRAILING);
117.
118. ParallelGroup parg3 = parg2.addComponent(L_img2,
119. GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 380,
120. Short.MAX_VALUE);
121. ParallelGroup parg4 = parg3.addComponent(L_img,
122. GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 380,
123. Short.MAX_VALUE);
124. SequentialGroup seqg2 = seqg.addContainerGap();
125. SequentialGroup seqg3 = seqg2.addGroup(parg4);
126. SequentialGroup seqg4 = seqg3.addContainerGap();
127. ParallelGroup parg5 = parg.addGroup(GroupLayout.Alignment.TRAILING,
128. seqg4);
129. layout.setHorizontalGroup(parg5);
130. parg = layout.createParallelGroup(GroupLayout.Alignment.LEADING);
131. layout.setVerticalGroup(parg.addGroup(layout.createSequentialGroup()
132. .addContainerGap().addComponent(L_img).addGap(29, 29, 29)
133. .addComponent(L_img2, GroupLayout.PREFERRED_SIZE, 222,
134. GroupLayout.PREFERRED_SIZE).addContainerGap(39,
135. Short.MAX_VALUE)));
136.
137. pack();
138. }
139.
140. /**
141. * @param args
142. * the command line arguments
143. */
144. public static void main(String args[]) {
145. java.awt.EventQueue.invokeLater(new Runnable() {
146. public void run() {
147. new ToTrayIcon().setVisible(true);
148. }
149. });
150. }
151.
152. public void actionPerformed(ActionEvent e) {
153. if (e.getSource() == open) {
154. openFrame();
155. }
156. if (e.getSource() == close) {
157. System.exit(-1);
158. }
159. }
160.
161. public void openFrame() {
162. setVisible(true);// 设置为可见
163. setAlwaysOnTop(true);// 设置置顶
164. // 设置窗口状态(在最小化状态弹出显示)
165. setExtendedState(JFrame.NORMAL);
166. }
167.
168. public void windowActivated(WindowEvent arg0) {
169.
170. }
171.
172. public void windowClosed(WindowEvent arg0) {
173. dispose();
174. }
175.
176. public void windowClosing(WindowEvent arg0) {
177.
178. }
179.
180. public void windowDeactivated(WindowEvent arg0) {
181.
182. }
183.
184. public void windowDeiconified(WindowEvent arg0) {
185.
186. }
187.
188. // 窗口最小化
189. public void windowIconified(WindowEvent arg0) {
190. setVisible(false);// 设置为不可见
191. }
192.
193. public void windowOpened(WindowEvent arg0) {
194.
195. }
196.
197.}
这段程序怎么加一个 快捷键,在界面最小化后 按快捷键就让界面还原? 谢谢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货