Java Reference
In-Depth Information
The FlowLayout( int , int , int ) constructor takes the following three arguments, in
order:
The alignment, which must be one of five class variables of FlowLayout : CENTER ,
LEFT , RIGHT , LEADING , or TRAILING
n
The horizontal gap between components, in pixels
n
The vertical gap, in pixels
n
The following constructor creates a flow layout manager with centered components, a
horizontal gap of 30 pixels, and a vertical gap of 10:
FlowLayout flo = new FlowLayout(FlowLayout.CENTER, 30, 10);
Box Layout
The next layout manager can be used to stack components from top to bottom or from
left to right. Box layout, managed by the BoxLayout class in the javax.swing package,
improves on flow layout by making sure that components always line up vertically or
horizontally—regardless of how their container is resized.
A box layout manager must be created with two arguments to its constructor: the con-
tainer it will manage and a class variable that sets up vertical or horizontal alignment.
11
The alignment, specified with class variables of the BoxLayout class, can be X_AXIS for
left-to-right horizontal alignment and Y_AXIS for top-to-bottom vertical alignment.
The following code sets up a panel to use vertical box layout:
JPanel optionPane = new JPanel();
BoxLayout box = new BoxLayout(optionPane,
BoxLayout.Y_AXIS);
Components added to the container will line up on the specified axis and be displayed at
their preferred sizes. In horizontal alignment, the box layout manager attempts to give
each component the same height. In vertical alignment, it attempts to give each one the
same width.
The Stacker application in Listing 11.2 contains a panel of buttons arranged with box
layout.
Search WWH ::




Custom Search