Java Reference
In-Depth Information
Display 17.16
Listeners as Inner Classes (part 3 of 3)
56
JMenuItem grayChoice = new JMenuItem("Gray");
57
grayChoice.addActionListener( new grayListener());
58
colorMenu.add(grayChoice);
59
JMenuBar bar = new JMenuBar();
60
bar.add(colorMenu);
61
setJMenuBar(bar);
62
}
63
}
Self-Test Exercises
25. What type of event is fired when you click a JMenuItem ? How does it differ from
the type of event fired when you click a JButton ?
26. Write code to create a JButton with "Hello" written on it but with "Bye" as its
action command.
27. Write code to create a JMenuItem with "Hello" as its displayed text (when it is a
choice in a menu) but with "Bye" as its action command.
28. If you want to change the action command for a JButton , you use the method
setActionCommand . What method do you use to change the action command for
a JMenuItem ?
29. Is the following legal in Java?
JMenu aMenu = new JMenu();
...
JMenu aSubMenu = new JMenu();
...
aMenu.add(aSubMenu);
30. How many JMenuBar objects can you have in a JFrame ?
31. A JFrame has a private instance variable of type JMenuBar . What is the name of
the mutator method to change the value of this instance variable?
32. Write code to create a new menu item named aChoice that has the label "Exit" .
33. Suppose you are defining a class called MenuGUI that is a derived class of the class
JFrame . Write code to add the menu item mItem to the menu m . Then add m to
the menu bar mBar , and then add the menu bar to the JFrame MenuGUI . Assume
that this all takes place inside a constructor for MenuGUI . Also assume that every-
thing has already been constructed with new , and that all necessary listeners are
registered. You just need to do the adding of things.
 
Search WWH ::




Custom Search