Java Reference
In-Depth Information
24.
It will not compile, but will give compiler error messages saying that, in effect,
the invocations of
addActionListener
such as
redButton.addActionListener(
this
);
have arguments of an incorrect type.
25. Clicking a
JMenuItem
fires an action event (that is, an object of the class
Action-
Event
). This is the same as with a
JButton
.
26.
JButton b =
new
JButton("Hello");
b.setActionCommand("Bye");
27.
JMenuItem
m =
new
JMenuItem
("
Hello
");
m.setActionCommand("Bye");
28. To change the action command for a
JMenuItem
, you use the method
set-
ActionCommand
, just as you would for a
JButton
.
29. Yes, it is legal.
30. As many as you want. Only one can be added with the method
setJMenuBar
, but
any number of others can be added to using the
add
method.
31.
setJMenuBar
32.
JMenuItem aChoice =
new
JMenuItem("Exit");
33.
m.add(mItem);
mBar.add(m);
setJMenuBar(mBar);
You could use the following instead of using
setJMenuBar
:
add(mBar);
This will all take place inside a constructor named
MenuGUI
.
34.
Register all three types of listeners with
blueChoice
, as follows:
blueChoice.addActionListener(
new
RedListener());
blueChoice.addActionListener(
new
WhiteListener());
blueChoice.addActionListener(
new
BlueListener());
35.
Replace the three inner classes with the following inner class:
private class
ColorListener
implements
ActionListener
{
private
JPanel thePanel;
private
Color theColor;
public
ColorListener(Color c, JPanel p)
{