Reputation: 23
This is my 1st post here and I am hoping if I could get some help on a school project I have. The project is to make a calculator in Java and I have made it and it works fine for the most part, only problem I am having is that when I get an answer - lets say 5+5=10
and 10 is displayed, when I want to enter another number lets say I want to enter in 8*10 and in order to do that I write in 8, instead of deleting the previous answer which is 10 and write 8 instead of that number it will write 8 after 10, so it will be 108. What I want is for the previous answer to be deleted once I enter in a new number after an answer has been give. I hope I explained it good, here is my code and I would love some assistance on the matter since everything I tried hasn't worked. Thanks in advance.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator_UI implements ActionListener {
JFrame frame = new JFrame("Calculator");
JPanel panel = new JPanel();
JTextArea text = new JTextArea(1, 20);
JButton but1 = new JButton("1");
JButton but2 = new JButton("2");
JButton but3 = new JButton("3");
JButton but4 = new JButton("4");
JButton but5 = new JButton("5");
JButton but6 = new JButton("6");
JButton but7 = new JButton("7");
JButton but8 = new JButton("8");
JButton but9 = new JButton("9");
JButton but0 = new JButton("0");
JButton butadd = new JButton("+");
JButton butsub = new JButton("-");
JButton butmulti = new JButton("*");
JButton butdiv = new JButton("/");
JButton buteq = new JButton("=");
JButton butclear = new JButton("C");
Double number1, number2, result;
int addc = 0, subc = 0, multic = 0, divc = 0;
public void ui() {
frame.setVisible(true);
frame.setSize(230, 200);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel.add(text);
panel.add(but1);
panel.add(but2);
panel.add(but3);
panel.add(but4);
panel.add(but5);
panel.add(but6);
panel.add(but7);
panel.add(but8);
panel.add(but9);
panel.add(but0);
panel.add(butadd);
panel.add(butsub);
panel.add(butmulti);
panel.add(butdiv);
panel.add(buteq);
panel.add(butclear);
but1.addActionListener(this);
but1.setBackground(Color.cyan);
but2.addActionListener(this);
but2.setBackground(Color.cyan);
but3.addActionListener(this);
but3.setBackground(Color.cyan);
but4.addActionListener(this);
but4.setBackground(Color.cyan);
but5.addActionListener(this);
but5.setBackground(Color.cyan);
but6.addActionListener(this);
but6.setBackground(Color.cyan);
but7.addActionListener(this);
but7.setBackground(Color.cyan);
but8.addActionListener(this);
but8.setBackground(Color.cyan);
but9.addActionListener(this);
but9.setBackground(Color.cyan);
but0.addActionListener(this);
but0.setBackground(Color.cyan);
butadd.addActionListener(this);
butadd.setBackground(Color.cyan);
butsub.addActionListener(this);
butsub.setBackground(Color.cyan);
butmulti.addActionListener(this);
butmulti.setBackground(Color.cyan);
butdiv.addActionListener(this);
butdiv.setBackground(Color.cyan);
buteq.addActionListener(this);
buteq.setBackground(Color.cyan);
butclear.addActionListener(this);
butclear.setBackground(Color.cyan);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == butclear) {
number1 = 0.0;
number2 = 0.0;
text.setText("");
}
if (source == but1) {
text.append("1");
}
if (source == but2) {
text.append("2");
}
if (source == but3) {
text.append("3");
}
if (source == but4) {
text.append("4");
}
if (source == but5) {
text.append("5");
}
if (source == but6) {
text.append("6");
}
if (source == but7) {
text.append("7");
}
if (source == but8) {
text.append("8");
}
if (source == but9) {
text.append("9");
}
if (source == but0) {
text.append("0");
}
if (source == butadd) {
number1 = number_reader();
text.setText("");
addc = 1;
subc = 0;
multic = 0;
divc = 0;
}
if (source == butsub) {
number1 = number_reader();
text.setText("");
addc = 0;
subc = 1;
multic = 0;
divc = 0;
}
if (source == butmulti) {
number1 = number_reader();
text.setText("");
addc = 0;
subc = 0;
multic = 1;
divc = 0;
}
if (source == butdiv) {
number1 = number_reader();
text.setText("");
addc = 0;
subc = 0;
multic = 0;
divc = 1;
}
if (source == buteq) {
number2 = number_reader();
if (addc == 1) {
result = number1 + number2;
text.setText(Double.toString(result));
}
if (subc == 1) {
result = number1 - number2;
text.setText(Double.toString(result));
}
if (multic == 1) {
result = number1 * number2;
text.setText(Double.toString(result));
}
if (divc == 1) {
result = number1 / number2;
text.setText(Double.toString(result));
}
}
}
public double number_reader() {
Double num1;
String s;
s = text.getText();
num1 = Double.valueOf(s);
return num1;
}
}
Upvotes: 1
Views: 177
Reputation: 691735
When the user presses =
and the result is printed, you should set a flag to true, so that the next time a number button is pressed, the result is first erased.
In fact you should even transform your whole logic into a state diagram, because if, for example, the user presses +
twice, you should probably display an error message or ignore the second +
, instead of reading the current number. Each state should define what happens when any button is pressed, and what the next state is depending on which button is pressed.
Since a GUI can be smarter than a real calculator, each state could also define which buttons are enabled and which ones are disabled.
Upvotes: 4