Java


Java Tutorial


Admission Enquiry Form

  

Java BorderLayout

Java BorderLayout

The Border layout, which is created by using the BorderLayout class,divide a container into five sections: north,south,east,west and center.
Under border layout, the components in the four sides will take up as much space as they need, the center gets whatever space is left over.

Constructors of BorderLayout class

Type of Constructor Constructor Description
BorderLayout() It creates a border layout but with no gaps between the components.
BorderLayout(int hgap, int vgap) It creates a border layout with the given horizontal and vertical gaps between the components.



Example of BorderLayout

import javax.swing.*;
import java.awt.*;

class DemoBorder extends JFrame
{
JButton b1,b2,b3,b4,b5;
DemoBorder()
{
b1=new JButton("NORTH");
b2=new JButton("SOUTH");
b3=new JButton("EAST");
b4=new JButton("WEST");
b5=new JButton("CENTER");
add(b1, BorderLayout.NORTH);
add(b2,BorderLayout.SOUTH);
add(b3,BorderLayout.EAST);
add(b4,BorderLayout.WEST);
add(b5,BorderLayout.CENTER);
setTitle("BorderLayout Demo");
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class BorderLayoutEx
{
public static void main(String[] args)
{
DemoBorder border=new DemoBorder();
}
}


Output

The code above will display :