coderz.py

Keep Coding Keep Cheering!

Layout Managers in Java

java thread class

Layout Managers are used to arrange components in a specific order. The Java Layout Managers allow us to control the positioning and size of GUI form components. Layout Manager is an interface that is implemented by all layout manager classes.

Several AWT and Swing classes provide general-purpose layout managers:
  • BorderLayout
  • BoxLayout
  • CardLayout
  • FlowLayout
  • GridBagLayout
  • GridLayout
  • GroupLayout
  • SpringLayout

We can find these layouts in the window builder using the Eclipse IDE.

Border Layout:

The Border Layout is used to organize the components into five distinct regions: north, south, east, west, and center. Each region (area) may only have one component. It is the standard frame or window layout. 5 types of constants in Border Layout are:

  • 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:
package gui1;


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

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

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

		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 Coderz();  
}  
} 
	
Output:
Flow Layout:

The Java FlowLayout class is used to arrange the components one after the other in a line (in a flow). It is the applet or panel’s default layout.

3 types of constructors in the Flow Layout are:

  • FlowLayout()
  • FlowLayout(int align)
  • FlowLayout(int align, int hgap, int vgap)
Example:
package gui1;


import java.awt.*;    
import javax.swing.*;    
    
public class Coderz{    
JFrame f;    
Coderz(){    
    f=new JFrame();    
        
    JButton b1=new JButton("C");    
    JButton b2=new JButton("C++");    
    JButton b3=new JButton("JAVA");    
    JButton b4=new JButton("SQL");    
    JButton b5=new JButton("RUBY");    
  
     // adding buttons to the frame           
    f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.add(b5);   
   
     // setting flow layout of right alignment    
    f.setLayout(new FlowLayout(FlowLayout.RIGHT));    
      
    f.setSize(300,300);    
    f.setVisible(true);    
}    
public static void main(String[] args) {    
    new Coderz();    
}    
}
Output:
Grid Layout:

To arrange the components in a rectangular grid, the Java GridLayout class is used. Each rectangle displays one component.3 types of constructors in Grid Layout are :

  • 1. GridLayout()
  • 2. GridLayout(int rows, int columns)
  • 3. GridLayout(int rows, int columns, inthgap, int vgap)
Example:
package gui1;


import java.awt.*;    
import javax.swing.*;    
    
public class Coderz{    
JFrame f;    
   
Coderz(){    
    f=new JFrame();    
    JButton b1=new JButton("A");    
    JButton b2=new JButton("B");    
    JButton b3=new JButton("C");    
    JButton b4=new JButton("D");    
    JButton b5=new JButton("E");    
    JButton b6=new JButton("F");    
    JButton b7=new JButton("G");    
    JButton b8=new JButton("H");    
    JButton b9=new JButton("I");    
     // adding buttons to the frame       
    f.add(b1); f.add(b2); f.add(b3);  
    f.add(b4); f.add(b5); f.add(b6);  
    f.add(b7); f.add(b8); f.add(b9);    
  
    // setting grid layout of 3 rows and 3 columns    
    f.setLayout(new GridLayout(3,3));    
    f.setSize(300,300);    
    f.setVisible(true);    
}    
public static void main(String[] args) {    
    new Coderz();    
}    
}
Output:
Box Layout:

To arrange the components vertically or horizontally, the Java BoxLayout class is used. The BoxLayout class provides four constants for this purpose.

  • public static final int X_AXIS
  • public static final int Y_AXIS
  • public static final int LINE_AXIS
  • public static final int PAGE_AXIS
Example:
package gui1;

import java.awt.*;    
import javax.swing.*;    
    
public class Coderz extends Frame {    
 Button buttons[];    
    
 public Coderz () {    
   buttons = new Button [5];    
      
   for (int i = 0;i<5;i++) {    
      buttons[i] = new Button ("Button " + (i + 1));    
      // adding the buttons so that it can be displayed  
      add (buttons[i]);    
    }    
  // the buttons will be placed horizontally  
setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));    
setSize(400,400);    
setVisible(true);    
}    
  // main method  
public static void main(String args[]){    
	Coderz b=new Coderz();    
}    
}    
Output
Card Layout:

The Java Card Layout class manages the components in such a way that just one component is visible at a time. It treats each component as a card that’s why it’s referred to as Card Layout. Constructors of CardLayout Class:

  • CardLayout()
  • CardLayout(int hgap, int vgap)
Example:
package gui1;

import java.awt.*;    
import java.awt.event.*;    
    
import javax.swing.*;    
    
public class Coderz extends JFrame implements ActionListener{    
CardLayout card;    
JButton b1,b2,b3;    
Container c;    
Coderz(){    
            
        c=getContentPane();    
        card=new CardLayout(40,30);    
//create CardLayout object with 40 hor space and 30 ver space    
        c.setLayout(card);    
            
        b1=new JButton("Java");    
        b2=new JButton("is Object Oriented");    
        b3=new JButton("Language");    
        b1.addActionListener(this);    
        b2.addActionListener(this);    
        b3.addActionListener(this);    
                
        c.add("a",b1);c.add("b",b2);c.add("c",b3);    
                            
    }    
    public void actionPerformed(ActionEvent e) {    
    card.next(c);    
    }    
    
    public static void main(String[] args) {    
    	Coderz cl=new Coderz();    
        cl.setSize(400,400);    
        cl.setVisible(true);    
        cl.setDefaultCloseOperation(EXIT_ON_CLOSE);    
    }    
}    
Output:

When the button named ‘java’ is clicked, we get

When the button named ‘is object oriented’is clicked, we get

Note: also read about the Queue Interface in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Comment

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

Advertisement