hi everyone, i have been asked to make a basic calculator but my calc only holds two integers like 2+2 it cant do more than 2 operations so id reaaly be glad if someone could take a look at my code and see what the problem is asap.im still a beginner in IT.thnx
this is what i have so far..............
/**
* (#)calculator.java
* Real author : MIMI
* version 1.00 2009/12/12
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class mycalculator extends JFrame implements ActionListener
{
JButton buttons[];
JTextField text;
JMenuBar men;
JMenu file, help;
JMenuItem ext, abt;
JPanel pan1,pan2,pan3;
String identifier;
Boolean tester = false ;
Boolean holder = false ;
Boolean decimal = false;
Double num1 , num2 , result;
JButton clear ;
public mycalculator()
{
men = new JMenuBar();
setJMenuBar(men);
ext = new JMenuItem("Exit");
file = new JMenu("file");
file.add(ext);
men.add(file);
abt = new JMenuItem("About");
help = new JMenu("help");
help.add(abt);
men.add(help);
ext.addActionListener(this);
abt.addActionListener(this);
pan1 = new JPanel();
pan1.setLayout(new GridLayout(2,2,2,2));
text = new JTextField("");
text.setHorizontalAlignment(JTextField.RIGHT);
pan1.add(text);
pan2 = new JPanel();
pan2.setLayout(new GridLayout(4,4,4,4));
buttons = new JButton[16];
String str[] = {"7","8","9","*","4","5","6","/","1","2","3","+",".","0","=","-"};
for(int i=0 ; i<buttons.length ; i++)
{
buttons[i] = new JButton(str[i]);
pan2.add(buttons[i]);
buttons[i].addActionListener(this);
pan3 = new JPanel();
clear = new JButton("clear");
pan3.add(clear);
clear.addActionListener(this);
setLayout(new BorderLayout());
add(pan1 ,BorderLayout.NORTH);
add(pan2 ,BorderLayout.CENTER);
add(pan3 ,BorderLayout.SOUTH);
setVisible(true);
setSize(350,300);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("My calcualtor");
}
}
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource() == abt)
{
JOptionPane.showMessageDialog(null, "author : ");
}
else if(evt.getSource() == ext)
{
System.exit(0);
}
if(evt.getActionCommand()== "1")
{if (text.getText().equals("0"))
text.setText("1");
else if (tester == true)
{
text.setText("1");
tester = false;
}
else
text.setText(text.getText() + "1");
}
else if(evt.getActionCommand()== "2")
{
if (text.getText().equals("0"))
text.setText("2");
else if (tester == true)
{
text.setText("2");
tester = false;
}
else
text.setText(text.getText() + "2");
}
else if(evt.getActionCommand()== "3")
{
if (text.getText().equals("0"))
text.setText("3");
else if (tester == true )
{
text.setText("3");
tester = false;
}
else
text.setText(text.getText()+ "3");
}
else if(evt.getActionCommand()== "4")
{
if (text.getText().equals("0"))
text.setText("4");
else if (tester == true )
{
text.setText("4");
tester = false;
}
else
text.setText(text.getText()+ "4");
}
else if(evt.getActionCommand()== "5")
{
if(text.getText().equals("0"))
text.setText("5");
else if (tester == true )
{
text.setText("5");
tester = false;
}
else
text.setText(text.getText()+ "5");
}
else if(evt.getActionCommand()== "6")
{if(text.getText().equals("0"))
text.setText("6");
else if (tester == true )
{
text.setText("6");
tester = false;
}
else
text.setText(text.getText()+ "6");
}
else if(evt.getActionCommand()== "7")
{ if(text.getText().equals("0"))
text.setText("7");
else if (tester == true )
{
text.setText("7");
tester = false;
}
else
text.setText(text.getText()+ "7");
}
else if(evt.getActionCommand()== "8")
{ if(text.getText().equals("0"))
text.setText("8");
else if (tester == true )
{
text.setText("8");
tester = false;
}
else
text.setText(text.getText()+ "8");
}
else if(evt.getActionCommand()== "9")
{ if(text.getText().equals("0"))
text.setText("9");
else if (tester == true )
{
text.setText("9");
tester = false;
}
else
text.setText(text.getText()+ "9");
}
else if(evt.getActionCommand()== "0")
{ if (text.getText().equals("0"))
text.setText("0");
else if (tester == true )
{
text.setText("0");
tester = false;
}
else
text.setText(text.getText()+ "0");
}
else if (evt.getActionCommand() == "clear")
{
text.setText(null);
tester = false;
}
else if (evt.getActionCommand() == ".")
{if(decimal == false)
{
text.setText(text.getText()+ ".");
decimal = true;
}
}
else if (evt.getActionCommand() == "+")
{
num1 = Double.parseDouble(text.getText());
tester = true;
identifier = "+";
decimal = false;
holder = true;
}
else if (evt.getActionCommand() == "-")
{
num1 = Double.parseDouble(text.getText());
tester = true;
identifier = "-";
decimal = false;
holder = true;
}
else if (evt.getActionCommand() == "*")
{
num1 = Double.parseDouble(text.getText());
tester = true;
identifier = "*";
decimal = false;
holder = true;
}
else if (evt.getActionCommand() == "/")
{
num1 = Double.parseDouble(text.getText());
tester = true;
identifier = "/";
decimal = false;
holder = true;
}
else if (evt.getActionCommand() == "=")
{
num2 = Double.parseDouble(text.getText());
tester = true;
holder = false;
if (identifier == "+")
{
result = num1 + num2;
text.setText(String.valueOf(result));
}
else if (identifier =="-")
{
result = num1 - num2;
text.setText(String.valueOf(result));
}
else if (identifier == "*")
{
result = num1 * num2;
text.setText(String.valueOf(result));
}
else if (identifier == "/")
{
result = num1 / num2;
text.setText(String.valueOf(result));
}
}
}
public static void main (String[] args)
{
new mycalculator();
}}