Program of addition in awt (Textfield)
PROGRAM-
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet
code="Calculate" height=200 width=200>
</applet>
*/
public class Calculate extends
Applet implements ActionListener
{
TextField
txta,txtb,txtc;
Button
b1,b2;
public
void init()
{
Label
l1=new Label("Enter the value of a
:");
add(l1);
txta=new
TextField(20);
add(txta);
Label
l2=new Label("Enter the value of b
:");
add(l2);
txtb=new
TextField(20);
add(txtb);
Label
l3=new Label("Result :");
add(l3);
txtc=new
TextField(20);
add(txtc);
txtc.setEditable(false);
b1=new
Button("Add");
add(b1);
b1.addActionListener(this);
b2=new Button("Reset");
add(b2);
b2.addActionListener(this);
}
public
void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
/*add
the numbers */
int
val1=Integer.parseInt(txta.getText());
int
val2=Integer.parseInt(txtb.getText());
int
val3=val1+val2;
txtc.setText(val3+"");
}
else
{
/*clear
all the text boxes*/
txta.setText("");
txtb.setText("");
txtc.setText("");
}
}
}
Comments
Post a Comment