-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuDemo.java
More file actions
executable file
·156 lines (128 loc) · 4.6 KB
/
MenuDemo.java
File metadata and controls
executable file
·156 lines (128 loc) · 4.6 KB
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
// The "MenuDemo" class.
import javax.swing.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
public class MenuDemo implements ActionListener, ItemListener //ItemListener is for checkboxes in menu
{
JFrame frame;
JPanel myPanel;
JMenuBar mainMenu;
JMenu gameMenu, subMenu, levelMenu, helpMenu;
JMenuItem newOption, exitOption, aboutOption, subMenuOne, subMenuTwo;
JLabel label;
JCheckBoxMenuItem beginnerOption, intermediateOption, advancedOption;
String message;
int level;
public MenuDemo ()
{
frame = new JFrame ("Working with Menus");
myPanel = new JPanel ();
myPanel.setPreferredSize(new Dimension(400, 400));
myPanel.setLocation (300,300);
myPanel.setLayout (new BoxLayout (myPanel, BoxLayout.PAGE_AXIS));
myPanel.setBackground (Color.white);
myPanel.setBorder (BorderFactory.createEmptyBorder (10,10,10,10));
// Set up the Menu
// Set up the Game MenuItems with Short cuts
newOption = new JMenuItem ("New", KeyEvent.VK_N);
exitOption = new JMenuItem ("Exit", KeyEvent.VK_X);
// Set up the Game Menu
gameMenu = new JMenu ("Game");
// Add each MenuItem to the Game Menu (with a separator)
gameMenu.add (newOption);
// Create a sub menu
subMenu = new JMenu ("Sub Menu");
subMenuOne = new JMenuItem ("First Sub Menu Choice");
subMenuTwo = new JMenuItem ("Second Sub Menu Choice");
subMenu.add (subMenuOne);
subMenu.add (subMenuTwo);
gameMenu.add (subMenu);
gameMenu.addSeparator ();
gameMenu.add (exitOption);
// Set up the Level Menu with the check boxes
beginnerOption = new JCheckBoxMenuItem ("Beginner", true);
level = 1;
intermediateOption = new JCheckBoxMenuItem ("Intermediate", false);
advancedOption = new JCheckBoxMenuItem ("Advanced", false);
levelMenu = new JMenu ("Level");
levelMenu.add (beginnerOption);
levelMenu.add (intermediateOption);
levelMenu.add (advancedOption);
// Set up the Help Menu
aboutOption = new JMenuItem ("About...");
helpMenu = new JMenu ("Help");
helpMenu.add (aboutOption);
// Set up the Menu Bar and add the above Menus
mainMenu = new JMenuBar ();
mainMenu.add (gameMenu);
mainMenu.add (levelMenu);
//mainMenu.setHelpMenu (helpMenu);
mainMenu.add (helpMenu);
// Set the menu bar for this frame to mainMenu
frame.setJMenuBar (mainMenu);
label = new JLabel ("Welcome to the Menu Demo"); // initial message
myPanel.add (label);
newOption.setActionCommand ("New");
newOption.addActionListener(this);
exitOption.setActionCommand ("Exit");
exitOption.addActionListener(this);
aboutOption.setActionCommand ("About");
aboutOption.addActionListener(this);
subMenuOne.setActionCommand ("SubMenu1");
subMenuOne.addActionListener(this);
subMenuTwo.setActionCommand ("SubMenu2");
subMenuTwo.addActionListener(this);
beginnerOption.setActionCommand ("Beginner");
beginnerOption.addItemListener(this);
intermediateOption.setActionCommand ("Intermediate");
intermediateOption.addItemListener(this);
advancedOption.setActionCommand ("Advanced");
advancedOption.addItemListener(this);
frame.add(myPanel);
frame.pack();
frame.setVisible(true);
}
// To handle normal menu items
public void actionPerformed (ActionEvent event) {
String eventName = event.getActionCommand();
if (eventName.equals ("New")) {
newGame ();
}
else if (eventName.equals ("SubMenu1"))
label.setText ("First Sub Menu Choice Selected");
else if (eventName.equals ("SubMenu2"))
label.setText ("Second Sub Menu Choice Selected");
else if (eventName.equals ("Exit")) {
System.exit (0);
}
else if (eventName.equals ("About"))
JOptionPane.showMessageDialog (frame, (Object) "Menu Event \nProcessing Demo", "About...", JOptionPane.INFORMATION_MESSAGE);
}
// To handle menu items that are checkboxes or radio buttons
public void itemStateChanged(ItemEvent event) {
if (event.getSource() == beginnerOption)
label.setText ("Beginner level selected");
else if (event.getSource () == intermediateOption)
label.setText ("Intermediate level selected");
else if (event.getSource () == advancedOption)
label.setText ("Advanced level selected");
}
public void newGame ()
{
message = "New Game Selected at the ";
if (level == 1)
message += "Beginner Level";
else if (level == 2)
message += "Intermediate Level";
else
message += "Advanced Level";
}
public static void main (String [] args){
new MenuDemo ();
}
}