ptg7068951
190 HOUR 14: Laying Out a User Interface
GridLayout grid = new GridLayout(2, 3);
setLayout(grid);
Figure 14.2 shows what the Crisisapplication would look like if it used
grid layout.
FIGURE 14.2
Arranging components using grid
layout.
Some labels in Figure 14.2 display text that has been shortened. If the text
is wider than the area available in the component, the label is shortened
using ellipses (...).
TheBorderLayoutManager
The BorderLayoutclass, also in java.awt, arranges components at specific
positions within the container that are identified by one of five directions:
north, south, east, west, or center.
The BorderLayoutmanager arranges components into five areas: four
denoted by compass directions and one for the center area. When you add
a component under this layout, the add()method includes a second argu-
ment to specify where the component should be placed. This argument
should be one of five class variables of the BorderLayoutclass: NORTH,
SOUTH, EAST, WEST, and CENTERare used for this argument.
Like the GridLayoutclass, BorderLayoutdevotes all available space to the
components. The component placed in the center is given all the space that
isn’t needed for the four border components, so it’s usually the largest.
The following statements create a container that uses border layout:
BorderLayout crisisLayout = new BorderLayout();
setLayout(crisisLayout);
add(panicButton, BorderLayout.NORTH);
add(dontPanicButton, BorderLayout.SOUTH);
add(blameButton, BorderLayout.EAST);
add(mediaButton, BorderLayout.WEST);
add(saveButton, BorderLayout.CENTER);
Figure 14.3 shows how this looks in the Crisisapplication.