Java Reference
In-Depth Information
buttons as possible on one row within the panel, and then starts putting compo-
nents on another row. When the size of the frame is widened (by dragging the
lower-right corner with the mouse, for example), the panel grows as well, and
more buttons can fit on a row. When the frame is resized, the layout manager is
consulted and the components are reorganized automatically. Note that on each
row the components are centered within the window by default.
The constructor of the FlowLayout class is overloaded to allow the program-
mer to tailor the characteristics of the layout manager. Within each row, com-
ponents are either centered, left aligned, or right aligned. The alignment defaults
to centered. The horizontal and vertical gap size between components also can
be specified when the layout manager is created. The FlowLayout class also has
methods to set the alignment and gap sizes after the layout manager is created.
VideoNote
Discussion of
layout
managers.
Border Layout
A border layout has five areas to which components can be added: North, South,
East, West, and Center. The areas have a particular positional relationship to each
other, as shown in Figure 7.8.
The four outer areas become as big as needed in order to accommodate the
component they contain. If no components are added to the North, South, East,
or West areas, these areas do not take up any room in the overall layout. The
Center area expands to fill any available space.
A particular container might use only a few areas, depending on the functional-
ity of the system. For example, a program might use only the Center, South, and
West areas. This versatility makes border layout a very useful layout manager.
The add method for a container governed by a border layout takes as its
first parameter the component to be added. The second parameter indicates the
area to which it is added. The area is specified using constants defined in the
BorderLayout class. Listing 7.21 shows the panel used by the LayoutDemo pro-
gram to demonstrate the border layout.
North
West
Center
East
South
FIGURE 7.8 Border layout organizes components in five areas
 
Search WWH ::




Custom Search