Java Reference
In-Depth Information
GridLayout
The GridLayout layout manager divides the container into a grid so that
components can be placed in rows and columns, from left to right and then top
to bottom, within the grid. The following line of code:
setLayout(new GridLayout(3,4));
instructs the layout manager to use the GridLayout with three rows and four
columns. While you can specify the size of the grid to create using the
GridLayout layout manager, you cannot specify into which row or column to put
a component; GridLayout adds components in order, from left to right, top to
bottom, within the grid. Figure 5-24 displays an application with 12 buttons
placed in a 3 x 4 grid using the GridLayout layout manager.
components added
using GridLayout
layout manager
column
row
FIGURE 5-24
CardLayout
A more flexible but slightly more complicated layout manager is
CardLayout. CardLayout is used primarily to house other containers.
CardLayout places other containers in a stack similar to a deck of cards.
Each container in the stack then can use its own layout manager. The container
at the top of the deck is visible when the program is executed.
Often, an instance of CardLayout is constructed for use later in the program,
using the code:
myCards = new CardLayout();
Containers are created and then used to construct the instance, using the code:
deck = new Panel();
deck.setLayout(myCards());
 
Search WWH ::




Custom Search