Java Reference
In-Depth Information
Figure 2.6 Output of
LayoutDriver
. Standard flow layout; flow layout with bigger spac-
ing; grid layout
row number is adjusted depending on the number of embedded components. In
program
LayoutFrame
we generate a 2
×
4 grid for five components. The layout
manager then generates a 2
3 grid, where only one cell is empty. Sometimes
one embeds
dummy components
into a grid layout. These are Swing components,
usually labels, with no function other than to fill certain cells. They can be used to
produce an 'empty' cell between two other cells. Chapter 6 contains an example.
The dimensions of the embedded components are basically ignored by the grid
layout; the components completely fill the cells. See also Figure 2.5c.
×
!
Below we list the constructor and some methods for grid layouts. The number
of rows
r
and columns
c
and also the width of the gaps can be passed between the
columns (
hdist
) and rows (
vdist
):
GridLayout(
int
r,
int
c);
setHgap(
int
hdist);
setVgap(
int
vdist);
The following listings
LayoutFrame
and
LayoutDriver
show examples for the
layouts. The first defines a frame for which the layout can be selected by passing
a
LayoutManager
in the constructor.
Program
LayoutDriver
generates three
LayoutFrame
s and makes them visible.
The first one is given a standard flow layout. The second one receives a flow
layout with specified distances between rows/columns and left alignment. The
third frame is given a 2
4 grid layout. The locations in the
showIt
instructions
are set in such a way that the three frames appear side by side. Figure 2.6 shows
the result.
×
File:
its/Layouts/LayoutFrame.java
1.
package
its.Layouts;
2.
3.
import
java.awt.LayoutManager;
4.
import
its.SimpleFrameWithPanels.ColorPanel;
5.
import
java.awt.Color;
6.
import
its.SimpleFrame.*;
7.
Search WWH ::
Custom Search