BORDER LAYOUT
The BorderLayout is used to arrange the components in five regions: north, south, east, west, and center. Each region (area) may contain one component only. It is the default layout of a frame or window. The four sides are referenced to as north, south, east, and west. The middle area is called the center. North, south, east, and west are narrow fixed-width regions whereas the center is a large region.
Commonly used Constructors
BorderLayout(); // creates a border layout but with no gaps between the components.
BorderLayout(int horz_gap, int vert_gap); // creates a border layout with the horizontal and vertical gaps between the components.
Constants for Regions
BorderLayout.CENTER
BorderLayout.EAST
BorderLayout.NORTH
Border Layout SOUTH,
BorderLayout. WEST.
When adding components, we will use these constants with the following form of add method.
void add(Component comp, object_region)
Example of BorderLayout() constrcutor
import java.awt.*;
public class BorderLayout Demo extends Frame
{
Button b1,b2, b3, b4,b5;
BorderLayout Demo ()
{
setSize(500, 250);
setTitle("Border Layout Demo"); setLayout (new BorderLayout());
b1=new Button("Button1");
b2=new Button("Button2");
b3=new Button ("Button3"); b4=new Button ("Button4");
b5-new Button ("Button5");
add (b1, BorderLayout. NORTH);
add (b2, BorderLayout.SOUTH);
add (b3, BorderLayout.EAST);
add (b4, Border Layout.WEST);
add (b5, BorderLayout.CENTER);
setVisible(true);
}
public static void main(String args[])
{
Border Layout Demo frame=new Border Layout Demo();
}
}
OR,Java BorderLayout
The BorderLayout is used to arrange the components in five regions: north, south, east, west, and center. Each region (area) may contain one component only. It is the default layout of a frame or window. The BorderLayout provides five constants for each region:
public static final int NORTH
public static final int SOUTH
public static final int EAST
public static final int WEST
public static final int CENTER
Constructors of BorderLayout class:
BorderLayout(): creates a border layout but with no gaps between the components.
BorderLayout(int hgap, int vgap): creates a border layout with the given horizontal and vertical gaps between the components.
Example of BorderLayout class: Using BorderLayout() constructor
FileName: Border.java
import java.awt.*;
import javax.swing.*;
public class Border
{
JFrame f;
Border()
{
f = new JFrame();
// creating buttons
JButton b1 = new JButton("NORTH");; // the button will be labeled as NORTH
JButton b2 = new JButton("SOUTH");; // the button will be labeled as SOUTH
JButton b3 = new JButton("EAST");; // the button will be labeled as EAST
JButton b4 = new JButton("WEST");; // the button will be labeled as WEST
JButton b5 = new JButton("CENTER");; // the button will be labeled as CENTER
f.add(b1, BorderLayout.NORTH); // b1 will be placed in the North Direction
f.add(b2, BorderLayout.SOUTH); // b2 will be placed in the South Direction
f.add(b3, BorderLayout.EAST); // b2 will be placed in the East Direction
f.add(b4, BorderLayout.WEST); // b2 will be placed in the West Direction
f.add(b5, BorderLayout.CENTER); // b2 will be placed in the Center
f.setSize(300, 300);
f.setVisible(true);
}
public static void main(String[] args) {
new Border();
}
}
Output:
Example of BorderLayout class: Using BorderLayout(int hgap, int vgap) constructor
The following example inserts horizontal and vertical gaps between buttons using the parameterized constructor BorderLayout(int hgap, int gap)
FileName: BorderLayoutExample.java
// import statement
import java.awt.*;
import javax.swing.*;
public class BorderLayoutExample
{
JFrame jframe;
// constructor
BorderLayoutExample()
{
// creating a Frame
jframe = new JFrame();
// create buttons
JButton btn1 = new JButton("NORTH");
JButton btn2 = new JButton("SOUTH");
JButton btn3 = new JButton("EAST");
JButton btn4 = new JButton("WEST");
JButton btn5 = new JButton("CENTER");
// creating an object of the BorderLayout class using
// the parameterized constructor where the horizontal gap is 20
// and vertical gap is 15. The gap will be evident when buttons are placed
// in the frame
jframe.setLayout(new BorderLayout(20, 15));
jframe.add(btn1, BorderLayout.NORTH);
jframe.add(btn2, BorderLayout.SOUTH);
jframe.add(btn3, BorderLayout.EAST);
jframe.add(btn4, BorderLayout.WEST);
jframe.add(btn5, BorderLayout.CENTER);
jframe.setSize(300,300);
jframe.setVisible(true);
}
// main method
public static void main(String argvs[])
{
new BorderLayoutExample();
}
}

Comments
Post a Comment