package cn.edu.ldu.www;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener {
private JTextField txfResult;// 数字显示区
private JTextField txfM;// 显示文本框
private boolean isNewNumber = true;// 是不是新数字输入,默认是true
private double d1, d2;// d1代表运算数,d2用于存储数
int option;
String actionSource, lastActionSource = "123";
// 构造方法
public Calculator() {
super("JAVA 计算器");
// 设置用于显示的文本框
txfResult = new JTextField("0", 30);
txfResult.setHorizontalAlignment(JTextField.RIGHT);
txfResult.setEditable(false);
JPanel resultTextPanel = new JPanel();
resultTextPanel.add(txfResult);
//
txfM = new JTextField(10);
JButton b1 = new JButton("BackSpace");
JButton b2 = new JButton("CE");
JButton b3 = new JButton("C");
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(1, 4, 2, 2));
controlPanel.add(txfM);
controlPanel.add(b1);
controlPanel.add(b2);
controlPanel.add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
// 数字键区和运算符
String[] dcKey = { "MC", "7", "8", "9", "/", "sqrt", "MR", "4", "5",
"6", "*", "%", "MS", "1", "2", "3", "-", "1/x", "M+", "0",
"+/-", ".", "+", "=" };
JPanel dcKeyPanel = new JPanel();
dcKeyPanel.setLayout(new GridLayout(4, 6, 2, 2));
for (int i = 0; i < 24; i++) {
JButton b = new JButton(dcKey[i]);
dcKeyPanel.add(b);
b.addActionListener(this);
}
Container ct = this.getContentPane();
ct.add(resultTextPanel, BorderLayout.NORTH);
ct.add(controlPanel, BorderLayout.CENTER);
ct.add(dcKeyPanel, BorderLayout.SOUTH);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
}
@Override
public void actionPerformed(ActionEvent e) {
actionSource = e.getActionCommand();// 返回事件源的 字符串
String text = txfResult.getText();
// BackSpace事件响应
if (actionSource.equals("BackSpace")) {
if (text.length() > 1) {
txfResult.setText(text.substring(0, text.length() - 1));
} else if (text.length() == 1) {
txfResult.setText("0");
isNewNumber = true;
}
}
// CE事件响应
if (actionSource.equals("CE")) {
txfResult.setText("0");
isNewNumber = true;
option = 0;
}
// C事件响应
if (actionSource.equals("C")) {
txfResult.setText("0");
isNewNumber = true;
}
// 数字键响应
if (actionSource.equals("0") || actionSource.equals("1")
|| actionSource.equals("2") || actionSource.equals("3")
|| actionSource.equals("4") || actionSource.equals("5")
|| actionSource.equals("6") || actionSource.equals("7")
|| actionSource.equals("8") || actionSource.equals("9")) {
if (isNewNumber) {
txfResult.setText(actionSource);
isNewNumber = false;
} else {
txfResult.setText(text + actionSource);
}
}
// 小数点响应
if (actionSource.equals(".")) {
if (isNewNumber) {
txfResult.setText("0.");
} else {
txfResult.setText(text + ".");
}
isNewNumber = false;
}
// 正负号响应
if (actionSource.equals("+/-")) {
String str = txfResult.getText();
if (str.startsWith("-")) {
txfResult.setText(str.substring(1));
} else {
txfResult.setText("-" + str);
}
}
if (actionSource.equals("+") || actionSource.equals("-")
|| actionSource.equals("*") || actionSource.equals("/")
|| actionSource.equals("=")) {
// 判断上次的输入是不是运算符号,如果是运算符号,那么,不进行运算,只改变符号代表值option
if (lastActionSource.equals("+") || lastActionSource.equals("-")
|| lastActionSource.equals("*")
|| lastActionSource.equals("/")
|| lastActionSource.equals("=")) {
// System.out.println(1);
} else {
if (option == 0) {
d1 = Double.parseDouble(txfResult.getText());
isNewNumber = true;
} else {
cal(option);
}
}
option = changeOption(actionSource);
}
//求平方根
if (actionSource.equals("sqrt")) {
double d = Double.parseDouble(txfResult.getText());
if (d >= 0) {
txfResult.setText(String.valueOf(Math.sqrt(d)));
} else {
txfResult.setText("负数不能平方根");
}
}
// 求倒数
if (actionSource.equals("1/x")) {
double d = Double.parseDouble(txfResult.getText());
if (d != 0) {
txfResult.setText(String.valueOf(1 / d));
} else {
txfResult.setText("0不能取倒数");
}
}
// *0.01
if (actionSource.equals("%")) {
double d = Double.parseDouble(txfResult.getText());
txfResult.setText(String.valueOf(0.01 * d));
}
// 清除记忆数
if (actionSource.equals("MC")) {
d2 = 0;
txfM.setText("");
isNewNumber = true;
}
// 显示记忆数
if (actionSource.equals("MR")) {
txfResult.setText("" + d2);
isNewNumber = true;
}
// 保存当前数
if (actionSource.equals("MS")) {
d2 = Double.parseDouble(txfResult.getText());
txfM.setText("M");
isNewNumber = true;
}
// 与记忆数相加
if (actionSource.equals("M+")) {
d2 += Double.parseDouble(txfResult.getText());
txfM.setText("M");
isNewNumber = true;
}
// 事件响应结束,保存此次的响应事件源,供下次使用,主要是用来判断两次的输入是否都是运算符+-*/
lastActionSource = actionSource;
}
// 计算方法
private void cal(int option) {
switch (option) {
case 1:
d1 = d1 + Double.parseDouble(txfResult.getText());
break;
case 2:
d1 -= Double.parseDouble(txfResult.getText());
break;
case 3:
d1 *= Double.parseDouble(txfResult.getText());
break;
case 4:
d1 /= Double.parseDouble(txfResult.getText());
break;
}
txfResult.setText(String.valueOf(d1));
isNewNumber = true;
}
// 改变符号代表符option
private int changeOption(String str) {
if (actionSource.equals("+")) {
return 1;
} else if (actionSource.equals("-")) {
return 2;
} else if (actionSource.equals("*")) {
return 3;
} else if (actionSource.equals("/")) {
return 4;
} else if (actionSource.equals("=")) {
return 0;
}
return 0;
}
public static void main(String args[]) {
new Calculator();
}
}