Most commonly used Layout Managers: FlowLayout, BorderLayout, GridLayout
Instructor Jeff Zhuk

The FlowLayout is the default layout manager for the Panel class. The FlowLayout with Center justification is used by default. There are FlowLayout.CENTER, FlowLayout.LEFT and FlowLayout.RIGHT justifications. FlowLayout places components next to each other into one line. When the components fill the width of a container, FlowLayout will start a new row. Fields in the FlowLayout keep their sizes (not grow) when user stretches the window.



// FlowLayoutExample
public class FlowLayoutExample extends Applet {
    public void init() {
        add(new Button("The Training"));
        add(new Button("Programs are just great!"));
        add(new Button("I have elected one"));
    }
}
The BorderLayout (default for Frame and Dialog) has 5 regions: North, South, East, West and Center

The North and South regions grow horizontally. The East and West Regions grow vertically.
The Center region grows in both directions. It is not a requirement to fill all fields. (see example)


// BorderLayoutExample
public class BorderLayoutExample extends Applet {
    public void init() {
        setLayout(new BorderLayout());
        add("North", new Button("My valued time"));
        add("West", new Button("I"));
        add("Center", new Button("dedicate"));
        add("South", new Button("to Java from the Sun"));        
    }
}

The number of regions for a GridLayout is specified in the constructor. by rows and columns.
The GridLayout makes all regions the same size, equal to the largest.


// MoneyMachine (in the GridLayout)
public class MoneyMachine extends Applet {
    private static final String[] pay = {
      "$10","$20","$25","$50","$75","$99"};
    public void init() {
        setLayout(new GridLayout(3, 2, 2,2));
        for (int i = 0; i < 6; i++)
            add (new Button(pay[i]));
    }
}

Back To AWT Basics