The BorderLayout layout manager divides the container into five regions that are named geographically: Top (north), lower (south), left (west), right (east) and central (center). North corresponds to the top of the container. You can add only one component per region to a container controlled by a BorderLayout layout manager. However, this limitation can be overcome by adding a container with multiple components (such as Panel). Components laid out by this layout manager normally do not get to have their preferred size.
The components added to the NORTH or SOUTH regions will get its width equal to the width of the container while maintaining their preferred height. The components added to the EAST or WEST regions will get its height equal to the height of the container minus any components in NORTH or SOUTH while maintaining their preferred width. The component that are added to the CENTER will fill up the rest of the space in horizontal and vertical dimensions. BorderLayout is the default layout manager for a JApplet, JFrame, JDialog and JWindow.
The java.awt.BorderLayout class contain the following constructors and methods that can use to customize this manager:
Method | Description |
BorderLayout () | Creates a new layout manager without spacing between regions. |
BorderLayout (int, int) | Creates a new layout manager with the spacing horizontal and vertical specified. |
getHgap () | Gets the horizontal spacing. |
getVgap () | Gets the vertical spacing. |
setHgap (int) | Specifies the horizontal spacing. |
setVgap (int) | Specifies the vertical spacing. |
import java.awt.*;
class BorderLayoutExample extends Frame
{
BorderLayoutExample()
{
setLayout(new BorderLayout());
add(new Button("NORTH"),BorderLayout.NORTH);
add(new Button("SOUTH"),BorderLayout.SOUTH);
add(new Button("EAST"),BorderLayout.EAST);
add(new Button("WEST"),BorderLayout.WEST);
add(new Button("CENTER"),BorderLayout.CENTER);
}
}
class BorderLayoutJavaExample
{
public static void main(String args[])
{
BorderLayoutExample frame = new BorderLayoutExample();
frame.setTitle("BorderLayout in Java Example");
frame.setSize(400,150);
frame.setVisible(true);
}
}