BorderLayout in Java is also known as Layout Manager. Its work is to control the layout, i.e., the size and position of the components. Also, the default BorderLayout in Java has been used to layout a container (north, east, south, etc.), i.e., arrange and resize its components.
BorderLayout class is used by window objects such as JFrame, JWindow, and JDialog to display GUI(Graphic User Interface) components. BorderLayout is used to arrange and resize the components of a container.
The border layout divides these elements into five distinct regions. Four of the five regions or areas are referred to as north, south, east, and west, with the middle region referred to as the center. Each region can only have one component. The border layout includes a set of constants for positioning components.
In addition to the constants(NORTH, SOUTH, EAST, WEST) listed above(in the figure), BorderLayout provides additional positioning constants such as PAGE_START, PAGE_END, and so on.
Class Declaration
Declaration for java.awt.BorderLayout class −
public class BorderLayout
extends Object
implements LayoutManager2, Serializable
Field
Following are the fields for java.awt.BorderLayout class:
Example
By using a constructor name BorderLayout()
// BorderLayout In Java
import java.awt.*;
import javax.swing.*;
public class Border_lines
{
JFrame j;
Border()
{
j = new JFrame();
// Creating buttons
JButton a1 = new JButton("NORTH");
JButton a2 = new JButton("SOUTH");
JButton a3 = new JButton("EAST");
JButton a4 = new JButton("WEST");
JButton a5 = new JButton("CENTER(Welcome to Coding Ninjas)");
j.add(a1, BorderLayout.NORTH);
j.add(a2, BorderLayout.SOUTH);
j.add(a3, BorderLayout.EAST);
j.add(a4, BorderLayout.WEST);
j.add(a5, BorderLayout.CENTER);
j.setSize(350, 350);
j.setVisible(true);
}
public static void main(String[] args)
{
new Border();
}
}
You can also try this code with Online Java Compiler
Following classes that represent the layout managers:
java.awt.BorderLayout
java.awt.FlowLayout
java.awt.GridLayout
java.awt.CardLayout
java.awt.GridBagLayout
javax.swing.BoxLayout etc.
Constructors
BorderLayout() creates a new border layout with no gaps between the components.
BorderLayout (int hgap, int vgap) creates a border layout with the specified spacing between components. Where, hgap is the horizontal gap and vgap is the vertical gap.
Methods of BorderLayout in Java
Methods
Description
int getVgap()
Returns the distance between the components' vertical axes.
void layout container(Container target)
This border layout is used to lay out the container argument.
The BorderLayout is used to organize the components into five distinct regions: north, south, east, west, and center. Each region (area) may only have one component. It is the standard frame or window layout.
What is the default layout in Java?
The FlowLayout is the default layout. It layout the components in a directional flow.
What is the grid layout in Java?
The GridLayout class is a layout manager that arranges the components of a container in a rectangular grid. The container is divided into equal-sized rectangles, with one component in each.
What are layouts in Java?
The arrangement of components within the container is referred to as layout. In other words, the components are placed in a specific location within the container. The Layout Manager handles the task of layout and the controls automatically.
What is the purpose of setLayout in Java?
The setLayout(...) method lets you specify the layout of the container, which is usually a JPanel, such as FlowLayout, BorderLayout, GridLayout, null layout, or whichever layout you want.
Conclusion
This blog covered BorderLayout in Java, where we also covered the constructors and methods of the layout manager. If you would like to learn more, check out these articles on the following to have a better understanding of java: