FlowLayout Class:
The FlowLayout class permits you to lay out controls in rows. A Control is placed in rows as long as there is room. Subsequent controls are placed in the next row after a row has been filled. The Figure displays the flow layout applet.
A flow applet creates a easy FlowLayout:
import java.awt.*;
public class flow extends java.applet.Applet {
public void init() {
setLayout( new FlowLayout() ) ;
add( new Button( "One" ) ) ;
add( new Button( "Two" ) ) ;
add( new Button( "Three" ) ) ;
add( new Button( "Four" ) ) ;
add( new Button( "Five" ) ) ;
}
}
Within this applet, a FlowLayout constructor is called without parameters. There are two other overloaded constructors for the FlowLayout class. These permit you to fine tune the FlowLayout to meet your particular requirements.
The first constructor takes one parameter. Through FlowLayout.CENTER, passingFlowLayout.LEFT, or FlowLayout.RIGHT, you specify the alignment for the controls. A default alignment (whenever you don't specify one) is FlowLayout.CENTER. Thus, to align the buttons on the left in the flow applet, you replace the call to setLayout() along with this call:
setLayout( new FlowLayout( FlowLayout.LEFT ) ) ;
Layouts also provide you control over the amount of space among controls. A FlowLayout() method fills the first row and then every following row as necessary. If the layout needs more than one row, you can specify the vertical spacing as well. A constructor that does this takes an alignment parameter followed through two parameters specifying the spacing among controls. For make the flow applet place their controls centered with ten pixels of horizontal gap and five pixels of vertical gap that use the given code:
setLayout( new FlowLayout( FlowLayout.CENTER, 20, 9 ) ) ;