Java Reference
In-Depth Information
Components are given their preferred size.
■■
The order in which the components are added determines their order
in the container. The first component added appears to the left, and
subsequent components flow in from the right.
■■
If the container is not wide enough to display all of the components, the
components wrap around to a new line.
■■
You can control whether the components are centered, left-justified, or
right-justified.
■■
You can control the vertical and horizontal gap between components.
■■
I always emphasize the importance of what a layout manager does
to your components in terms of resizing them. What is nice about
FlowLayout is that components get their preferred size, meaning that
a FlowLayout manager will not attempt to override the width or height
of a component if you have previously declared a specific size for the
component.
To use FlowLayout in a Frame or JFrame, you need to invoke setLayout() on
the container and pass in a new FlowLayout object. The FlowLayout class has
three constructors:
public FlowLayout(). Creates a new FlowLayout that centers the compo-
nents with a horizontal and vertical gap of five units (where the unit is
pixels in most GUI operating systems).
public FlowLayout(int align). Creates a FlowLayout object with the
specified alignment, which is one of the following values: FlowLayout
.CENTER, FlowLayout.RIGHT, or FlowLayout.LEFT. The horizontal and
vertical gap between components is five units.
public FlowLayout(int align, int hgap, int vgap). Creates a FlowLayout
object with the specified alignment, horizontal gap, and vertical gap.
For example, the following statement instantiates a new FlowLayout man-
ager that justifies components to the right. The horizontal and vertical gap is
not specified, so they will have the default value of 5.
Frame f = new Frame();
f.setLayout(new FlowLayout(FlowLayout.RIGHT));
Search WWH ::




Custom Search