//Author:Srinivas.b
//version:java1.5
//This program is for Calculator.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class Sricalc
{
public static void main
(String[] args
)
{
Mycalc1 m = new Mycalc1();
m.setSize(350,300);
m.setTitle("My Own Calculator");
m.show();
}
}
{
boolean startNumber = true;
CalcLogic2 l = new CalcLogic2();
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,c1,c2,c3,c4,c5,c6,c7
;
Mycalc1()
{
c.
setBackground(new Color(50,
70,
90));
c.setLayout(null);
no1.
setBackground(new Color(0,
45,
30));
no1.
setForeground(Color.
white);
no1.setBounds(150,70,180,20);
c.add(no1);
b1.
setBackground(Color.
green);
b1.
setForeground(Color.
red);
b1.setBounds(100,100,50,20);
b2.
setBackground(Color.
white);
b2.
setForeground(Color.
blue);
b2.setBounds(160,100,50,20);
b3.
setBackground(Color.
yellow);
b3.
setForeground(Color.
black);
b3.setBounds(220,100,50,20);
b4.
setBackground(Color.
black);
b4.
setForeground(Color.
red);
b4.setBounds(280,100,50,20);
b5.
setBackground(Color.
black);
b5.
setForeground(Color.
blue);
b5.setBounds(100,130,50,20);
b6.
setBackground(Color.
yellow);
b6.
setForeground(Color.
green);
b6.setBounds(160,130,50,20);
b7.
setBackground(Color.
blue);
b7.
setForeground(Color.
yellow);
b7.setBounds(220,130,50,20);
b8.
setBackground(Color.
white);
b8.
setForeground(Color.
green);
b8.setBounds(280,130,50,20);
b9.
setBackground(Color.
red);
b9.
setForeground(Color.
yellow);
b9.setBounds(100,160,50,20);
c1.
setBackground(Color.
black);
c1.
setForeground(Color.
yellow);
c1.setBounds(160,160,50,20);
c2.setBounds(220,160,50,20);
c3.
setBackground(Color.
black);
c3.
setForeground(Color.
blue);
c3.setBounds(280,160,50,20);
c4.setBounds(100,190,50,20);
c5.setBounds(160,190,50,20);
c6.setBounds(220,190,50,20);
c7.setBounds(280,190,50,20);
c.add(b1);
c.add(b2);
c.add(b3);
c.add(b4);
c.add(b5);
c.add(b6);
c.add(b7);
c.add(b8);
c.add(b9);
c.add(c1);
c.add(c2);
c.add(c3);
c.add(c4);
c.add(c5);
c.add(c6);
c.add(c7);
b1.addActionListener(numListener);
b2.addActionListener(numListener);
b3.addActionListener(numListener);
b4.addActionListener(numListener);
b5.addActionListener(numListener);
b6.addActionListener(numListener);
b7.addActionListener(numListener);
b8.addActionListener(numListener);
b9.addActionListener(numListener);
c1.addActionListener(numListener);
c2.addActionListener(opListener);
c3.addActionListener(new ClearListener());
c4.addActionListener(opListener);
c5.addActionListener(opListener);
c6.addActionListener(opListener);
c7.addActionListener(opListener);
no1.setText("0");
setDefaultCloseOperation
(JFrame.
EXIT_ON_CLOSE);
}
public void action_clear()
{
startNumber = true;
no1.setText("0");
previousOp = "=";
l.setTotal("0");
}
//This Listener for operators
{
{
if (startNumber)
{
action_clear();
no1.setText("ERROR");
}
else
{
startNumber = true;
try {
if (previousOp.equals("="))
{
l.setTotal(str);
}
else if (previousOp.equals("+"))
{
l.add(str);
}
else if (previousOp.equals("-"))
{
l.subtract(str);
}
else if (previousOp.equals("*"))
{
l.multiply(str);
}
else if (previousOp.equals("/"))
{
l.divide(str);
}
no1.setText("" + l.getTotalString());
}
action_clear();
no1.setText("Error");
}
previousOp = e.getActionCommand();
}
}
}
// This Listener is used For set number's in to Textfield
{
{
String str1
= e.
getActionCommand();
if (startNumber)
{
no1.setText(str1);
startNumber = false;
}
else
{
no1.setText(no1.getText() + str1);
}
}
}
//This Listener is Clear the textfield.
{
{
action_clear();
}
}
}
class CalcLogic2
{
private float total;
public CalcLogic2()
{
total = 0;
}
public String getTotalString
()
{
return ""+total;
}
public void setTotal
(String n
)
{
}
{
}
public void subtract
(String n
)
{
}
public void multiply
(String n
)
{
}
{
}
}