Using a BorderLayout Manager : BorderLayout « Swing « Java Tutorial






Using a BorderLayout Manager
import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.EtchedBorder;

public class TryBorderLayout {
  public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Border Layout");
    aWindow.setBounds(30, 30, 300, 300); // Size
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    BorderLayout border = new BorderLayout(); // Create a layout manager
    Container content = aWindow.getContentPane(); // Get the content pane
    content.setLayout(border); // Set the container layout mgr
    EtchedBorder edge = new EtchedBorder(EtchedBorder.RAISED); // Button border
    // Now add five JButton components and set their borders
    JButton button;
    content.add(button = new JButton("EAST"), BorderLayout.EAST);
    button.setBorder(edge);
    content.add(button = new JButton("WEST"), BorderLayout.WEST);
    button.setBorder(edge);
    content.add(button = new JButton("NORTH"), BorderLayout.NORTH);
    button.setBorder(edge);
    content.add(button = new JButton("SOUTH"), BorderLayout.SOUTH);
    button.setBorder(edge);
    content.add(button = new JButton("CENTER"), BorderLayout.CENTER);
    button.setBorder(edge);
    aWindow.setVisible(true); // Display the window
  }
}








14.86.BorderLayout
14.86.1.What is the BorderLayoutWhat is the BorderLayout
14.86.2.A BorderLayout divides the space into five regions: North, West, South, East and Centre.
14.86.3.Using a BorderLayout ManagerUsing a BorderLayout Manager
14.86.4.A typical usage of a border layout manager.A typical usage of a border layout manager.
14.86.5.Place multiple components into one of the regions of a BorderLayout-managed containerPlace multiple components into one of the regions of a BorderLayout-managed container
14.86.6.Combining BorderLayout and GridLayout ManagersCombining BorderLayout and GridLayout Managers