CIT 591 Swing Layout Examples
Fall 2004, David Matuszek

Source code for the following examples is available as SwingLayoutExamples.zip.

FlowLayout
import java.awt.*;
import java.applet.*;
import javax.swing.*;
 
public class FlowLayoutExample extends JApplet {
 
  public void init () {
    getContentPane().setLayout(new FlowLayout ());
    getContentPane().add(new JButton("One"));
    getContentPane().add(new JButton("Two"));
    getContentPane().add(new JButton("Three"));
    getContentPane().add(new JButton("Four"));
    getContentPane().add(new JButton("Five"));
    getContentPane().add(new JButton("Six"));
  }
}

GridLayout
import java.awt.*;
import java.applet.*;
import javax.swing.*;
 
public class GridLayoutExample extends JApplet {
 
    public void init() {
        Container c = getContentPane();
        c.setLayout(new GridLayout(2, 4));
        c.add(new JButton("One"));
        c.add(new JButton("Two"));
        c.add(new JButton("Three"));
        c.add(new JButton("Four"));
        c.add(new JButton("Five"));
    }
}

BorderLayout (Applet)
import java.awt.*;
import java.applet.*;
import javax.swing.*;
 
public class BorderLayoutExample extends JApplet {
 
  public void init() {
    Container c = getContentPane();
    c.setLayout(new BorderLayout());
    c.add(new JButton("One"),   BorderLayout.NORTH);
    c.add(new JButton("Two"),   BorderLayout.WEST);
    c.add(new JButton("Three"), BorderLayout.CENTER);
    c.add(new JButton("Four"),  BorderLayout.EAST);
    c.add(new JButton("Five"),  BorderLayout.SOUTH);
    c.add(new JButton("Six"),   BorderLayout.SOUTH);
  }
}

BorderLayout (Application)
import java.awt.*;
// import java.applet.*;
import javax.swing.*;
 
public class BorderLayoutExample2 extends JFrame {
 
  public static void main(String[] args) {
      BorderLayoutExample2 frame = new BorderLayoutExample2();
      frame.doSomething();
  }
 
  void doSomething() {
    Container c = getContentPane();
    c.setLayout(new BorderLayout());
    c.add(new JButton("One"),   BorderLayout.NORTH);
    c.add(new JButton("Two"),   BorderLayout.WEST);
    c.add(new JButton("Three"), BorderLayout.CENTER);
    c.add(new JButton("Four"),  BorderLayout.EAST);
    c.add(new JButton("Five"),  BorderLayout.SOUTH);
    c.add(new JButton("Six"),   BorderLayout.SOUTH);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setVisible(true);
  }
}

Nesting Panels
import java.awt.*;
import javax.swing.*;


public class FunWithPanels extends JFrame {
 
    public static void main(String[] args) {
        FunWithPanels frame = new FunWithPanels();
        frame.doSomething();
    }
 
    void doSomething() {
        Container c = getContentPane();
 
        JPanel p1 = new JPanel();
        p1.setLayout(new BorderLayout());
        p1.add(new JButton("A"), BorderLayout.NORTH);
        p1.add(new JButton("B"), BorderLayout.WEST);
        p1.add(new JButton("C"), BorderLayout.CENTER);
        p1.add(new JButton("D"), BorderLayout.EAST);
        p1.add(new JButton("E"), BorderLayout.SOUTH);
 
        JPanel p2 = new JPanel();
        p2.setLayout(new GridLayout(3, 2));
        p2.add(new JButton("F"));
        p2.add(new JButton("G"));
        p2.add(new JButton("H"));
        p2.add(new JButton("I"));
        p2.add(new JButton("J"));
        p2.add(new JButton("K"));
 
        JPanel p3 = new JPanel();
        p3.setLayout(new BoxLayout(p3, BoxLayout.Y_AXIS));
        p3.add(new JButton("L"));
        p3.add(new JButton("M"));
        p3.add(new JButton("N"));
        p3.add(new JButton("O"));
        p3.add(new JButton("P"));
 
        c.setLayout(new BorderLayout());
        c.add(p1, BorderLayout.CENTER);
        c.add(p2, BorderLayout.SOUTH);
        c.add(p3, BorderLayout.EAST);
 
        pack();
        setVisible(true);
    }
}