Java AWT | BorderLayout Class
BorderLayout is the default layout for the window objects such as JFrame, JWindow, JDialog, JInternalFrame etc. BorderLayout arranges the components in the five regions. Four sides are referred to as north, south, east, and west. The middle part is called the center. Each region can contain only one component and is identified by a corresponding constant as NORTH, SOUTH, EAST, WEST, and CENTER.
Constructors:
- BorderLayout(): It will construct a new borderlayout with no gaps between the components.
- BorderLayout(int, int): It will constructs a border layout with the specified gaps between the components.
Commonly Used Methods:
- toString(): Returns a string which is the representation of the state of border layout.
- getLayoutAlignmentX(Container parent): Returns the layout alignment along the X-axis.
- getLayoutAlignmentY(Container parent): It will return the layout alignment along the Y-axis.
- removeLayoutComponent(Component comp): This method is used to remove the specified component from the borderlayout.
- getVgap(): Return the vertical gap between the components.
- getHgap(): Returns the Horizontal gap between the components.
- setHgap(int hgap): It is used to set the horizontal gap between the components.
- setVgap(int vgap): It is used to set the vertical gap between the components.
Below Programs will illustrate the BorderLayout class:
- Program 1: The following program creates JButton components in a JFrame, whose instance class is “BorderLayoutDemo”. We create 5 JButton and then add them to the JFrame by using add() method. We will set the size and visibility of the frame by using setSize() and setVisible() method respectively. The layout is set by using the setLayout() method.
Java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class BoderLayoutDemo extends JFrame {
BoderLayoutDemo()
{
JPanel pa = new JPanel();
pa.setLayout( new BorderLayout());
pa.add( new JButton( "WelCome" ), BorderLayout.NORTH);
pa.add( new JButton( "Geeks" ), BorderLayout.SOUTH);
pa.add( new JButton( "Layout" ), BorderLayout.EAST);
pa.add( new JButton( "Border" ), BorderLayout.WEST);
pa.add( new JButton( "GeeksforGeeks" ), BorderLayout.CENTER);
add(pa);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize( 300 , 300 );
setVisible( true );
}
}
class MainFrame {
public static void main(String[] args)
{
new BoderLayoutDemo();
}
}
|
Output:
- Program 2: This program will show how to pass the arguments in BorderLayout. Set the background color by using setBackground() method. We create 5 JButton components named “btn1“, “btn2“, “btn3“, “btn4“, “btn5“, and then add them to the JFrame by using add() method. We set the title, size, and visibility of the frame by using setTitle(), setSize() and setVisible() methods respectively. The layout is set by the method setLayout().
Java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Button;
import java.awt.Color;
public class BorderDemo extends JFrame {
public BorderDemo()
{
setLayout( new BorderLayout());
setBackground(Color.red);
Button btn1 = new Button( "Geeks" );
Button btn2 = new Button( "GFG" );
Button btn3 = new Button( "Sudo Placement" );
Button btn4 = new Button( "GeeksforGeeks" );
Button btn5 = new Button( "Java" );
add(btn1, "North" );
add(btn2, "South" );
add(btn3, "East" );
add(btn4, "West" );
add(btn5, "Center" );
setTitle( "Learning a Border Layout" );
setSize( 350 , 300 );
setVisible( true );
}
public static void main(String args[])
{
new BorderDemo();
}
}
|
Output:
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html
Last Updated :
06 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...