/**********************Calculator in java AWT******************************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Calculator implements ActionListener,WindowListener
{
String data= "0",opr;
int number, first_number, result;
Frame f;
Button b[]=new Button [10];
Button b1[]= new Button [6];
TextField tf;
Calculator()
{
f= new Frame("Calculator");
tf= new TextField("");
for(int i=0;i<10;i++)
{
String str= Integer.toString(i);
b[i]= new Button(str);
}
b1[0]= new Button("+");
b1[1]= new Button("-");
b1[2]= new Button("*");
b1[3]= new Button("/");
b1[4]= new Button("C");
b1[5]= new Button("=");
}
void display()
{
try{
int left,top, hight,width;
left=20;
top=80;
hight=30;
width=30;
f.setLayout(null);
// f.setSize(220,250);
f.setBounds(350,250,220,250);
f.setVisible(true);
for(int i=1;i<10;i++)
{
b[i].setBounds(left,top,width,hight);
if(i%3==0)
{
top=top+40;
left= 20;
}
else
left=left+40;
}
b[0].setBounds(left+40,top,width,hight);
b1[0].setBounds(left+120,80,width*2,hight);
b1[1].setBounds(left+120,120,width*2,hight);
b1[2].setBounds(left+120,160,width*2,hight);
b1[3].setBounds(left+120,200,width*2,hight);
b1[4].setBounds(left,200,width,hight);
b1[5].setBounds(left+80,200,width,hight);
tf.setBounds(left,40,180,hight);
for(int i=0;i<10;i++)
f.add(b[i]);
for(int i=0;i<6;i++)
f.add(b1[i]);
f.add(tf);
for(int i=0;i<10;i++)
b[i].addActionListener(this);
for(int i=0;i<6;i++)
b1[i].addActionListener(this);
f.addWindowListener(this);
}
catch(Exception e)
{
System.out.println(e);
}
}
/**************************************Window Listener Interface************************/
public void windowActivated(WindowEvent e)
{}
public void windowClosed(WindowEvent e)
{}
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
public void windowDeactivated(WindowEvent e)
{}
public void windowDeiconified(WindowEvent e)
{}
public void windowIconified(WindowEvent e)
{}
public void windowOpened(WindowEvent e)
{}
public void actionPerformed(ActionEvent e)
{
try{
if(e.getSource()==b[0])
{
number=(number*10)+0;
data=Integer.toString(number);
tf.setText(data);
}
else if(e.getSource()==b[1])
{
number=(number*10)+1;
data=Integer.toString(number);
tf.setText(data);
}
else if(e.getSource()==b[2])
{
number=(number*10)+2;
data=Integer.toString(number);
tf.setText(data);
}
else if(e.getSource()==b[3])
{
number=(number*10)+3;
data=Integer.toString(number);
tf.setText(data);
}
else if(e.getSource()==b[4])
{
number=(number*10)+4;
data=Integer.toString(number);
tf.setText(data);
}
else if(e.getSource()==b[5])
{
number=(number*10)+5;
data=Integer.toString(number);
tf.setText(data);
}
else if(e.getSource()==b[6])
{
number=(number*10)+6;
data=Integer.toString(number);
tf.setText(data);
}
else if(e.getSource()==b[7])
{
number=(number*10)+7;
data=Integer.toString(number);
tf.setText(data);
}
else if(e.getSource()==b[8])
{
number=(number*10)+8;
data=Integer.toString(number);
tf.setText(data);
}
else if(e.getSource()==b[9])
{
number=(number*10)+9;
data=Integer.toString(number);
tf.setText(data);
}
else if(e.getSource()==b1[4])
{
}
/**************************************************************************/
if(e.getSource()==b1[0])
{
opr="+";
first_number=number;
number=0;
}
else if(e.getSource()==b1[1])
{
opr="-";
first_number=number;
number=0;
}
else if(e.getSource()==b1[2])
{
opr="*";
first_number=number;
number=0;
}
else if(e.getSource()==b1[3])
{
opr="/";
first_number=number;
number=0;
}
else if(e.getSource()==b1[4])
{
opr=" ";
number=0;
first_number=0;
result=0;
tf.setText(" ");
}
else if(e.getSource()==b1[5])
{
if(opr.equals("+"))
{
result=number+first_number;
data=Integer.toString(result);
tf.setText(data);
number=result;
result=first_number;
}
else if(opr.equals("-"))
{
result=number-first_number;
data=Integer.toString(result);
tf.setText(data);
number=result;
result=first_number;
}
else if(opr.equals("*"))
{
result=number*first_number;
data=Integer.toString(result);
tf.setText(data);
number=result;
result=first_number;
}
else if(opr.equals("/"))
{
result=number+first_number;
data=Integer.toString(result);
tf.setText(data);
first_number=result;
result=number;
}
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
//****************************************************************************
class CalculatorTest
{
public static void main(String args[])
{
Calculator c= new Calculator();
c.display();
}
}
/***************************Pic of Calculator*****************************************/
/******************Download the Zip file of above program********************************/
Download the Zip file of Calculator