Java Reference
In-Depth Information
FlowLayout
FlowLayout
is the default layout manager for a Java application. Creating a
FlowLayout
manager is similar to creating a
GridLayout
manager. For example, suppose that you
have the following declaration:
Container pane = getContentPane();
The statement(s):
pane.setLayout(
new
FlowLayout());
or:
FlowLayout flowLayoutMgr =
new
FlowLayout();
pane.setLayout(flowLayoutMgr);
set(s) the layout of the container pane to
FlowLayout
.
FlowLayout
places the compo-
nents from left to right and centered, by default, until no more items can be placed. The
next item(s) will be placed in the second line. Thus, a
FlowLayout
manager works
similarly to a
GridLayout
manager. The main difference between these two layouts is
that in a
GridLayout
, all rows (columns) have the same number of components and all
components have the same size. However, in a
FlowLayout
, there is no such guarantee.
Moreover, in a
FlowLayout
, you can align each line left, center, or right using a
statement such as:
flowLayoutMgr.setAlignment(FlowLayout.RIGHT);
Note that the default alignment is
CENTERED
.
The following Java application program illustrates the use of the
FlowLayout
manager:
//Program to illustrate FlowLayout
import
javax.swing.*;
import
java.awt.*;
public class
FlowLayoutExample
extends
JFrame
{
private static int
WIDTH = 350;
private static int
HEIGHT = 350;
//Variables to create GUI components
private
JLabel labelJL;
private
JTextField textFieldTF;
private
JButton buttonJB;
private
JCheckBox checkboxCB;
private
JRadioButton radioButtonRB;
private
JTextArea textAreaTA;
Search WWH ::
Custom Search