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:
Sample programs listed in this section have been tested with JDK 1.3.1, 1.4.1, 1.5.0, and 1.6.0.