Java Swing Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.00

java.awt.BorderLayout - Border Layout

This section provides a tutorial example on how to create a BorderLayout to layout components in a container. BorderLayout has 5 fixed sections - north, south, west, east and center.

java.awt.BorderLayout - A very simple layout that:

  • Divides the container into five regions: east, south, west, north, and center.
  • Takes maximum 5 components only, one per region.
  • Resizes each component to match the size of its region.
  • Acts as the default layout in a container.
  • Resizes each region when the container is resized.

Here is an example program I wrote to test the BorderLayout:

/**
 * BorderLayoutTest.java
 * Copyright (c) 2002 by Dr. Herong Yang, http://www.herongyang.com/
 */
import java.awt.*;
import javax.swing.*;
public class BorderLayoutTest {
   public static void main(String[] a) {
      JFrame myFrame = new JFrame("FlowLayout Test");
      myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Container myPane = myFrame.getContentPane();
      
      myPane.setLayout(new BorderLayout());
      myPane.add(new JButton("North"), BorderLayout.NORTH);
      myPane.add(new JButton("South"), BorderLayout.SOUTH);
      myPane.add(new JButton("East"), BorderLayout.EAST);
      myPane.add(new JButton("West"), BorderLayout.WEST);
      myPane.add(new JButton(new ImageIcon("java.gif")), 
         BorderLayout.CENTER);
      myFrame.pack();
      myFrame.setVisible(true);
   }
}

If you run this example, you will get:
Layout - BorderLayout

Sample programs listed in this section have been tested with JDK 1.3.1, 1.4.1, 1.5.0, and 1.6.0.

Last update: 2009.

Sections in This Chapter

What Is Layout?

java.awt.BorderLayout - Border Layout

java.awt.FlowLayout - Flow Layout

java.awt.BoxLayout - Box Layout

java.awt.GridLayout - Grid Layout

java.awt.GridBagLayout - Grid Bag Layout

Dr. Herong Yang, updated in 2009
java.awt.BorderLayout - Border Layout