Open In App

Java AWT | BorderLayout Class

Last Updated : 06 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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: 

  1. BorderLayout(): It will construct a new borderlayout with no gaps between the components.
  2. BorderLayout(int, int): It will constructs a border layout with the specified gaps between the components.

Commonly Used Methods:  

  1. toString(): Returns a string which is the representation of the state of border layout.
  2. getLayoutAlignmentX(Container parent): Returns the layout alignment along the X-axis.
  3. getLayoutAlignmentY(Container parent): It will return the layout alignment along the Y-axis.
  4. removeLayoutComponent(Component comp): This method is used to remove the specified component from the borderlayout.
  5. getVgap(): Return the vertical gap between the components.
  6. getHgap(): Returns the Horizontal gap between the components.
  7. setHgap(int hgap): It is used to set the horizontal gap between the components.
  8. 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




// Java program to illustrate the BorderLayout
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
// class extends JFrame
class BoderLayoutDemo extends JFrame {
 
    BoderLayoutDemo()
    {
         
        // Creating Object of Jpanel class
        JPanel pa = new JPanel();
 
        // set the layout
        pa.setLayout(new BorderLayout());
 
        // add a new JButton with name "wel" and it is
        // lie top of the container
        pa.add(new JButton("WelCome"), BorderLayout.NORTH);
 
        // add a new JButton with name "come" and it is
        // lie button of the container
        pa.add(new JButton("Geeks"), BorderLayout.SOUTH);
 
        // add a new JButton with name "Layout" and it is
        // lie left of the container
        pa.add(new JButton("Layout"), BorderLayout.EAST);
 
        // add a new JButton with name "Border" and it is
        // lie right of the container
        pa.add(new JButton("Border"), BorderLayout.WEST);
 
        // add a new JButton with name "hello everybody" and it is
        // lie center of the container
        pa.add(new JButton("GeeksforGeeks"), BorderLayout.CENTER);
 
        // add the pa object which refer to the Jpanel
        add(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);
    }
}
 
class MainFrame {
 
    // Driver code
    public static void main(String[] args)
    {
         
        // calling the constructor
        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




// Java program to illustrate the BorderLayout
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;
  
// class extends JFrame
public class BorderDemo extends JFrame {
  
    // Constructor of BorderDemo class.
    public BorderDemo()
    {
  
        // set the layout
        setLayout(new BorderLayout());
  
        // set the background
        setBackground(Color.red);
  
        // creates Button (btn1)
        Button btn1 = new Button("Geeks");
  
        // creates Button (btn2)
        Button btn2 = new Button("GFG");
  
        // creates Button (btn3)
        Button btn3 = new Button("Sudo Placement");
  
        // creates Button (btn4)
        Button btn4 = new Button("GeeksforGeeks");
  
        // creates Button (btn5)
        Button btn5 = new Button("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 title
        setTitle("Learning a Border Layout");
  
        // Function to set size of JFrame.
        setSize(350, 300);
  
        // Function to set visible status of JFrame
        setVisible(true);
    }
  
    // Main Method
    public static void main(String args[])
    {
  
        // calling the constructor
        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
 



Similar Reads

Java.awt.image.RescaleOp Class in Java with Examples
RescaleOp is a class in java.awt.image package which implements BufferedImageOp and RasterOp interface. This class performs a pixel-by-pixel rescaling of the data in the source image by multiplying the sample values for each pixel by a scale factor and then adding an offset. The scaled sample values are clipped to the minimum/maximum representation
5 min read
Java AWT vs Java Swing vs Java FX
Java's UI frameworks include Java AWT, Java Swing, and JavaFX. This plays a very important role in creating the user experience of Java applications. These frameworks provide a range of tools and components for creating graphical user interfaces (GUIs) that are not only functional but also visually appealing. As a Java developer, selecting the righ
11 min read
Java AWT | GridLayout Class
GridLayout class represents a layout manager with a specified number of rows and columns in a rectangular grid. The GridLayout container is divided into an equal-sized of rectangles, and one of the components is placed in each rectangle. Every rectangle cell has the same size therefore, they contain a component, which fills the entire cell. When th
5 min read
Java AWT | Cursor class with examples
Cursor class is a part of Java AWT package and it is used to create custom cursors or inherit system or predefined cursors. Cursor class is mainly used to encapsulate the bitmap representation of the mouse cursor. Constructor of cursor class are : Cursor(int t) : Creates a cursor with specified class Cursor(String name) : Creates a custom cursor wi
4 min read
Java AWT | Choice Class
Choice class is part of Java Abstract Window Toolkit(AWT). The Choice class presents a pop- up menu for the user, the user may select an item from the popup menu. The selected item appears on the top. The Choice class inherits the Component. Constructor for the Choice class Choice() : creates an new empty choice menu . Different Methods for the Cho
5 min read
Java AWT | Desktop Class
The Desktop class is a part of Java AWT package. This class is used to launch associated applications registered on the native desktop to handle a URI or a file. Important Points About Desktop Class : It can open a default web browser showing a specific URI It can launch default mail client with optional mailto URI It can launch a registered applic
5 min read
Java AWT | Color Class
The Color class is a part of Java Abstract Window Toolkit(AWT) package. The Color class creates color by using the given RGBA values where RGBA stands for RED, GREEN, BLUE, ALPHA or using HSB value where HSB stands for HUE, SATURATION, BRIcomponents. The value for individual components RGBA ranges from 0 to 255 or 0.0 to 0.1. The value of alpha det
9 min read
Java AWT | Dimension Class
Dimension class is a part of Java AWT. It contains the height and width of a component in an integer as well as double precision. The use of Dimension class is that many functions of Java AWT and Swing return dimension object. Constructors of the Dimension class Dimension() : It will create a new Object with height and width set to zero.Dimension(D
4 min read
Java AWT | Canvas Class
Canvas class is a part of Java AWT. Canvas is a blank rectangular area where the user can draw or trap input from the user. Canvas class inherits the Component class.Constructor of the Canvas class are : Canvas(): Creates a new blank canvas.Canvas(GraphicsConfiguration c): Creates a new canvas with a specified graphics configuration. Commonly used
3 min read
Java AWT | CardLayout Class
The CardLayout class manages the components in such a way that only one component is visible at a time. It treats each component as a card in the container. Only one card is visible at a time, and the container acts as a stack of cards. The first component added to a CardLayout object is the visible component when the container is first displayed.
6 min read
Article Tags :
Practice Tags :