Java Reference
In-Depth Information
0
1
2
3
0
B
A
1
E
2
D
C
F
3
Figure 17.1 Sketch of the desired layout. The tick marks at the upper and left margins
indicate the dimensions of the panels. For example, panels A and C should be half as wide
as the parent component and panel A should be three-quarters the height of the parent
component. The underlying 4
4 grid is shown as dashed lines. With respect to this grid,
panel C has a width of 2 (columns) and a height of 1 (row). Its position is column 0 and
row 3
×
Table 17.1 The size and location parameters for the six panels in the layout of
Figure 17.1
Panel
Width
Height
Left
Top
A
2
3
0
0
B
2
2
2
0
C
2
1
0
3
D
1
2
2
2
E
1
1
3
2
F
1
1
3
3
Using these values would give the result shown in Figure 17.2. The corresponding
program GridBagFrameBad is not listed here but can be downloaded from the
topic's home page. The figure shows two deficiencies: the panels A and C are only
one-third the width of the frame (not a half) and panel B is only one-third the
height of the frame, not a half. This is because when the layout manager computes
the grid size from the values in Table 17.1 it deletes unnecessary column and row
boundaries. These are boundaries that do not coincide with the boundary of some
component. In our example the boundary between columns 0 and 1 in Figure 17.1
is unnecessary. At no place does it coincide with a component boundary. On the
other hand the boundary between columns 2 and 3 is necessary; it coincides with
the right boundary of panel D and the left boundaries of panels E and F . Inspecting
the other row and column boundaries shows that the one between row 0 and row 1
is unnecessary and is thus deleted by the layout manager. All other bounds are
necessary. The layout used by the layout manager is then a 3
3 grid. All columns
are equally wide and all rows are equally high. This explains why the result shown
in Figure 17.2 differs from the specification in Figure 17.1.
The way to adjust the width and height of components is thus not to introduce
additional columns and rows but to assign weights to the columns and rows. We
×
Search WWH ::




Custom Search