Table of Contents

Java Layout Managers

Layout Manager is an interface that is implemented by all the classes of layout managers. The Layout Managers are used to arrange components in a particular manner. The Layout managers enable us to control the way in which visual components are arranged in the GUI forms by determining the size and position of components within the containers.

i) Flow Layout:

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

//FlowLayoutExample.java
import java.awt.Frame;
import java.awt.Label;
import java.awt.FlowLayout;
public class FlowLayoutExample{
  public FlowLayoutExample(){
   Frame frame = new Frame("Flow Layout");
   Label label1 = new Label("One");
   Label label2 = new Label("Two");
   Label label3 = new Label("Three");
   Label label4 = new Label("Four");

   frame.add(label1);
   frame.add(label2);
   frame.add(label3);
   frame.add(label4);

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

   new FlowLayoutExample( );
   }}

output:

ii) Border Layout:

Border Layout is a Layout manager that allows us to use different areas of a screen. Itis used, when we want to arrange the components in five regions. The five regions can be north, south, east, west and the center.

Example:

//BorderLayoutExample .java
import java.awt.Frame;  
import java.awt.Button;
import java.awt.BorderLayout;  

public class BorderLayoutExample
{  
Frame frame;  
BorderLayoutExample ()
{  
   Frame frame=new Frame("Border Layout");  

   Button box1=new Button("**NORTH**");;  
   Button box2=new Button("**SOUTH**");;  
   Button box3=new Button("**EAST**");;  
   Button box4=new Button("**WEST**");;  
   Button box5=new Button("**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 BorderLayoutExample();  
}  
}

output:

iii) Grid Layout:

Grid Layout is used, when we want to arrange the components in a rectangular grid. A grid is a two- dimensional structure in which components are aligned in rows and columns.

Example:

//GridLayoutExample.java
import java.awt.Frame;  
import java.awt.Button;
import java.awt.GridLayout;

public class GridLayoutExample{  
Frame frame1;  
GridLayoutExample(){
  Frame frame1=new Frame("Grid Layout");  

Button box1=new Button("*1*");  
Button box2=new Button("*2*");  
Button box3=new Button("*3*");  
Button box4=new Button("*4*");  
Button box5=new Button("*5*");  
Button box6=new Button("*6*");  
Button box7=new Button("*7*");  
Button box8=new Button("*8*");  
Button box9=new Button("*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 GridLayoutExample();  
}  
}

Output:

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?