Java Reference
In-Depth Information
import java.awt.*;
public class Starter extends javax.swing.JFrame {
public Starter() {
FlowLayout lm = new FlowLayout();
setLayout(lm);
// add components here
}
}
After the layout manager is set, you can start adding components to the container that it
manages. For some of the layout managers, such as
FlowLayout
, the order in which com-
ponents are added is significant. You'll see this as you work with each of the managers.
Flow Layout
The
FlowLayout
class in the
java.awt
package is the simplest layout manager. It lays
out components in rows in a manner similar to the way words are laid out on a page—
from left to right until there's no more room at the right edge and then on to the leftmost
point on the next row.
By default, the components on each row will be centered when you use the
FlowLayout
()
constructor with no arguments. If you want the components to be aligned along the
left or right edge of the container, the
FlowLayout.LEFT
or
FlowLayout.RIGHT
class vari-
able can be used as the constructor's only argument, as in the following statement:
11
FlowLayout righty = new FlowLayout(FlowLayout.RIGHT);
The
FlowLayout.CENTER
class variable is used to specify a centered alignment for com-
ponents.
NOTE
If you need to align components for a non-English speaking audi-
ence where left-to-right order does not make sense, the
FlowLayout.LEADING
and
FlowLayout.TRAILING
variables can be
used. They set justification to either the side of the first compo-
nent in a row or the last, respectively.
The application in Listing 11.1 displays six buttons arranged by the flow layout manager.
Because the
FlowLayout.LEFT
class variable was used in the
FlowLayout()
constructor,
the components are lined up along the left side of the application window.