06-04-2011, 01:24 PM
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Choice.*;
//<applet code=Awte width=500 height=555></applet>
public class Awte extends Applet implements TextListener,ActionListener
{
int a,b,c;
String s;
TextField f1,f2,f3;
Label l1,l2,l3;
Button Add,Sub,Mul,Div;
public void init()
{
//setBackground(Color.green);
setForeground(Color.red);
l1=new Label("First number");
l2=new Label("Second number");
l3=new Label("Result");
f1=new TextField(10);
f2=new TextField(20);
f3=new TextField(20);
//f3=new TextField(20);
//f2.setEchochar("*");
add(l1);
add(f1);
add(l2);
add(f2);
add(l3);
add(f3);
Add=new Button("Add");
Sub=new Button("Sub");
Mul=new Button("Mult");
Div=new Button("Div");
add(Add);
add(Sub);
add(Mul);
add(Div);
f1.addTextListener(this);
f2.addTextListener(this);
f3.addTextListener(this);
Add.addActionListener(this);
Sub.addActionListener(this);
Mul.addActionListener(this);
Div.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
a=Integer.parseInt(f1.getText());
b=Integer.parseInt(f2.getText());
if(ae.getActionCommand().equals("Add"))
c=a+b;
else if(ae.getActionCommand().equals("Sub"))
c=a-b;
else if(ae.getActionCommand().equals("Mult"))
c=a*b;
else
c=a/b;
s=String.valueOf(c);
repaint();
}
public void textValueChanged(TextEvent te)
{
}
public void paint(Graphics g)
{
f3.setText(s);
//g.drawString(String.valueOf(c),355,355);
}
}
OUTPUT
E:\msc3\jiss\java>javac awte.java
E:\msc3\jiss\java>ap