swing Layout management Border layout


import static java.awt.BorderLayout.*;
import javax.swing.*;
import java.awt.BorderLayout;

JPanel root = new JPanel(new BorderLayout());

root.add(new JButton("East"), EAST);
root.add(new JButton("West"), WEST);
root.add(new JButton("North"), NORTH);
root.add(new JButton("South"), SOUTH);
root.add(new JButton("Center"), CENTER);

JFrame frame = new JFrame();

Border layout is one of the simplest layout managers. The way to use a layout manager is to set the manager of a JPanel.

Border Layout slots follow the following rules:

  • North & South: preferred height
  • East & West: preferred width
  • Center: maximum remaining space

In BorderLayout slots can also be empty. The layout manager will automatically compensate for any empty spaces, resizing when needed.

Here is what this example looks like:

enter image description here