Java Reference
In-Depth Information
Display 17.10
Some Layout Managers
LAYOUT MANAGER DESCRIPTION
These layout manager classes are in the
java.awt
package.
FlowLayout
Displays components from left to right in the order in
which they are added to the container.
BorderLayout
Displays the components in five areas: north, south,
east, west, and center. You specify the area a component
goes into in a second argument of the
add
method.
GridLayout
Lays out components in a grid, with each component
stretched to fill its box in the grid.
Self-Test Exercises
14. In Display 17.7, would it be legal to replace
JLabel label1 =
new
JLabel("First label");
add(label1, BorderLayout.NORTH);
JLabel label2 =
new
JLabel("Second label");
add(label2, BorderLayout.SOUTH);
JLabel label3 =
new
JLabel("Third label");
add(label3, BorderLayout.CENTER);
with the following?
JLabel aLabel =
new
JLabel("First label");
add(aLabel, BorderLayout.NORTH);
aLabel =
new
JLabel("Second label");
add(aLabel, BorderLayout.SOUTH);
aLabel =
new
JLabel("Third label");
add(aLabel, BorderLayout.CENTER);
In other words, can we reuse the variable
aLabel
or must each label have its own
variable name?
15. How would you modify the class definition in Display 17.7 so that the three
labels are displayed as follows?
First label
Second label
Third label
(There may be space between each pair of lines.)