Java Reference
In-Depth Information
The code in lines 16 through 37 creates two Colors, a Rooms object, as well
as three Panels that include other components, such as a Choice component and
a CheckboxGroup. An object array of TextArea components also is created.
Layout Managers
In previous projects, Java or Applet Viewer automatically has determined the
placement of components, based on the size of the window listed in the HTML
host document, among other considerations. Alternatively, you might have posi-
tioned components in a window using coordinates. When you position compo-
nents using coordinates, however, it can cause problems when someone using a
monitor with 640 x 480 resolution tries to run a program designed to fit on a
screen with 1280 x 1024 resolution.
To assist in component placement inside containers, Java provides a layout
manager — one of a set of five classes that help programmers organize
components into predefined locations in the window, rather than allowing Java
to place them left to right within the space provided. By using layout managers,
you can instruct the AWT where to place certain components relative to other
components.
The five layout managers provided by Java are FlowLayout, BorderLayout,
GridLayout, CardLayout, and GridBagLayout. Table 5-11 provides a quick
reference table of the five layout managers, how they handle components, and
their features.
Table 5-11 Layout Managers
LAYOUT MANAGER
COMPONENT HANDLING
FEATURES
FlowLayout
Places components in rows from
Three alignment possibilities: LEFT, RIGHT, and
left to right
CENTER
Places components at compass points
BorderLayout
Placement in five possible areas in the container;
North, South, East, and West
specify horizontal and vertical gaps
GridLayout
Places components left to right in a
Specification of number of rows and number of
predetermined grid
columns
CardLayout
Places components in a stack
Support for methods such as first(), last(),
previous(), and next() to display stack
components
GridBagLayout
Places components in grids;
Specification of grid and location of components
components may vary in size
 
Search WWH ::




Custom Search