菜单篇之~~老外杰作~~~~~~~~~~~~~~~~~注释完成
写得很有条理啊 呵呵
1
import
java.awt.
*
;
2 import java.awt.event. * ;
3 import javax.swing.JPopupMenu;
4 import javax.swing.JMenu;
5 import javax.swing.JMenuItem;
6 import javax.swing.JCheckBoxMenuItem;
7 import javax.swing.JRadioButtonMenuItem;
8 import javax.swing.ButtonGroup;
9 import javax.swing.JMenuBar;
10 import javax.swing.KeyStroke;
11 import javax.swing.ImageIcon;
12
13 import javax.swing.JPanel;
14 import javax.swing.JTextArea;
15 import javax.swing.JScrollPane;
16 import javax.swing.JFrame;
17
18 /**/ /* PopupMenuDemo.java requires images/middle.gif. */
19
20 /**/ /*
21 * Like MenuDemo, but with popup menus added.
22 */
23 public class PopupMenuDemo implements ActionListener, ItemListener { // 负责对点击事件的监听
24 JTextArea output; // 负责输出的变量
25 JScrollPane scrollPane; // 创建画板
26 String newline = " \n " ; // 每输出一次,大回车
27
28 public JMenuBar createMenuBar() { // 创建菜单栏JMenuBar 函数
29
30 // 这里要解释一下,J* (如:JMenuBar)是swing提供的
31 // 而, * (如:MenuBar)是由awt提供的
32 JMenuBar menuBar;
33 JMenu menu, submenu;
34 JMenuItem menuItem;
35 JRadioButtonMenuItem rbMenuItem;
36 JCheckBoxMenuItem cbMenuItem;
37
38 // Create the menu bar.
39 menuBar = new JMenuBar(); // 创建菜单栏JMenuBar
40
41 // Build the first menu.
42 menu = new JMenu( " A Menu " ); // 创建第一个menu
43 menu.setMnemonic(KeyEvent.VK_A); // 指定键盘助记符的 menu
44 menu.getAccessibleContext().setAccessibleDescription(
45 " The only menu in this program that has menu items " ); // 第一个menu内容
46 menuBar.add(menu); // 将menu加入菜单栏
47
48 // a group of JMenuItems // 下同
49 menuItem = new JMenuItem( " A text-only menu item " ,
50 KeyEvent.VK_T);
51 // menuItem.setMnemonic(KeyEvent.VK_T); // used constructor instead
52 menuItem.setAccelerator(KeyStroke.getKeyStroke(
53 KeyEvent.VK_1, ActionEvent.ALT_MASK));
54 menuItem.getAccessibleContext().setAccessibleDescription(
55 " This doesn't really do anything " );
56 menuItem.addActionListener( this );
57 menu.add(menuItem);
58
59 ImageIcon icon = createImageIcon( " images/middle.gif " ); // 这里有图片
60 menuItem = new JMenuItem( " Both text and icon " , icon);
61 menuItem.setMnemonic(KeyEvent.VK_B);
62 menuItem.addActionListener( this );
63 menu.add(menuItem);
64
65 menuItem = new JMenuItem(icon);
66 menuItem.setMnemonic(KeyEvent.VK_D);
67 menuItem.addActionListener( this );
68 menu.add(menuItem);
69
70 // a group of radio button menu items
71 menu.addSeparator(); // 将新分隔符追加到菜单的末尾
72
73 ButtonGroup group = new ButtonGroup(); // 创建Button集
74
75 rbMenuItem = new JRadioButtonMenuItem( " A radio button menu item " ); // 加入单选(1)
76 rbMenuItem.setSelected( true );
77 rbMenuItem.setMnemonic(KeyEvent.VK_R);
78 group.add(rbMenuItem);
79 rbMenuItem.addActionListener( this ); // 设置事件监听
80 menu.add(rbMenuItem);
81
82 rbMenuItem = new JRadioButtonMenuItem( " Another one " ); // 加入单选(2)
83 rbMenuItem.setMnemonic(KeyEvent.VK_O);
84 group.add(rbMenuItem);
85 rbMenuItem.addActionListener( this );
86 menu.add(rbMenuItem);
87
88 // a group of check box menu items
89 menu.addSeparator(); // 将新分隔符追加到菜单的末尾
90 cbMenuItem = new JCheckBoxMenuItem( " A check box menu item " ); // 加入复选(1)
91 cbMenuItem.setMnemonic(KeyEvent.VK_C);
92 cbMenuItem.addItemListener( this );
93 menu.add(cbMenuItem);
94
95 cbMenuItem = new JCheckBoxMenuItem( " Another one " ); // 加入复选(2)
96 cbMenuItem.setMnemonic(KeyEvent.VK_H);
97 cbMenuItem.addItemListener( this );
98 menu.add(cbMenuItem);
99
100 // a submenu // 创建具有子菜单的超级菜单
101 menu.addSeparator();
102 submenu = new JMenu( " A submenu " ); // 父menu (submenu)
103 submenu.setMnemonic(KeyEvent.VK_S);
104
105 menuItem = new JMenuItem( " An item in the submenu " ); // 子 menu1 (menuItem)
106 menuItem.setAccelerator(KeyStroke.getKeyStroke(
107 KeyEvent.VK_2, ActionEvent.ALT_MASK));
108 menuItem.addActionListener( this );
109 submenu.add(menuItem);
110
111 menuItem = new JMenuItem( " Another item " ); // 子 menu2 (menuItem)
112 menuItem.addActionListener( this );
113 submenu.add(menuItem);
114 menu.add(submenu);
115
116 // Build second menu in the menu bar. // 另一个菜单
117 menu = new JMenu( " Another Menu " );
118 menu.setMnemonic(KeyEvent.VK_N);
119 menu.getAccessibleContext().setAccessibleDescription(
120 " This menu does nothing " );
121 menuBar.add(menu);
122
123 return menuBar;
124 }
125
126 public Container createContentPane() { // 创建中间画板
127 // Create the content-pane-to-be.
128 JPanel contentPane = new JPanel( new BorderLayout());
129 contentPane.setOpaque( true );
130
131 // Create a scrolled text area.
132 output = new JTextArea( 5 , 30 );
133 output.setEditable( false );
134 scrollPane = new JScrollPane(output);
135
136 // Add the text area to the content pane.
137 contentPane.add(scrollPane, BorderLayout.CENTER);
138
139 return contentPane;
140 }
141
142 public void createPopupMenu() { // 创建右击菜单,,不懂请看我的另一篇文章
143
144 JMenuItem menuItem;
145
146 // Create the popup menu.
147 JPopupMenu popup = new JPopupMenu();
148 menuItem = new JMenuItem( " A popup menu item " );
149 menuItem.addActionListener( this );
150 popup.add(menuItem);
151 menuItem = new JMenuItem( " Another popup menu item " );
152 menuItem.addActionListener( this );
153 popup.add(menuItem);
154
155 // Add listener to the text area so the popup menu can come up.
156 MouseListener popupListener = new PopupListener(popup);
157 output.addMouseListener(popupListener);
158 }
159
160 public void actionPerformed(ActionEvent e) { // 创建菜单监听
161 // 并做出相应的事件
162 JMenuItem source = (JMenuItem)(e.getSource());
163 String s = " Action event detected. "
164 + newline
165 + " Event source: " + source.getText()
166 + " (an instance of " + getClassName(source) + " ) " ;
167 output.append(s + newline);
168 output.setCaretPosition(output.getDocument().getLength());
169 }
170
171 public void itemStateChanged(ItemEvent e) { // 监听多选框
172 JMenuItem source = (JMenuItem)(e.getSource());
173 String s = " Item event detected. "
174 + newline
175 + " Event source: " + source.getText()
176 + " (an instance of " + getClassName(source) + " ) "
177 + newline
178 + " New state: "
179 + ((e.getStateChange() == ItemEvent.SELECTED) ?
180 " selected " : " unselected " );
181 output.append(s + newline);
182 output.setCaretPosition(output.getDocument().getLength());
183 }
184
185 // Returns just the class name -- no package info.
186 protected String getClassName(Object o) { // 创建函数,获取所调用的函数的名称
187 String classString = o.getClass().getName();
188 int dotIndex = classString.lastIndexOf( " . " );
189 return classString.substring(dotIndex + 1 );
190 }
191
192 /** */ /** Returns an ImageIcon, or null if the path was invalid. */
193 protected static ImageIcon createImageIcon(String path) { // 调用前面的gif图片
194 java.net.URL imgURL = PopupMenuDemo. class .getResource(path);
195 if (imgURL != null ) {
196 return new ImageIcon(imgURL);
197 } else {
198 System.err.println( " Couldn't find file: " + path);
199 return null ;
200 }
201 }
202
203 /** */ /**
204 * Create the GUI and show it. For thread safety,
205 * this method should be invoked from the
206 * event-dispatching thread.
207 */
208 private static void createAndShowGUI() { // 创建所须GUI线程(控件),并进行设置
209 // Create and set up the window.
210 JFrame frame = new JFrame( " PopupMenuDemo " );
211 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
212
213 // Create/set menu bar and content pane.
214 PopupMenuDemo demo = new PopupMenuDemo();
215 frame.setJMenuBar(demo.createMenuBar());
216 frame.setContentPane(demo.createContentPane());
217
218 // Create and set up the popup menu.
219 demo.createPopupMenu();
220
221 // Display the window.
222 frame.setSize( 450 , 260 );
223 frame.setVisible( true );
224 }
225
226 public static void main(String[] args) { // 主函数
227 // Schedule a job for the event-dispatching thread:
228 // creating and showing this application's GUI.
229 javax.swing.SwingUtilities.invokeLater( new Runnable() {
230 public void run() {
231 createAndShowGUI();
232 }
233 } );
234 }
235
236 class PopupListener extends MouseAdapter { // 右击菜单监听
237 JPopupMenu popup;
238
239 PopupListener(JPopupMenu popupMenu) {
240 popup = popupMenu;
241 }
242
243 public void mousePressed(MouseEvent e) {
244 maybeShowPopup(e);
245 }
246
247 public void mouseReleased(MouseEvent e) {
248 maybeShowPopup(e);
249 }
250
251 private void maybeShowPopup(MouseEvent e) {
252 if (e.isPopupTrigger()) {
253 popup.show(e.getComponent(),
254 e.getX(), e.getY());
255 }
256 }
257 }
258
259
260
261 }
262
2 import java.awt.event. * ;
3 import javax.swing.JPopupMenu;
4 import javax.swing.JMenu;
5 import javax.swing.JMenuItem;
6 import javax.swing.JCheckBoxMenuItem;
7 import javax.swing.JRadioButtonMenuItem;
8 import javax.swing.ButtonGroup;
9 import javax.swing.JMenuBar;
10 import javax.swing.KeyStroke;
11 import javax.swing.ImageIcon;
12
13 import javax.swing.JPanel;
14 import javax.swing.JTextArea;
15 import javax.swing.JScrollPane;
16 import javax.swing.JFrame;
17
18 /**/ /* PopupMenuDemo.java requires images/middle.gif. */
19
20 /**/ /*
21 * Like MenuDemo, but with popup menus added.
22 */
23 public class PopupMenuDemo implements ActionListener, ItemListener { // 负责对点击事件的监听
24 JTextArea output; // 负责输出的变量
25 JScrollPane scrollPane; // 创建画板
26 String newline = " \n " ; // 每输出一次,大回车
27
28 public JMenuBar createMenuBar() { // 创建菜单栏JMenuBar 函数
29
30 // 这里要解释一下,J* (如:JMenuBar)是swing提供的
31 // 而, * (如:MenuBar)是由awt提供的
32 JMenuBar menuBar;
33 JMenu menu, submenu;
34 JMenuItem menuItem;
35 JRadioButtonMenuItem rbMenuItem;
36 JCheckBoxMenuItem cbMenuItem;
37
38 // Create the menu bar.
39 menuBar = new JMenuBar(); // 创建菜单栏JMenuBar
40
41 // Build the first menu.
42 menu = new JMenu( " A Menu " ); // 创建第一个menu
43 menu.setMnemonic(KeyEvent.VK_A); // 指定键盘助记符的 menu
44 menu.getAccessibleContext().setAccessibleDescription(
45 " The only menu in this program that has menu items " ); // 第一个menu内容
46 menuBar.add(menu); // 将menu加入菜单栏
47
48 // a group of JMenuItems // 下同
49 menuItem = new JMenuItem( " A text-only menu item " ,
50 KeyEvent.VK_T);
51 // menuItem.setMnemonic(KeyEvent.VK_T); // used constructor instead
52 menuItem.setAccelerator(KeyStroke.getKeyStroke(
53 KeyEvent.VK_1, ActionEvent.ALT_MASK));
54 menuItem.getAccessibleContext().setAccessibleDescription(
55 " This doesn't really do anything " );
56 menuItem.addActionListener( this );
57 menu.add(menuItem);
58
59 ImageIcon icon = createImageIcon( " images/middle.gif " ); // 这里有图片
60 menuItem = new JMenuItem( " Both text and icon " , icon);
61 menuItem.setMnemonic(KeyEvent.VK_B);
62 menuItem.addActionListener( this );
63 menu.add(menuItem);
64
65 menuItem = new JMenuItem(icon);
66 menuItem.setMnemonic(KeyEvent.VK_D);
67 menuItem.addActionListener( this );
68 menu.add(menuItem);
69
70 // a group of radio button menu items
71 menu.addSeparator(); // 将新分隔符追加到菜单的末尾
72
73 ButtonGroup group = new ButtonGroup(); // 创建Button集
74
75 rbMenuItem = new JRadioButtonMenuItem( " A radio button menu item " ); // 加入单选(1)
76 rbMenuItem.setSelected( true );
77 rbMenuItem.setMnemonic(KeyEvent.VK_R);
78 group.add(rbMenuItem);
79 rbMenuItem.addActionListener( this ); // 设置事件监听
80 menu.add(rbMenuItem);
81
82 rbMenuItem = new JRadioButtonMenuItem( " Another one " ); // 加入单选(2)
83 rbMenuItem.setMnemonic(KeyEvent.VK_O);
84 group.add(rbMenuItem);
85 rbMenuItem.addActionListener( this );
86 menu.add(rbMenuItem);
87
88 // a group of check box menu items
89 menu.addSeparator(); // 将新分隔符追加到菜单的末尾
90 cbMenuItem = new JCheckBoxMenuItem( " A check box menu item " ); // 加入复选(1)
91 cbMenuItem.setMnemonic(KeyEvent.VK_C);
92 cbMenuItem.addItemListener( this );
93 menu.add(cbMenuItem);
94
95 cbMenuItem = new JCheckBoxMenuItem( " Another one " ); // 加入复选(2)
96 cbMenuItem.setMnemonic(KeyEvent.VK_H);
97 cbMenuItem.addItemListener( this );
98 menu.add(cbMenuItem);
99
100 // a submenu // 创建具有子菜单的超级菜单
101 menu.addSeparator();
102 submenu = new JMenu( " A submenu " ); // 父menu (submenu)
103 submenu.setMnemonic(KeyEvent.VK_S);
104
105 menuItem = new JMenuItem( " An item in the submenu " ); // 子 menu1 (menuItem)
106 menuItem.setAccelerator(KeyStroke.getKeyStroke(
107 KeyEvent.VK_2, ActionEvent.ALT_MASK));
108 menuItem.addActionListener( this );
109 submenu.add(menuItem);
110
111 menuItem = new JMenuItem( " Another item " ); // 子 menu2 (menuItem)
112 menuItem.addActionListener( this );
113 submenu.add(menuItem);
114 menu.add(submenu);
115
116 // Build second menu in the menu bar. // 另一个菜单
117 menu = new JMenu( " Another Menu " );
118 menu.setMnemonic(KeyEvent.VK_N);
119 menu.getAccessibleContext().setAccessibleDescription(
120 " This menu does nothing " );
121 menuBar.add(menu);
122
123 return menuBar;
124 }
125
126 public Container createContentPane() { // 创建中间画板
127 // Create the content-pane-to-be.
128 JPanel contentPane = new JPanel( new BorderLayout());
129 contentPane.setOpaque( true );
130
131 // Create a scrolled text area.
132 output = new JTextArea( 5 , 30 );
133 output.setEditable( false );
134 scrollPane = new JScrollPane(output);
135
136 // Add the text area to the content pane.
137 contentPane.add(scrollPane, BorderLayout.CENTER);
138
139 return contentPane;
140 }
141
142 public void createPopupMenu() { // 创建右击菜单,,不懂请看我的另一篇文章
143
144 JMenuItem menuItem;
145
146 // Create the popup menu.
147 JPopupMenu popup = new JPopupMenu();
148 menuItem = new JMenuItem( " A popup menu item " );
149 menuItem.addActionListener( this );
150 popup.add(menuItem);
151 menuItem = new JMenuItem( " Another popup menu item " );
152 menuItem.addActionListener( this );
153 popup.add(menuItem);
154
155 // Add listener to the text area so the popup menu can come up.
156 MouseListener popupListener = new PopupListener(popup);
157 output.addMouseListener(popupListener);
158 }
159
160 public void actionPerformed(ActionEvent e) { // 创建菜单监听
161 // 并做出相应的事件
162 JMenuItem source = (JMenuItem)(e.getSource());
163 String s = " Action event detected. "
164 + newline
165 + " Event source: " + source.getText()
166 + " (an instance of " + getClassName(source) + " ) " ;
167 output.append(s + newline);
168 output.setCaretPosition(output.getDocument().getLength());
169 }
170
171 public void itemStateChanged(ItemEvent e) { // 监听多选框
172 JMenuItem source = (JMenuItem)(e.getSource());
173 String s = " Item event detected. "
174 + newline
175 + " Event source: " + source.getText()
176 + " (an instance of " + getClassName(source) + " ) "
177 + newline
178 + " New state: "
179 + ((e.getStateChange() == ItemEvent.SELECTED) ?
180 " selected " : " unselected " );
181 output.append(s + newline);
182 output.setCaretPosition(output.getDocument().getLength());
183 }
184
185 // Returns just the class name -- no package info.
186 protected String getClassName(Object o) { // 创建函数,获取所调用的函数的名称
187 String classString = o.getClass().getName();
188 int dotIndex = classString.lastIndexOf( " . " );
189 return classString.substring(dotIndex + 1 );
190 }
191
192 /** */ /** Returns an ImageIcon, or null if the path was invalid. */
193 protected static ImageIcon createImageIcon(String path) { // 调用前面的gif图片
194 java.net.URL imgURL = PopupMenuDemo. class .getResource(path);
195 if (imgURL != null ) {
196 return new ImageIcon(imgURL);
197 } else {
198 System.err.println( " Couldn't find file: " + path);
199 return null ;
200 }
201 }
202
203 /** */ /**
204 * Create the GUI and show it. For thread safety,
205 * this method should be invoked from the
206 * event-dispatching thread.
207 */
208 private static void createAndShowGUI() { // 创建所须GUI线程(控件),并进行设置
209 // Create and set up the window.
210 JFrame frame = new JFrame( " PopupMenuDemo " );
211 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
212
213 // Create/set menu bar and content pane.
214 PopupMenuDemo demo = new PopupMenuDemo();
215 frame.setJMenuBar(demo.createMenuBar());
216 frame.setContentPane(demo.createContentPane());
217
218 // Create and set up the popup menu.
219 demo.createPopupMenu();
220
221 // Display the window.
222 frame.setSize( 450 , 260 );
223 frame.setVisible( true );
224 }
225
226 public static void main(String[] args) { // 主函数
227 // Schedule a job for the event-dispatching thread:
228 // creating and showing this application's GUI.
229 javax.swing.SwingUtilities.invokeLater( new Runnable() {
230 public void run() {
231 createAndShowGUI();
232 }
233 } );
234 }
235
236 class PopupListener extends MouseAdapter { // 右击菜单监听
237 JPopupMenu popup;
238
239 PopupListener(JPopupMenu popupMenu) {
240 popup = popupMenu;
241 }
242
243 public void mousePressed(MouseEvent e) {
244 maybeShowPopup(e);
245 }
246
247 public void mouseReleased(MouseEvent e) {
248 maybeShowPopup(e);
249 }
250
251 private void maybeShowPopup(MouseEvent e) {
252 if (e.isPopupTrigger()) {
253 popup.show(e.getComponent(),
254 e.getX(), e.getY());
255 }
256 }
257 }
258
259
260
261 }
262
地震让大伙知道:居安思危,才是生存之道。