Java Reference
In-Depth Information
The add() method then can be used to add the internal containers to the deck,
using the code:
deck.add(myPanel);
deck.add(myDialog);
Only one card is visible at any one time. CardLayout uses four methods —
first(), last(), previous(), and next() to display the cards, with the name of
the container as an argument, as in the following code:
myCards.previous(deck);
Figure 5-25 displays an application that is typical of how Java programmers
combine some of the different layout managers to achieve certain visual effects.
The application combines both the GridLayout and the CardLayout. On the left
is a set of Button components positioned using the GridLayout man-
ager. On the right is a Panel component, positioned using the CardLay-
out manager. The buttons then are programmed to bring a specific
panel to the front. The Panel's back-
ground color has been set to green
to better display its location.
GridBagLayout
components added
using CardLayout
layout manager
Perhaps the most flexible of all
the Java layout managers is the
GridBagLayout. GridBagLayout ,
which is created in a manner similar
to GridLayout, aligns components
horizontally and vertically without
requiring that the components be
the same size. Each GridBagLayout
manager uses a rectangular grid of
cells, with each component occupy-
ing one or more cells. Components
may be added in any order.
Figure 5-26 displays an application
that places components using the
GridBagLayout layout manager.
The use of a layout manager
offers many benefits, including that
of automatically adjusting the size
and position of components if the
user resizes the Frame.
Panel
component
Button
components
FIGURE 5-25
components added
using GridBagLayout
layout manager
TextArea
component
Button components
Choice component
Checkbox component
TextField
component
FIGURE 5-26
 
Search WWH ::




Custom Search