Apr 30, 2008

JAVA Applet Program for doing simple calculation Addition, Substraction, Multiplication

/*
It is a JAVA Applet program for doing simple calculation using GUI. It performs Addition, Substraction, Multiplication Here in this program Buttons, TextField, Labels, CheckboxGroup and Checkbox are used. User has to input values into and A and B textboxes and then choose the appropriate operation such as Add, Sub or mul then press calculate button to perform the choosen operation.
*/


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

public class lademo extends Applet implements ActionListener
{
Button b1;
TextField t1,t2,t3;
Label l1,l2,l3;
String a;
CheckboxGroup cbg;
Checkbox c1,c2,c3;



public void init()
{
setLayout(null);

cbg=new CheckboxGroup();
t1 = new TextField(10);
b1 = new Button("Calculate");
t2 = new TextField(10);
t3 = new TextField(10);

l1 = new Label("A");
l2 = new Label("B");
l3 = new Label("C");

c1 = new Checkbox("Add",cbg,true);
c2 = new Checkbox("Sub",cbg,false);
c3 = new Checkbox("Mul",cbg,false);

add(b1);
add(t1);
add(t2);
add(t3);
add(l1);
add(l2);
add(l3);
add(c1);
add(c2);
add(c3);


l1.setBounds(100,10,100,20);
l2.setBounds(100,30,100,20);
l3.setBounds(100,50,100,20);
t1.setBounds(200,10,100,20);
t2.setBounds(200,30,100,20);
t3.setBounds(200,50,100,20);
b1.setBounds(200,70,100,20);
c1.setBounds(200,90,100,20);
c2.setBounds(300,90,100,20);
c3.setBounds(400,90,100,20);
b1.addActionListener(this);
}


public void actionPerformed(ActionEvent ae)
{

if (ae.getSource()==b1)
{

if (cbg.getSelectedCheckbox().getLabel()=="Add")
{
int z= Integer.parseInt(t1.getText())+Integer.parseInt(t2.getText());
t3.setText(Integer.toString(z));
}

if (cbg.getSelectedCheckbox().getLabel()=="Sub")
{
int z= Integer.parseInt(t1.getText())-Integer.parseInt(t2.getText());
t3.setText(Integer.toString(z));
}

if (cbg.getSelectedCheckbox().getLabel()=="Mul")
{
int z= Integer.parseInt(t1.getText())*Integer.parseInt(t2.getText());
t3.setText(Integer.toString(z));
}

}
}

};


Output of the above program as shown below