Signup/Sign In

Layout Managers


In Java, Layout Managers is used for arranging the components in order. LayoutMananger is an interface which implements the classes of the layout manager.

Below are some of the class which are used for the representation of layout manager.


1. java.awt.BorderLayout

2. java.awt.FlowLayout

3. java.awt.GridLayout

4. java.awt.CardLayout

5. javax.swing.BoxLayout


Border Layout

BorderLayout is used, when we want to arrange the components in five regions. The five regions can be north, south, east, west and the centre. There are 5 types of constructor in Border Layout. They are as following:

1. public static final int NORTH

2. public static final int SOUTH

3. public static final int EAST

4. public static final int WEST

5. public static final int CENTER

Example:

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

public class BorderDemo1 
{  
JFrame frame;  
BorderDemo1()
{  
		frame=new JFrame();  

		JButton box1=new JButton("**NORTH**");;  
		JButton box2=new JButton("**SOUTH**");;  
		JButton box3=new JButton("**EAST**");;  
		JButton box4=new JButton("**WEST**");;  
		JButton box5=new JButton("**CENTER**");;  

		frame.add(box1,BorderLayout.NORTH);  
		frame.add(box2,BorderLayout.SOUTH);  
		frame.add(box3,BorderLayout.EAST);  
		frame.add(box4,BorderLayout.WEST);  
		frame.add(box5,BorderLayout.CENTER);  

		frame.setSize(400,400);  
		frame.setVisible(true);  
}  
public static void main(String[] args) 
{  
    new BorderDemo1();  
}  
} 
	

border-layout border-layout-example


Grid Layout

Grid Layout is used, when we want to arrange the components in a rectangular grid.

There are 3 types of constructor in Grid Layout. They are as following:

1. GridLayout()

2. GridLayout(int rows, int columns)

3. GridLayout(int rows, int columns, inthgap, int vgap)

Example:

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

public class GridDemo1{  
JFrame frame1;  
GridDemo1(){
    frame1=new JFrame();  

JButton box1=new JButton("*1*");  
JButton box2=new JButton("*2*");  
JButton box3=new JButton("*3*");  
JButton box4=new JButton("*4*");  
JButton box5=new JButton("*5*");  
JButton box6=new JButton("*6*");  
JButton box7=new JButton("*7*");  
JButton box8=new JButton("*8*");  
JButton box9=new JButton("*9*");  

    frame1.add(box1);
    frame1.add(box2);
    frame1.add(box3);
    frame1.add(box4);
    frame1.add(box5);  
    frame1.add(box6);
    frame1.add(box7);
    frame1.add(box8);
    frame1.add(box9);  

    frame1.setLayout(new GridLayout(3,3));   
    frame1.setSize(500,500);  
    frame1.setVisible(true);  
}  
public static void main(String[] args) {  
    new GridDemo1();  
}  
}
	

grid-layout grid-layout


Flow Layout

Flow Layout is used, when we want to arrange the components in a sequence one after another.

There are 3 types of constructor in the Flow Layout. They are as following:

1. FlowLayout()

2. FlowLayout(int align)

3. FlowLayout(int align, inthgap, intvgap)

Example:

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

public class FlowDemo1{  
JFrame frame1;  
FlowDemo1(){
    frame1=new JFrame();  

JButton box1=new JButton("1");  
JButton box2=new JButton("2");  
JButton box3=new JButton("3");  
JButton box4=new JButton("4");  
JButton box5=new JButton("5");  
JButton box6=new JButton("6"); 
JButton box7=new JButton("7"); 
JButton box8=new JButton("8"); 
JButton box9=new JButton("9"); 
JButton box10=new JButton("10");

    frame1.add(box1);
    frame1.add(box2);
    frame1.add(box3);
    frame1.add(box4);
    frame1.add(box5);  
    frame1.add(box6);
    frame1.add(box7);
    frame1.add(box8);
    frame1.add(box9);
    frame1.add(box10);
    frame1.setLayout(new FlowLayout(FlowLayout.LEFT));  

    frame1.setSize(400,400);  
    frame1.setVisible(true);  
}  
public static void main(String[] args) {  
    new FlowDemo1();  
}  
}
	

flow-layout flow-layout


Box Layout

Box Layout is used, when we want to arrange the components vertically or horizontally.

BoxLayout(Container c, int axis)is the only constructor in the Box Layout

Example:

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

public class BoxDemo1 extends Frame {  
	Button buttonBox[];  
	public BoxDemo1 () 
	{  
		buttonBox = new Button [2];  
		for (inti = 0;i<2;i++) 
		{  
			buttonBox[i] = new Button ("** Button " + (i + 1)+" **");  
			add (buttonBox[i]);  
		}  
	setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));  
	setSize(500,500);  
	setVisible(true);    
	}    
	
public static void main(String args[])
{  
	BoxDemo1 obj=new BoxDemo1();  
}  
} 
	

box-layout box-layout

Example:

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

public class BoxDemo1 extends Frame {  
	Button buttonBox[];  
	public BoxDemo1 () 
	{  
		buttonBox = new Button [2];  
		for (inti = 0;i<2;i++) 
		{  
			buttonBox[i] = new Button ("** Button " + (i + 1)+" **");  
			add (buttonBox[i]);  
		}  
	setLayout (new BoxLayout (this, BoxLayout.X_AXIS));  
	setSize(500,500);  
	setVisible(true);    
	}    
	
public static void main(String args[])
{  
	BoxDemo1 obj=new BoxDemo1();  
}  
} 
	

box-layout-2 box-layout-3


Card Layout

Card Layout is used, when we want to see only one component at a time.

There are 2 types of constructor in the Card Layout. They are as following:

1. CardLayout()

2. CardLayout(inthgap, intvgap)

Example:

	
import java.awt.*;  
import java.awt.event.*;  
import javax.swing.*;  
public class CardDemo1 extends JFrame implements ActionListener
{  
CardLayoutc_Card;  
JButton box1,box2,box3;  
Container d;  
	CardDemo1()
{  
		d=getContentPane();  
		c_Card=new CardLayout(40,30);  
		d.setLayout(c_Card);

		 box1=new JButton("studytonight_1");  
		 box2=new JButton("studytonight_2");  
		box3=new JButton("studytonight_3");  
		box1.addActionListener(this);  
		box2.addActionListener(this);  
		box3.addActionListener(this);  

		d.add("P",box1);
		d.add("Q",box2);
		d.add("R",box3); 
    }  
    public void actionPerformed(ActionEvent e) 
{  
		c_Card.next(d);  
	}  
    public static void main(String[] args) 
{  
		CardDemo1 obj =new CardDemo1();  
		obj.setSize(500,500);  
		obj.setVisible(true);  
		obj.setDefaultCloseOperation(EXIT_ON_CLOSE);  
	}  
}
	

card-layout card-layout