Thursday, 12 March 2015

BoxLayout

BoxLayout class:

The BoxLayout is used to arrange the components either vertically or horizontally. For this purpose, BoxLayout provides four constants. They are as follows:

Note: BoxLayout class is found in javax.swing package.

Fields of BoxLayout class:

  1. public static final int X_AXIS
  2. public static final int Y_AXIS
  3. public static final int LINE_AXIS
  4. public static final int PAGE_AXIS

Constructor of BoxLayout class:

  1. BoxLayout(Container c, int axis): creates a box layout that arranges the components with the given axis.

Example of BoxLayout class with Y-AXIS:

BoxLayout class
import java.awt.*;  
import javax.swing.*;  
  
public class BoxLayoutExample1 extends Frame {  
 Button buttons[];  
  
 public BoxLayoutExample1 () {  
   buttons = new Button [5];  
    
   for (int i = 0;i<5;i++) {  
      buttons[i] = new Button ("Button " + (i + 1));  
      add (buttons[i]);  
    }  
  
setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));  
setSize(400,400);  
setVisible(true);  
}  
  
public static void main(String args[]){  
BoxLayoutExample1 b=new BoxLayoutExample1();  
}  
}  

Example of BoxLayout class with X-AXIS:

BoxLayout class example
import java.awt.*;  
import javax.swing.*;  
  
public class BoxLayoutExample2 extends Frame {  
 Button buttons[];  
  
 public BoxLayoutExample2() {  
   buttons = new Button [5];  
    
   for (int i = 0;i<5;i++) {  
      buttons[i] = new Button ("Button " + (i + 1));  
      add (buttons[i]);  
    }  
  
setLayout (new BoxLayout(this, BoxLayout.X_AXIS));  
setSize(400,400);  
setVisible(true);  
}  
  
public static void main(String args[]){  
BoxLayoutExample2 b=new BoxLayoutExample2();  
}  
}  


FlowLayout

FlowLayout

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

Fields of FlowLayout class:

  1. public static final int LEFT
  2. public static final int RIGHT
  3. public static final int CENTER
  4. public static final int LEADING
  5. public static final int TRAILING

Constructors of FlowLayout class:

  1. FlowLayout(): creates a flow layout with centered alignment and a default 5 unit horizontal and vertical gap.
  2. FlowLayout(int align): creates a flow layout with the given alignment and a default 5 unit horizontal and vertical gap.
  3. 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:

FlowLayout class
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");  
              
    f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);  
      
    f.setLayout(new FlowLayout(FlowLayout.RIGHT));  
    //setting flow layout of right alignment  
  
    f.setSize(300,300);  
    f.setVisible(true);  
}  
public static void main(String[] args) {  
    new MyFlowLayout();  
}  
}