Explain flow layout manager with suitable constructors and demonstrate it by using suitable java code.

 FLOW LAYOUT

It is the default layout manager for the container Panel and Applets. It simply arranges components in a single row, starting a new row if its container is not sufficiently wide. Components are laid out from the upper left corner, left to right, and top to bottom. When no more components fit on a line, the next one appears on the next line.

Commonly used Constructors

FlowLayout(); // creates a flow layout with centered alignment and a default 5 unit horizontal and vertical gap.

FlowLayout(int alignment); //creates a flow layout with the given alignment and a default 5 unit horizontal and vertical gap.

FlowLayout(int alignment, int horz_gap, int vert_gap); // creates a flow layout with the given alignment and the given horizontal and vertical gap. 

Example of flowLayout() constrctor 

import java.awt.*;

public class FlowLayout Demo extends Frame

{

Button b1,b2, 63, b4, b5, b6;

FlowLayout Demo ()

{

setSize(400, 150);

setTitle("FlowLayout Demo");

set Layout (new FlowLayout (FlowLayout.LEFT));

bl-new Button("Button 1");

b2=new Button("Button 2"); b3=new Button("Button 3");

b4=new Button("This is Button 4");

b5=new Button("5");

b6=new Button("Button 6");

add (b1);

add (b2);

add (b3);

add (b4);

add (b5);

add (b6);

setVisible (true);

}

public static void main(String args[ ])

{

new FlowLayout Demo();

}

}


                     OR,

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


Fields of FlowLayout class

public static final int LEFT

public static final int RIGHT

public static final int CENTER

public static final int LEADING

public static final int TRAILING


Constructors of FlowLayout class

FlowLayout(): creates a flow layout with centered alignment and a default 5 unit horizontal and vertical gap.

FlowLayout(int align): creates a flow layout with the given alignment and a default 5 unit horizontal and vertical gap.

FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given alignment and the given horizontal and vertical gap.

Example of FlowLayout class: Using FlowLayout(int align) constructor

FileName: MyFlowLayout.java

import java.awt.*;    

import javax.swing.*;    

public class MyFlowLayout{    

JFrame f;    

MyFlowLayout(){    

    f=new JFrame();    

        

    JButton b1=new JButton("1");    

    JButton b2=new JButton("2");    

    JButton b3=new JButton("3");    

    JButton b4=new JButton("4");    

    JButton b5=new JButton("5");    

  

     // 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 MyFlowLayout();    

}    

}    




Example of FlowLayout class: Using FlowLayout(int align, int hgap, int vgap) constructor
FileName: FlowLayoutExample1.java

// import statement  
import java.awt.*;    
import javax.swing.*;    
    
public class FlowLayoutExample1  
{    
JFrame frameObj;  
  
// constructor    
FlowLayoutExample1()  
{    
    // creating a frame object  
    frameObj = new JFrame();    
        
     // creating the buttons  
    JButton b1 = new JButton("1");    
    JButton b2 = new JButton("2");    
    JButton b3 = new JButton("3");    
    JButton b4 = new JButton("4");    
    JButton b5 = new JButton("5");  
    JButton b6 = new JButton("6");    
    JButton b7 = new JButton("7");    
    JButton b8 = new JButton("8");    
    JButton b9 = new JButton("9");    
    JButton b10 = new JButton("10");    
    
         
    // adding the buttons to frame        
    frameObj.add(b1); frameObj.add(b2); frameObj.add(b3); frameObj.add(b4);      
    frameObj.add(b5); frameObj.add(b6);  frameObj.add(b7);  frameObj.add(b8);    
    frameObj.add(b9);  frameObj.add(b10);      
       
    // parameterized constructor is used  
    // where alignment is left   
    // horizontal gap is 20 units and vertical gap is 25 units.  
    frameObj.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 25));    
        
      
    frameObj.setSize(300, 300);    
    frameObj.setVisible(true);    
}    
// main method  
public static void main(String argvs[])   
{    
    new FlowLayoutExample1();    
}    
}    


Comments

Popular posts from this blog

What are different steps used in JDBC? Write down a small program showing all steps.

Discuss classification or taxonomy of virtualization at different levels.

Pure Versus Partial EC