Border Layout in Java With Examples

Border Layout With Examples:


logo

Table of Contents

a) Introduction to Border Layout
b) Different types of Constructor
c) Source Code and Output of each Constructor

****************************

Introduction:

Border Layout is the default layout of the frame/container. It arranges the components according to five different regions, they are; east, west, center, north and south. In each region only one component can be added. And the region is also named as EAST,  WEST, NORTH, SOUTH and CENTER.

The Border Layout also has two constructors, they are;

    a) BorderLayout()
    b) BorderLayout (int horizontal_gap, int vertical_gap)

A) BorderLayout(): It is the first constructor which creates the layout with no gap between the components.  It is a default constructor which is used in frame when we do not set any layout.

For example;

// setting the layout to the frame
setLayout(new BorderLayout());
// while we add the components to the frame
add(component_name, BorderLayout.SOUTH);


Let’s see the source code and output now;
/*
A  SWING GUI TO DEMONSTRSTE THE BORDER LAYOUT
 */
/**
 *
 * @author AnkitPC
 */
//importing the required packages
import javax.swing.*; //for components
import java.awt.*; //for layout
public class BorderLayoutDemo {
    JFrame f;
    JButton b1,b2,b3,b4,b5;
    //constructor
    BorderLayoutDemo(){
        f=new JFrame("Border Layout Demo");
        f.setSize(400,300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        //set the layout
        f.setLayout(new BorderLayout()); //first constructor
       
        b1=new JButton("NORTH");
        b2=new JButton("SOUTH");
        b3=new JButton("EAST");
        b4=new JButton("WEST");
        b5=new JButton("CENTER");
       
        //add components to the frame
        f.add(b1,BorderLayout.NORTH);
        f.add(b2,BorderLayout.SOUTH);
        f.add(b3,BorderLayout.EAST);
        f.add(b4,BorderLayout.WEST);
        f.add(b5,BorderLayout.CENTER);
       
        //set the frame visibility to true
        f.setVisible(true);
    }
   
    //main method
    public static void main(String[] args) {
        //call the constructor
        new BorderLayoutDemo();
    }
   
}

The given source code demonstrate the use of first constructor of Border Layout. As we know that it arranges the components in five different regions i.e EAST, WEST, NORTH. SOUTH and CENTER. It does not provide the horizontal and vertical gap between the components.

Output:



B) BorderLayout(int horizontal_gap,  int vertical_gap): It is the second constructor where you can specify the horizontal and vertical gap between the components. Integer values are passed to its parameter.

For example;

//setting the layout to the frame
setLayout(new BorderLayout(10,10);
// while we add the components to the frame
add(component_name, BorderLayout.SOUTH);

Let’s see the source code and output now;
/*
A  SWING GUI TO DEMONSTRSTE THE BORDER LAYOUT
 */
/**
 *
 * @author AnkitPC
 */
//importing the required packages
import javax.swing.*; //for components
import java.awt.*; //for layout
public class BorderLayoutDemo {
    JFrame f1;
    JButton b11,b12,b13,b14,b15;
    //constructor
    BorderLayoutDemo(){
        f1=new JFrame("Border Layout Demo");
        f1.setSize(400,300);
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        //set the layout
        f1.setLayout(new BorderLayout(10,10)); //second constructor
       
        b11=new JButton("NORTH");
        b12=new JButton("SOUTH");
        b13=new JButton("EAST");
        b14=new JButton("WEST");
        b15=new JButton("CENTER");
       
        //add components to the frame
        f1.add(b11,BorderLayout.NORTH);
        f1.add(b12,BorderLayout.SOUTH);
        f1.add(b13,BorderLayout.EAST);
        f1.add(b14,BorderLayout.WEST);
        f1.add(b15,BorderLayout.CENTER);
       
        //set the frame visibility to true
        f.setVisible(true);
    }
   
    //main method
    public static void main(String[] args) {
        //call the constructor
        new BorderLayoutDemo();
    }
   
}

The given source code demonstrate the use of second constructor of Border Layout. As we know that it arranges the components in five different regions i.e EAST, WEST, NORTH. SOUTH and CENTER. We can specifically provide the horizontal and vertical gap between the components.

Output:





























****************

Post a Comment

0 Comments