Use BorderLayout to arrange components around a central area. It works well for a window with a header at the top, a status bar at the bottom, and content in the middle.
In this example, you will place a button in each of the five regions and see how they resize with the window.
Source code
Save the following code as BorderLayoutExample.java:
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class BorderLayoutExample {
void main() {
SwingUtilities.invokeLater(this::showWindow);
}
private void showWindow() {
JPanel panel = new JPanel(new BorderLayout(8, 8));
panel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
panel.add(new JButton("North"), BorderLayout.NORTH);
panel.add(new JButton("South"), BorderLayout.SOUTH);
panel.add(new JButton("West"), BorderLayout.WEST);
panel.add(new JButton("East"), BorderLayout.EAST);
panel.add(new JButton("Center"), BorderLayout.CENTER);
JFrame frame = new JFrame("BorderLayout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.setSize(480, 260);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
What the window looks like

Explanation
new BorderLayout(8, 8) sets an 8-pixel horizontal gap and an 8-pixel vertical gap between neighboring components. The empty border adds 12 pixels of padding around the panel.
The second argument to panel.add() selects the region:
panel.add(new JButton("Center"), BorderLayout.CENTER);
Each region uses the available space differently:
| Region | How it behaves when the window grows |
|---|---|
NORTH and SOUTH |
Stretch horizontally and keep their preferred height |
WEST and EAST |
Stretch vertically between the top and bottom regions and keep their preferred width |
CENTER |
Fills the space left over in both directions |
Each region holds one component. To place several controls in the same region, put them in a JPanel and add that panel to the region. For example, a header panel might contain a title and several buttons.
Run the example
javac BorderLayoutExample.java
java BorderLayoutExample
Drag the right edge of the window outward. The North, Center, and South buttons grow wider, while West and East keep their widths. Then drag the bottom edge downward: West, Center, and East grow taller.