-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuOptionsDemo.java
104 lines (77 loc) · 3.01 KB
/
MenuOptionsDemo.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
/* MenuOptionsDemo.java
*
* This program illustrates both options for creating menus in a single program:
* a "native awt" menu, and a "swing" menu. ONE WOULD NOT ORDINARILY DO THIS IN
* A SINGLE APPLICATION - IT IS BEING DONE HERE ONLY TO ILLUSTRATE THE DIFFERENCE
* BETWEEN THE TWO APPROACHES TO MENUING
*
* Copyright (c) 2004 - Russell C. Bjork
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MenuOptionsDemo extends JFrame implements ActionListener {
// Constructor - create the GUI
public MenuOptionsDemo() {
super("Menu Options Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add a label indicating which menu we used
whatWeUsedLabel = new JLabel(" ", JLabel.CENTER);
getContentPane().add(whatWeUsedLabel, BorderLayout.CENTER);
// Add the AWT menu.
MenuBar menuBar = new MenuBar();
Menu awtColorMenu = new Menu("Color (awt version)");
menuBar.add(awtColorMenu);
MenuItem awtRedItem = new MenuItem("Red", new MenuShortcut('R'));
awtRedItem.addActionListener(this);
awtColorMenu.add(awtRedItem);
MenuItem awtGreenItem = new MenuItem("Green", new MenuShortcut('G'));
awtGreenItem.addActionListener(this);
awtColorMenu.add(awtGreenItem);
MenuItem awtBlueItem = new MenuItem("Blue", new MenuShortcut('B'));
awtBlueItem.addActionListener(this);
awtColorMenu.add(awtBlueItem);
setMenuBar(menuBar);
// Add the swing menu.
JMenuBar jMenuBar = new JMenuBar();
JMenu swingColorMenu = new JMenu("Color (swing version)");
jMenuBar.add(swingColorMenu);
JMenuItem swingRedItem = new JMenuItem("Red", 'R');
swingRedItem.addActionListener(this);
swingColorMenu.add(swingRedItem);
JMenuItem swingGreenItem = new JMenuItem("Green", 'G');
swingGreenItem.addActionListener(this);
swingColorMenu.add(swingGreenItem);
JMenuItem swingBlueItem = new JMenuItem("Blue", 'B');
swingBlueItem.addActionListener(this);
swingColorMenu.add(swingBlueItem);
setJMenuBar(jMenuBar);
setSize(300, 200);
}
// Action listener
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source instanceof MenuItem) {
whatWeUsedLabel.setText("We did it AWT's way");
} else if (source instanceof JMenuItem) {
whatWeUsedLabel.setText("We did it Swing's way");
}
String command = event.getActionCommand();
if (command.equals("Red")) {
getContentPane().setBackground(Color.red);
whatWeUsedLabel.setForeground(Color.black);
} else if (command.equals("Green")) {
getContentPane().setBackground(Color.green);
whatWeUsedLabel.setForeground(Color.black);
} else if (command.equals("Blue")) {
getContentPane().setBackground(Color.blue);
whatWeUsedLabel.setForeground(Color.white);
}
repaint();
}
// Main program - Create the GUI and let it do the work
public static void main(String [] args) {
new MenuOptionsDemo().setVisible(true);
}
private JLabel whatWeUsedLabel;
}