Monday, March 24, 2014

simple calculator using java applet

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class calc extends Applet implements ActionListener
{
Label l1,l2,l3;
TextField t1,t2,t3;
Button add1,sub,mult,div;
public void init()
{
l1=new Label("first no.:");
add(l1);



t1=new TextField(10);
add(t1);

l2=new Label("2nd no.:");
add(l2);

t2=new TextField(10);
add(t2);

l3=new Label("Result:");
add(l3);


t3=new TextField(10);
add(t3);


add1=new Button("+");
add(add1);
add1.addActionListener(this);


sub=new Button("-");
add(sub);
sub.addActionListener(this);

mult=new Button("*");
add(mult);
mult.addActionListener(this);

div=new Button("/");
add(div);
div.addActionListener(this);


}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==add1)
{
int sum=Integer.parseInt(t1.getText()) + Integer.parseInt(t2.getText());
t3.setText(String.valueOf(sum));
}

if(ae.getSource()==sub)
{
int sub=Integer.parseInt(t1.getText()) - Integer.parseInt(t2.getText());
t3.setText(String.valueOf(sub));
}


if(ae.getSource()==mult)
{
int mul=Integer.parseInt(t1.getText()) * Integer.parseInt(t2.getText());
t3.setText(String.valueOf(mul));
}


if(ae.getSource()==div)
{
int div=Integer.parseInt(t1.getText()) / Integer.parseInt(t2.getText());
t3.setText(String.valueOf(div));
}

}

}
/*
<applet code="calc" width=200 height=200>
</applet>
*/


2 comments:

Anonymous said...

thanks a lot ...

Anonymous said...

I was looking for this. My minor project is same.