Java Reference
In-Depth Information
return
;
Component
[]
components
=
parent
.
getComponents
();
Dimension contSize
=
parent
.
getSize
();
int
return
int
x
=
0
;
for
for
(
int
int
i
=
0
;
i
<
components
.
length
;
i
++) {
int
int
row
=
i
/
COLUMNS
;
int
int
col
=
i
%
COLUMNS
;
Component c
=
components
[
i
];
Dimension d
=
c
.
getPreferredSize
();
int
int
colWidth
= (
int
int
)(
contSize
.
width
*
widthPercentages
[
col
]);
iif
(
col
==
0
) {
x
=
hpad
;
}
else
else
{
x
+=
hpad
* (
col
-
1
) +
(
int
int
)(
contSize
.
width
*
widthPercentages
[
col
-
1
]);
}
int
int
y
=
vpad
* (
row
) + (
row
*
heights
[
row
]) + (
heights
[
row
]-
d
.
height
);
Rectangle r
=
new
new
Rectangle
(
x
,
y
,
colWidth
,
d
.
height
);
c
.
setBounds
(
r
);
}
}
}
See Also
be confused with the like-named but much simpler
RelativeLayout
in the source distribu-
tion accompanying the
Java Cookbook
; Jim's is more complete.
The
JGoodies Forms package
provides a number of useful additions to help build complex
layouts.
MetaWidget
provides a facility for mapping Javabean properties to Swing UI, but can also
build Android, HTML, and several other layout types.
As mentioned in the Introduction, there are many good books on windowed application pro-
gramming with Java. O'Reilly's
Java Swing
discusses the many Swing components not
covered here, such as
JTable
,
JScrollPane
,
JList
, and
JTree
, and many more. Some
Swing components not covered in this chapter are nontheless used in later recipes in this