Core Java - [AWT FlowLayout Manager]

♠ Posted by Unknown in at 11:34

FlowLayout


FlowLayout is the simplest layout manager. The components are arranged in the
container from left to right in the order in which they were added. When one row
becomes filled, a new row is started. You can specify the way the components are
aligned by using one of three constants: 

                  FlowLayout.RIGHT
                  FlowLayout.CENTER
                  FlowLayout.LEFT

you can also specify the gap between components in pixels. FlowLayout has the
following three constructors.
                 
                  public FlowLayout(int align, int hGap, int vGap)

This is constructs a new FlowLayout with a specified alignment, horizontal gap, and vertical gap. The gaps are the distances in pixels between components.

                  public FlowLayout(int alignment)

This constructs a new FlowLayout with a specified alignment and default gap of 5 pixels for both horizontal and vertical.

                  public FlowLayout()

This constructs a new FlowLayout with a default center alignment and default gap of five pixels for both horizontal and vertical.


Example:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class FlowLayoutExample extends Applet implements ActionListener 
{
    TextField tfFirstValue, tfSecondValue, tfAnswer;
    Label lblFirstValue, lblSecondValue, lblAnswer;
    Button btnSum;
    Panel p1;
    Panel p2;
    
    public void init()
    {
            lblFirstValue = new Label("Enter First value : ");
            lblSecondValue = new Label("Enter Second Value : ");
            lblAnswer = new Label("The Answer : ");
            tfFirstValue = new TextField();
            tfSecondValue = new TextField();
            tfAnswer = new TextField();
            btnSum = new Button("Sum");
            p1 = new Panel(new FlowLayout(FlowLayout.CENTER,10,20));
            p2 = new Panel(new FlowLayout(FlowLayout.CENTER,10,20));
            add(p1);
            add(p2);
            p1.add(lblFirstValue);
            p1.add(tfFirstValue);
            p1.add(lblSecondValue);
            p1.add(tfSecondValue);
            p1.add(lblAnswer);
            p1.add(tfAnswer);
            p2.add(btnSum);
            btnSum.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae)
    {
            int a = Integer.parseInt(tfFirstValue.getText());
            int b = Integer.parseInt(tfSecondValue.getText());
            int c = a + b;
            tfAnswer.setText(Integer.toString(c));
    }
}

0 comments:

Post a Comment