Previous | Next | Trail Map | Creating a User Interface | Laying Out Components within a Container


How to Use FlowLayout

Here's an applet that shows a FlowLayout(in the API reference documentation) in action.


Your browser can't run 1.0 Java applets, so here's a picture of the window the program brings up:


As the above applet shows, FlowLayout puts components in a row, sized at their preferred size. If the horizontal space in the container is too small to put all the components in one row, FlowLayout uses multiple rows. Within each row, components are centered (the default), left-aligned, or right-aligned as specified when the FlowLayout is created.

Below is the code that creates the FlowLayout and the components it manages. (Here's the whole program. The program runs either within an applet, with the help of AppletButton, or as an application.)

setLayout(new FlowLayout());
setFont(new Font("Helvetica", Font.PLAIN, 14));
   
add(new Button("Button 1"));
add(new Button("2"));
add(new Button("Button 3"));
add(new Button("Long-Named Button 4"));
add(new Button("Button 5"));
The FlowLayout class has three constructors:
public FlowLayout()
public FlowLayout(int alignment)
public FlowLayout(int alignment,
                  int horizontalGap, int verticalGap)
The alignment argument must have the value FlowLayout.LEFT, FlowLayout.CENTER, or FlowLayout.RIGHT. The horizontalGap and verticalGap arguments specify the number of pixels to put between components. If you don't specify a gap value, FlowLayout acts as if you specified 5 for the gap value.


Previous | Next | Trail Map | Creating a User Interface | Laying Out Components within a Container