Java Reference
In-Depth Information
BorderLayout
The
BorderLayout
manager allows you to place items in specific regions. This layout
manager divides the container into five regions:
NORTH
,
SOUTH
,
EAST
,
WEST
, and
CENTER
.
Components placed in the
NORTH
and
SOUTH
regions extend horizontally, completely
spanning one edge to the other.
EAST
and
WEST
components extend vertically between
the components in the
NORTH
and
SOUTH
regions. The component placed at the
CENTER
expands to occupy any unused regions.
In the following example, we create five components and place them in the content pane
using the
BorderLayout
manager:
//Program to illustrate BorderLayout
import
javax.swing.*;
import
java.awt.*;
public class
BorderLayoutExample
extends
JFrame
{
private static int
WIDTH = 350;
private static int
HEIGHT = 300;
//GUI components
private
JLabel labelJL;
private
JTextField textFieldTF;
private
JButton buttonJB;
private
JCheckBox checkboxCB;
private
JRadioButton radioButtonRB;
private
JTextArea textAreaTA;
private
BorderLayout borderLayoutMgr;
public
BorderLayoutExample()
{
setTitle("BorderLayout Manager");
Container pane = getContentPane();
setSize(WIDTH, HEIGHT);
1
2
borderLayoutMgr =
new
BorderLayout(10, 10);
pane.setLayout(borderLayoutMgr);
labelJL =
new
JLabel("North Component");
textAreaTA =
new
JTextArea(10, 20);
textAreaTA.setText("South Component.\n");
textAreaTA.append(
"Use the mouse to change the size of the window.");
buttonJB =
new
JButton("West Component");
checkboxCB =
new
JCheckBox("East Component");
radioButtonRB =
new
JRadioButton("Center Component");
Search WWH ::
Custom Search