我知道可能有更好的方法让这个程序做它做的事情,但是我想不出一个更好的算法来完全按照这个程序做,任何建议都是有帮助的
package calc;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
/**
*
* @author Ben
*/
public class GUI extends JFrame {
int response, count=0;
double num1, num2, num3, num5, num6, num7, num8, total=0;
String operation, answer, num, testnum;
private JButton one, two, three, four, five, six, seven, eight,
nine, zero, multiply, divide, subtract, add, equals, clear;
private JTextField display, fakedisplay;
public GUI(){
super("Calculator");
setLayout (new FlowLayout());
fakedisplay = new JTextField(10);
display = new JTextField(10);
display.setEditable(false);
add(display);
one = new JButton("1");
add(one);
two = new JButton("2");
add(two);
three = new JButton("3");
add(three);
four = new JButton("4");
add(four);
five = new JButton("5");
add(five);
six = new JButton("6");
add(six);
seven = new JButton("7");
add(seven);
eight = new JButton("8");
add(eight);
nine = new JButton("9");
add(nine);
zero = new JButton("0");
add(zero);
multiply = new JButton("*");
add(multiply);
divide = new JButton("/");
add(divide);
subtract = new JButton("-");
add(subtract);
add = new JButton("+");
add(add);
equals = new JButton("=");
add(equals);
clear = new JButton("Clear");
add(clear);
handler handle = new handler();
one.addActionListener(handle);
two.addActionListener(handle);
three.addActionListener(handle);
four.addActionListener(handle);
five.addActionListener(handle);
six.addActionListener(handle);
seven.addActionListener(handle);
eight.addActionListener(handle);
nine.addActionListener(handle);
zero.addActionListener(handle);
multiply.addActionListener(handle);
divide.addActionListener(handle);
subtract.addActionListener(handle);
add.addActionListener(handle);
equals.addActionListener(handle);
clear.addActionListener(handle);
}
private class handler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
if(e.getSource()==one){
response = 1;
display.setText(display.getText() + response);
fakedisplay.setText(fakedisplay.getText() + response);
}
else if (e.getSource()==two){
response = 2;
display.setText(display.getText() + response);
fakedisplay.setText(fakedisplay.getText() + response);
}
else if (e.getSource()==three){
response = 3;
display.setText(display.getText() + response);
fakedisplay.setText(fakedisplay.getText() + response);
}
else if (e.getSource()==four){
response = 4;
display.setText(display.getText() + response);
fakedisplay.setText(fakedisplay.getText() + response);
}
else if (e.getSource()==five){
response = 5;
display.setText(display.getText() + response);
fakedisplay.setText(fakedisplay.getText() + response);
}
else if (e.getSource()==six){
response = 6;
display.setText(display.getText() + response);
fakedisplay.setText(fakedisplay.getText() + response);
}
else if (e.getSource()==seven){
response = 7;
display.setText(display.getText() + response);
fakedisplay.setText(fakedisplay.getText() + response);
}
else if (e.getSource()==eight){
response = 8;
display.setText(display.getText() + response);
fakedisplay.setText(fakedisplay.getText() + response);
}
else if (e.getSource()==nine){
response = 9;
display.setText(display.getText() + response);
fakedisplay.setText(fakedisplay.getText() + response);
}
else if(e.getSource()==zero){
response = 0;
display.setText(display.getText() + response);
fakedisplay.setText(fakedisplay.getText() + response);
}
else if (e.getSource()==multiply){
if(count == 0){
num1 = Double.parseDouble(display.getText());
count++;
}
else if(count == 1){
num2 = Double.parseDouble(fakedisplay.getText());
total = num1*num2;
count++;
}
else if(count == 2){
num3 = Double.parseDouble(fakedisplay.getText());
total = total*num3;
count++;
}
operation = "*";
display.setText(display.getText() + operation);
fakedisplay.setText("");
}
else if (e.getSource()==divide){
if(count == 0){
num1 = Double.parseDouble(display.getText());
count++;
}
else if(count == 1){
num2 = Double.parseDouble(fakedisplay.getText());
total = num1/num2+num1%num2;
count++;
}
else if(count == 2){
num3 = Double.parseDouble(fakedisplay.getText());
total = total/num3+total%num3;
count++;
}
operation = "/";
display.setText(display.getText() + operation);
fakedisplay.setText("");
}
else if (e.getSource()==add){
if(count == 0){
num1 = Double.parseDouble(display.getText());
count++;
}
else if(count == 1){
num2 = Double.parseDouble(fakedisplay.getText());
total = num1+num2;
count++;
}
else if(count == 2){
num3 = Double.parseDouble(fakedisplay.getText());
total = total+num3;
count++;
}
operation = "+";
display.setText(display.getText() + operation);
fakedisplay.setText("");
}
else if (e.getSource()==subtract){
if(count == 0){
num1 = Double.parseDouble(display.getText());
count++;
}
else if (count == 1) {
num2 = Double.parseDouble(fakedisplay.getText());
total = num1-num2;
count++;
}
else if(count == 2){
num3 = Double.parseDouble(fakedisplay.getText());
total = total-num3;
count++;
}
operation = "-";
display.setText(display.getText() + operation);
fakedisplay.setText("");
}
else if (e.getSource()==equals){
operation = "=";
display.setText(display.getText() + operation + total);
}
else if (e.getSource()==clear){
display.setText("");
}
}
}
}