Apr 17, 2012

Flow Layout


The FlowLayout arrange the components from 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. A small space is left between every component on each side.

Constructors of FlowLayout:

FlowLayout()
FlowLayout(int align)
FlowLayout(int align, int horizontal, int vertical)

The first form creates a default layout,  which centers components and leaves five pixels of space between components.
The second one uses the following alignment constants. They align the components left, center and right respectively.

FlowLayout.LEFT
FlowLayout.CENTER
FlowLayout.RIGHT

The third form in addition to specify the vertical and horizontal space between the components.

Example:

import java.awt.*;
class Fl extends Frame
{
   Fl(String s)
   {
     super(s);
     setSize(300,140);
            setLayout(new FlowLayout());
            for(int i=0 ; i<=9 ; i++)
                    add(new Button(“Button No”+i));
                    setVisible(true);
   }
}
class test
{
Public static void main(String[] args)
        {
                Fl=new Fl(“Flow Layout”);
        }
}

The following applet program use the second type of FlowLayout constructor and the alignment value is LEFT. Try yourself to change the alignment Center and Right.

Example:

import java.awt.*;
import java.applet*;


public class Left extends Applet
{
      Button b1,b2,b3;
      Public void init()
          {
              setLayout(new FlowLayout(FlowLayout.LEFT));
              b1=new Button(“One”);
              b2=new Button(“Two”);
              b3=new Button(“Three”);
              add(b1);
              add(b2);
              add(b3);
          }
}

/*
<applet code=”Left.class” width=300 height=200>
</applet>
*/


0 comments :

Post a Comment