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 representation of the state of border layout.
- getLayoutAlignmentX(Container parent): Returns the layout alignment along the X-axis.
- getLayoutAlignmentY(Container parent): It will returns 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 program to illustrate the BorderLayoutimportjava.awt.*;importjava.awt.event.*;importjavax.swing.*;// class extends JFrameclassBoderLayoutDemoextendsJFrame {BoderLayoutDemo(){// Creating Object of Jpanel classJPanel pa =newJPanel();// set the layoutpa.setLayout(newBorderLayout());// add a new JButton with name "wel" and it is// lie top of the containerpa.add(newJButton("WelCome"), BorderLayout.NORTH);// add a new JButton with name "come" and it is// lie buttom of the containerpa.add(newJButton("Geeks"), BorderLayout.SOUTH);// add a new JButton with name "Layout" and it is// lie left of the containerpa.add(newJButton("Layout"), BorderLayout.EAST);// add a new JButton with name "Border" and it is// lie right of the containerpa.add(newJButton("Border"), BorderLayout.WEST);// add a new JButton with name "hello everybody" and it is// lie center of the containerpa.add(newJButton("GeeksforGeeks"), BorderLayout.CENTER);// add the pa object which refer to the Jpaneladd(pa);// Function to close the operation of JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// Function to set size of JFrame.setSize(300,300);// Function to set visible status of JFrame.setVisible(true);}}classMainFrame {// Driver codepublicstaticvoidmain(String[] args){// calling the constructornewBoderLayoutDemo();}}chevron_rightfilter_none - 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 program to illustrate the BorderLayoutimportjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjava.awt.BorderLayout;importjava.awt.Frame;importjava.awt.Button;importjava.awt.Color;// class extends JFramepublicclassBorderDemoextendsJFrame {// Constructor of BorderDemo class.publicBorderDemo(){// set the layoutsetLayout(newBorderLayout());// set the backgroundsetBackground(Color.red);// creates Button (btn1)Button btn1 =newButton("Geeks");// creates Button (btn2)Button btn2 =newButton("GFG");// creates Button (btn3)Button btn3 =newButton("Sudo Placement");// creates Button (btn4)Button btn4 =newButton("GeeksforGeeks");// creates Button (btn5)Button btn5 =newButton("Java");// Adding JButton "btn1" on JFrame.add(btn1,"North");// Adding JButton "btn2" on JFrame.add(btn2,"South");// Adding JButton "btn3" on JFrame.add(btn3,"East");// Adding JButton "btn4" on JFrame.add(btn4,"West");// Adding JButton "btn5" on JFrame.add(btn5,"Center");// function to set the titlesetTitle("Learning a Border Layout");// Function to set size of JFrame.setSize(350,300);// Function to set visible status of JFramesetVisible(true);}// Main Methodpublicstaticvoidmain(String args[]){// calling the constructornewBorderDemo();}}chevron_rightfilter_noneOutput:
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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- In Java, Can we call the main() method of a class from another class?
- Difference between Abstract Class and Concrete Class in Java
- Using predefined class name as Class or Variable name in Java
- Java.lang.Class class in Java | Set 2
- Java.lang.Class class in Java | Set 1
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java.util.concurrent.Phaser class in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.util.concurrent.Semaphore class in Java
- Java.awt.image.RescaleOp Class in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



