Java Reference
In-Depth Information
Alternatively, you could use
new GridLayout(1, 0)
. It is also possible to do
something similar with a
BorderLayout
manager or a
FlowLayout
manager, but
a
GridLayout
manager will work nicer here.
18. The argument should be
new GridLayout(0,1)
. So, the entire method invocation is
setLayout(
new
GridLayout(0, 1));
Alternatively, you could use
new GridLayout(3, 1)
, if you know there will be at
most three components added, but if more than three components are added, then
a second column will be added. It is also possible to do something similar with a
BorderLayout
manager, but a
GridLayout
manager will work nicer here.
19.
java.awt
20. An object of the class
JPanel
is both a container class and a component class.
21. To make it look as though you have an empty grid element, add an empty panel
to the grid element.
22.
import
javax.swing.JPanel;
import
java.awt.Color;
public class
PinkJPanel
extends
JPanel
{
public
PinkJPanel()
{
setBackground(Color.PINK);
}
}
extra code
on website
The class
PinkJPanel
is on the website that accompanies this text.
23. It will not compile, but will give a compiler error message saying that
actionPerformed
is not defined (because it claims to implement the
ActionListener
interface).
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
ActionEvent
). 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
, use the method
setAction
Command
, just as you would for a
JButton
.
29. Yes, it is legal.