Java Reference
In-Depth Information
9.
private JLabel display;
10.
private JMenuItem testItem;
11.
12.
public MenuFrame() {
13.
display = new JLabel("No menu selected.",JLabel.CENTER);
14.
15.
// Create a menu bar and add it to the frame
16.
17.
JMenuBar menubar = new JMenuBar();
18.
this .setJMenuBar(menubar);
19.
20.
// Create and add the menus
21.
JMenu firstMenu = new JMenu("Menu 1");
22.
JMenu secondMenu = new JMenu("Menu 2");
23.
menubar.add(firstMenu);
24.
menubar.add(secondMenu);
25.
26.
// Create the menu items and add them to the menus
27.
JMenuItem firstItem
= new JMenuItem("Item 1");
28.
testItem
= new JMenuItem("Test");
29.
JMenuItem exitItem
= new JMenuItem("Exit");
30.
JMenuItem enableItem = new JMenuItem("Enable Test");
31.
JMenuItem disableItem = new JMenuItem("Disable Test");
32.
33.
firstMenu.add(firstItem);
34.
firstMenu.add(testItem);
35.
firstMenu.addSeparator();
36.
firstMenu.add(exitItem);
37.
38.
secondMenu.add(enableItem);
39.
secondMenu.add(disableItem);
40.
41.
// Create a listener and add it to the menu items
42.
MenuListener menuList = new MenuListener( this );
43.
firstItem.addActionListener(menuList);
44.
testItem.addActionListener(menuList);
45.
exitItem.addActionListener(menuList);
46.
enableItem.addActionListener(menuList);
47.
disableItem.addActionListener(menuList);
48.
49.
// Add the label to the frame
50.
this .getContentPane().add(display,BorderLayout.CENTER);
51.
}
52.
53.
// Method to set the text in the label
54.
public void setText(String text){
Search WWH ::




Custom Search