-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathCheckBoxMenu.java
49 lines (44 loc) · 1.71 KB
/
CheckBoxMenu.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
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractButton;
import javax.swing.Icon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
//ok
public class JavaCheckBoxMenu{
public static void main(final String args[]){
JFrame frame = new JFrame("Jmenu Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
JMenuItem menuItem1 = new JMenuItem("Open", KeyEvent.VK_N);
fileMenu.add(menuItem1);
JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Option_1");
caseMenuItem.setMnemonic(KeyEvent.VK_C);
fileMenu.add(caseMenuItem);
ActionListener aListener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
AbstractButton aButton = (AbstractButton) event.getSource();
boolean selected = aButton.getModel().isSelected();
String newLabel;
Icon newIcon;
if (selected) {
newLabel = "Value-1";
} else {
newLabel = "Value-2";
}
aButton.setText(newLabel);
}
};
caseMenuItem.addActionListener(aListener);
frame.setJMenuBar(menuBar);
frame.setSize(350, 250);
frame.setVisible(true);
}
}