BorderLayout is the default layout manager for the JFrame.
It splits the container into five distinct sections where each section can
hold only one component. If you want a certain section to have more than
one component then place them into a
JPanel and add the
to the the
Five Component Areas
The five sections of the
BorderLayout are known as
They are arranged in a similar fashion as the points of a compass.
Although it might be easier to think of the
having three rows. The top row is the
NORTH section. The
bottom row is the
SOUTH section. The middle row is made up
of three sections -
In terms of section size, the
CENTER section will always
try and be the biggest. If a
JFrame is made bigger or
smaller typically it's the
CENTER section that will enlarge
or shrink first. The other sections will hold station around the
To add a component to a
BorderLayout you just need to
specify which section you want it placed:
JFrame guiFrame= new JFrame(); JPanel optionPanel = new JPanel(); guiFrame.add(optionPanel, BorderLayout.NORTH);
Remember that the
JFrame uses the
by default so the layout manager does not have to be explicitly set to use
BorderLayout layout manager. The
field values determine the section to be used:
CENTER- middle of the container.
EAST- the right side of the middle.
WEST- the left side of the middle.
NORTH- the top of the container.
SOUTH- the bottom of the container.
PAGE_START- the equivalent to
NORTHfor top-down, left-to-right component orientation.
PAGE_END- the equivalent to
SOUTHfor top-down, left-to-right component orientation.
LINE_START- the equivalent to
WESTfor top-down, left-to-right component orientation.
LINE-END- the equivalent to
EASTfor top-down, left-to-right component orientation.
If a section is not specified then the
CENTER section will
be used. Remember that if a component is already in a section it will be
displaced if another component is added to that section.
Note: The BorderLayout field values are strings. This means you can use String literals to set the sections:
However, it's not recommended to use a String literal as it can lead to
easy mistakes. The compiler we be unable to spot a misspelled String and
as always it is case sensitive.
The component spacing between the different sections can be set using one
BorderLayout bord = new BorderLayout(5,5);
The individual horizontal and vertical spacing can be set using the
int values for the horizontal and vertical spacing
can be returned by using the
For an example program that shows the different sections of the
BorderLayout have a look at the BorderLayout
example program. The program allows a user to specify the horizontal
and vertical spacing before showing five labels, each placed in one of the