BorderLayout in java

BorderLayout

  • It used to implement a common layout style for top level windows
  • It has 4 fixed width components at edges (NORTH, SOUTH, EAST, WEST) & 1 large in the CENTER
  • Components can be added to any region by specifying the region as BorderLayout.EAST, etc
Example
import java.awt.*;
import javax.swing.*;
public class test extends JFrame
{
test()
{
Container c= getContentPane();
BorderLayout obj = new BorderLayout(10,10);
c.setLayout(obj);
JButton b1=new JButton(“White”);
JButton b2=new JButton(“Orange”);
JButton b3=new JButton(“Green”);
JButton b4=new JButton(“Red”);
JButton b5=new JButton(“Blue”);
c.add(“North”,b1);
c.add(“East”,b2);
c.add(“South”,b3);
c.add(“West”,b4);
c.add(“Center”,b5);
}
public static void main(String[] args)
{
test t = new test();
t.setSize(400,400);
t.setTitle(“Border Layout”);
t.setVisible(true);
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Output:

Untitled

Leave a Reply

Your email address will not be published. Required fields are marked *