Core Java - [AWT GridLayout [Frame] Example]

♠ Posted by Unknown in at 22:11
GridLayout  Manager on Frame

Example:


import java.awt.*;
import java.awt.event.*;

class FindHighestExample extends Frame implements ActionListener
{
    Label lblFirst, lblSecond, lblThird, lblAnswer;
    TextField tfFirst, tfSecond, tfThird;
    Button btnCheck, btnClear, btnExit;
    Panel p1, p2;

    public FindHighestExample()
    {
        lblFirst = new Label("Enter First Value : ");
        lblSecond = new Label("Enter Second Value : ");
        lblThird = new Label("Enter Third Value : ");
        lblAnswer = new Label("The Highest : ");

        tfFirst = new TextField("0");
        tfSecond = new TextField("0");
        tfThird = new TextField("0");

        btnCheck = new Button("Check");
        btnClear = new Button("Clear");
        btnExit = new Button("Exit");

        setLayout(new FlowLayout());
        p1 = new Panel();
        p1.setLayout(new GridLayout(4,2,5,10));

        p2 = new Panel();
        p2.setLayout(new FlowLayout(FlowLayout.CENTER,5,10));

        add(p1);
        add(p2);

        p1.add(lblFirst);
        p1.add(tfFirst);
        p1.add(lblSecond);
        p1.add(tfSecond);
        p1.add(lblThird);
        p1.add(tfThird);
        p1.add(lblAnswer);

        p2.add(btnCheck);
        btnCheck.addActionListener(this);
        p2.add(btnClear);
        btnClear.addActionListener(this);
        p2.add(btnExit);
        btnExit.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource() == btnCheck)
        {
                int a, b, c;
                a = Integer.parseInt(tfFirst.getText());
                b = Integer.parseInt(tfSecond.getText());
                c = Integer.parseInt(tfThird.getText());

                if((a>b) && (a>c))
                {
                        lblAnswer.setText("The Highest : " + a);
                }
                else if(b>c)
                {
                        lblAnswer.setText("The Highest : " + b);
                }
                else
                {
                        lblAnswer.setText("The Highest : " + c);
                }
        }
        else if(ae.getSource() == btnClear)
        {
                tfFirst.setText("0");
                tfSecond.setText("0");
                tfThird.setText("0");

        }
        else
        {
                dispose();
                System.exit(0);
        }
    }

    public static void main(String[] args) 
    {
        FindHighestExample f = new FindHighestExample();
        f.setVisible(true);
        f.setSize(400,200);
    }

}


1 comments:

Thanks for Sharing this valuble information and itis useful for me and CORE JAVA learners.We also provides the best Online CORE JAVA Training classes.

Post a Comment