forked from ZhiqingXiao/java-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter19.java
285 lines (228 loc) · 7.88 KB
/
chapter19.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Demonstrate a simple main menu.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MenuDemo implements ActionListener {
JLabel jlab;
MenuDemo() {
// Create a new JFrame container.
JFrame jfrm = new JFrame("Menu Demo");
// Specify FlowLayout for the layout manager.
jfrm.setLayout(new FlowLayout());
// Give the frame an initial size.
jfrm.setSize(220, 200);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a label that will display the menu selection.
jlab = new JLabel();
// Create the menu bar.
JMenuBar jmb = new JMenuBar();
// Create the File menu.
JMenu jmFile = new JMenu("File");
JMenuItem jmiOpen = new JMenuItem("Open");
JMenuItem jmiClose = new JMenuItem("Close");
JMenuItem jmiSave = new JMenuItem("Save");
JMenuItem jmiExit = new JMenuItem("Exit");
jmFile.add(jmiOpen);
jmFile.add(jmiClose);
jmFile.add(jmiSave);
jmFile.addSeparator();
jmFile.add(jmiExit);
jmb.add(jmFile);
// Create the Options menu.
JMenu jmOptions = new JMenu("Options");
// Create the Colors submenu.
JMenu jmColors = new JMenu("Colors");
JMenuItem jmiRed = new JMenuItem("Red");
JMenuItem jmiGreen = new JMenuItem("Green");
JMenuItem jmiBlue = new JMenuItem("Blue");
jmColors.add(jmiRed);
jmColors.add(jmiGreen);
jmColors.add(jmiBlue);
jmOptions.add(jmColors);
// Create the Priority submenu.
JMenu jmPriority = new JMenu("Priority");
JMenuItem jmiHigh = new JMenuItem("High");
JMenuItem jmiLow = new JMenuItem("Low");
jmPriority.add(jmiHigh);
jmPriority.add(jmiLow);
jmOptions.add(jmPriority);
// Create the Reset menu item.
JMenuItem jmiReset = new JMenuItem("Reset");
jmOptions.addSeparator();
jmOptions.add(jmiReset);
// Finally, add the entire Options menu to the menu bar
jmb.add(jmOptions);
// Create the Help menu.
JMenu jmHelp = new JMenu("Help");
JMenuItem jmiAbout = new JMenuItem("About");
jmHelp.add(jmiAbout);
jmb.add(jmHelp);
// Add action listeners for the menu items.
jmiOpen.addActionListener(this);
jmiClose.addActionListener(this);
jmiSave.addActionListener(this);
jmiExit.addActionListener(this);
jmiRed.addActionListener(this);
jmiGreen.addActionListener(this);
jmiBlue.addActionListener(this);
jmiHigh.addActionListener(this);
jmiLow.addActionListener(this);
jmiReset.addActionListener(this);
jmiAbout.addActionListener(this);
// Add the label to the content pane.
jfrm.add(jlab);
// Add the menu bar to the frame.
jfrm.setJMenuBar(jmb);
// Display the frame.
jfrm.setVisible(true);
}
// Handle menu item action events.
public void actionPerformed(ActionEvent ae) {
// Get the action command from the menu selection.
String comStr = ae.getActionCommand();
// If user chooses Exit, then exit the program.
if(comStr.equals("Exit")) System.exit(0);
// Otherwise, display the selection.
jlab.setText(comStr + " Selected");
}
public static void main(String[] args) {
// Create the GUI on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MenuDemo();
}
});
}
}
// -----------------------------------------
// Try This 19-1: Add and remove menu items dynamically.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class DynMenuDemo implements ActionListener {
JLabel jlab;
JMenuItem jmiYellow;
JMenuItem jmiPurple;
JMenuItem jmiOrange;
JMenu jmColors;
DynMenuDemo() {
// Create a new JFrame container.
JFrame jfrm = new JFrame("Dynamic Menu Demo");
// Specify FlowLayout for the layout manager.
jfrm.setLayout(new FlowLayout());
// Give the frame an initial size.
jfrm.setSize(220, 200);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a label that will display the menu selection.
jlab = new JLabel();
// Create the menu bar.
JMenuBar jmb = new JMenuBar();
// Create the File menu.
JMenu jmFile = new JMenu("File");
JMenuItem jmiOpen = new JMenuItem("Open");
JMenuItem jmiClose = new JMenuItem("Close");
JMenuItem jmiSave = new JMenuItem("Save");
JMenuItem jmiExit = new JMenuItem("Exit");
jmFile.add(jmiOpen);
jmFile.add(jmiClose);
jmFile.add(jmiSave);
jmFile.addSeparator();
jmFile.add(jmiExit);
jmb.add(jmFile);
// Create the Options menu.
JMenu jmOptions = new JMenu("Options");
// Create the Colors submenu.
jmColors = new JMenu("Colors");
JMenuItem jmiRed = new JMenuItem("Red");
JMenuItem jmiGreen = new JMenuItem("Green");
JMenuItem jmiBlue = new JMenuItem("Blue");
jmColors.add(jmiRed);
jmColors.add(jmiGreen);
jmColors.add(jmiBlue);
// Create the More/Fewer Colors Menu item.
JMenuItem jmiMoreLess = new JMenuItem("More Colors");
jmColors.add(jmiMoreLess);
// Add Colors menu to the Options menu.
jmOptions.add(jmColors);
// Create the additional colors. These will be
// added or removed on demand.
jmiYellow = new JMenuItem("Yellow");
jmiPurple = new JMenuItem("Purple");
jmiOrange = new JMenuItem("Orange");
// Create the Priority submenu.
JMenu jmPriority = new JMenu("Priority");
JMenuItem jmiHigh = new JMenuItem("High");
JMenuItem jmiLow = new JMenuItem("Low");
jmPriority.add(jmiHigh);
jmPriority.add(jmiLow);
// Add the Priority menu to the Options menu.
jmOptions.add(jmPriority);
// Create the Reset menu item.
JMenuItem jmiReset = new JMenuItem("Reset");
jmOptions.addSeparator();
jmOptions.add(jmiReset);
// Finally, add the entire Options menu to the menu bar
jmb.add(jmOptions);
// Create the Help menu.
JMenu jmHelp = new JMenu("Help");
JMenuItem jmiAbout = new JMenuItem("About");
jmHelp.add(jmiAbout);
jmb.add(jmHelp);
// Add action listeners for the menu items.
jmiOpen.addActionListener(this);
jmiClose.addActionListener(this);
jmiSave.addActionListener(this);
jmiExit.addActionListener(this);
jmiRed.addActionListener(this);
jmiGreen.addActionListener(this);
jmiBlue.addActionListener(this);
jmiHigh.addActionListener(this);
jmiLow.addActionListener(this);
jmiReset.addActionListener(this);
jmiAbout.addActionListener(this);
// Add listeners for the additional colors option.
jmiMoreLess.addActionListener(this);
jmiYellow.addActionListener(this);
jmiPurple.addActionListener(this);
jmiOrange.addActionListener(this);
// Add the label to the content pane.
jfrm.add(jlab);
// Add the menu bar to the frame.
jfrm.setJMenuBar(jmb);
// Display the frame.
jfrm.setVisible(true);
}
// Handle menu item action events.
public void actionPerformed(ActionEvent ae) {
// Get the action command from the menu selection.
String comStr = ae.getActionCommand();
// If user chooses Exit, then exit the program.
if(comStr.equals("Exit"))
System.exit(0);
else if(comStr.equals("More Colors")) {
jmColors.add(jmiYellow);
jmColors.add(jmiPurple);
jmColors.add(jmiOrange);
JMenuItem mi = (JMenuItem) ae.getSource();
mi.setText("Fewer Colors");
} else if(comStr.equals("Fewer Colors")) {
jmColors.remove(jmiYellow);
jmColors.remove(jmiPurple);
jmColors.remove(jmiOrange);
JMenuItem mi = (JMenuItem) ae.getSource();
mi.setText("More Colors");
}
// Otherwise, display the selection.
jlab.setText(comStr + " Selected");
}
public static void main(String[] args) {
// Create the GUI on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new DynMenuDemo();
}
});
}
}