Java Reference
In-Depth Information
■
Note
If you need to learn the basics of programming with Java AWT, pick up a copy of
Beginning Java SE 6
Platform: From Novice to Professional
by Jeff Friesen (Apress, 2007) or visit Sun's documentation on the
topic at
http://java.sun.com/javase/6/docs/technotes/guides/awt/
.
Using AWT Containers
In AWT, a container is simply a component that can contain other components. The
lightweight
Panel
container lets you group related components, while the
Window
and
Frame
containers have corresponding heavyweight implementations in the native plat-
form's window environment. A
Window
provides a top-level window in the native
platform's window environment, while a
Frame
provides a top-level title and a border.
A container maintains a list of components and lets you add additional components
to that list. The various
add
methods of
Container
let you add components, specifying not
just the component to add, but where in the list of components the new component
should be, as well as any constraints on how the component should be placed within the
container.
The list represents only the components within a container and the Z-order for those
components; the container's
layout manager
determines how components are laid out
visually within the bounds of the container. The layout manager uses the minimum and
preferred sizes specified by each component and the container bounds and an algorithm
to determine how the components should appear within the container. The AWT defines
five layout managers:
•
BorderLayout
: Arranges its components to fit in five regions: north, south, east,
west, and center. When adding components to a container using the
BorderLayout
,
you indicate which region the component should occupy; the
BorderLayout
consid-
ers the preferred size of each component when positioning each component.
•
CardLayout
: Arranges its components so that each component occupies a virtual
card; only one card (and thus one component) is visible at a time. The
CardLayout
defines a set of methods that allow an application to traverse the cards sequentially
or show a specific card.
•
FlowLayout
: Arranges components in a left-to-right, top-to-bottom flow.
•
GridBagLayout
: Aligns components vertically and horizontally without requiring the
components to be the same size. Each component has a set of corresponding con-
straints that indicates how the component should be placed in the container.
•
GridLayout
: Aligns components vertically and horizontally in a rectangular grid,
requiring each component to be the same size.