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

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.

Table of Contents

 About This Java Swing Tutorial Book

 Introduction of Java Swing Package

 Graphics Environment of the Local System

 JFrame - Main Frame Class

 JLabel - Swing Label Class

 JButton - Swing Button Class

 JRadioButton - Swing Radio Button Class

 JTextField - Swing Text Field Class

 Menu Bar, Menus, Menu Items and Listeners

 Creating Internal Frames inside the Main Frame

Layout of Components in a Container

 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

 LookAndFeel and UIManager

 Option Dialog Boxes

 JEditorPane - The Editor Pane Class

 References

 Printable Copy - PDF Version

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