Rotate your phone or change to desktop for better experience

Rotate your phone or change to desktop for better experience

Pgm 8 || Section B || Program to create a window with TextFields and Buttons. The "ADD" button adds the two integers and display the result. The "CLEAR" button shall clear all the text fields.

 import java.awt.*;

import java.awt.event.*;

public class Pgm8 implements ActionListener

{

Label l1,l2,l3;

Button b1,b2,b3;

TextField t1,t2;

Pgm8()

{

Frame f=new Frame("ActionListener Example");

l1=new Label("Enter No-1");

l2=new Label("Enter No-2");

l3=new Label();

t1=new TextField(10);

t2=new TextField(10);


l1.setBounds(50,50, 100,20);

l2.setBounds(50,80, 100,20);

l3.setBounds(50,100, 100,20);

t1.setBounds(200,50, 100,20);

t2.setBounds(200,80, 100,20);

b1=new Button("ADD");

b2=new Button("Clear");

b3=new Button("Exit");

b1.setBounds(100,150,60,30);

b2.setBounds(200,150,60,30);

b3.setBounds(280,150,60,30);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

f.add(b1);

f.add(b2);

f.add(b3);

f.add(l1);


f.add(l2);

f.add(l3);

f.add(t1);

f.add(t2);

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

}

//3rd step

public void actionPerformed(ActionEvent e)

{

if (e.getSource() == b1)

{

int n1=Integer.parseInt(t1.getText());

int n2=Integer.parseInt(t2.getText());

int n3= n1+n2;

l3.setText("Sum="+n3);

}

if (e.getSource() == b2)

{

t1.setText("");

t2.setText("");

}

if (e.getSource() == b3)

{

System.exit(0);

}

}

public static void main(String[] args)

{

new Pgm8();

}

}

Post a Comment

0 Comments