|
|
@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
package computer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
|
|
|
public class a {
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
|
|
JS win = new JS();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class JS extends JFrame implements ActionListener {
|
|
|
|
|
|
|
|
private StringBuilder sBuilder = new StringBuilder(); // 利用StringBuilder类来显示,以及区分两个操作数
|
|
|
|
|
|
|
|
JTextArea text = new JTextArea();
|
|
|
|
|
|
|
|
double a, b;
|
|
|
|
|
|
|
|
Double sum;
|
|
|
|
|
|
|
|
String lastOperator = "=";
|
|
|
|
|
|
|
|
public JS() {
|
|
|
|
|
|
|
|
setBounds(100, 100, 400, 400);
|
|
|
|
|
|
|
|
setTitle("计算器");
|
|
|
|
|
|
|
|
JMenuBar menubar = new JMenuBar(); // 创建菜单条
|
|
|
|
|
|
|
|
this.setJMenuBar(menubar); // 将设置好的菜单条放在窗口中
|
|
|
|
|
|
|
|
this.setLayout(new BorderLayout()); // 添加文本框
|
|
|
|
|
|
|
|
JPanel p1 = new JPanel();
|
|
|
|
|
|
|
|
JPanel p2 = new JPanel();
|
|
|
|
|
|
|
|
text.setPreferredSize(new Dimension(370, 60)); // 设置组件大小
|
|
|
|
|
|
|
|
p2.setLayout(new FlowLayout());
|
|
|
|
|
|
|
|
p1.add(text);
|
|
|
|
|
|
|
|
this.add(p1, BorderLayout.NORTH);
|
|
|
|
|
|
|
|
p2.setLayout(new GridLayout(5, 4)); // 添加按钮
|
|
|
|
|
|
|
|
JButton button[] = new JButton[20];
|
|
|
|
|
|
|
|
button[0] = new JButton("C"); // 清空
|
|
|
|
|
|
|
|
button[1] = new JButton("CE"); // 清除之前输入的一个数据
|
|
|
|
|
|
|
|
button[2] = new JButton("%"); // 取余
|
|
|
|
|
|
|
|
button[3] = new JButton("÷"); // 除法
|
|
|
|
|
|
|
|
button[4] = new JButton("7");
|
|
|
|
|
|
|
|
button[5] = new JButton("8");
|
|
|
|
|
|
|
|
button[6] = new JButton("9");
|
|
|
|
|
|
|
|
button[7] = new JButton("x"); // 乘号
|
|
|
|
|
|
|
|
button[8] = new JButton("4");
|
|
|
|
|
|
|
|
button[9] = new JButton("5");
|
|
|
|
|
|
|
|
button[10] = new JButton("6");
|
|
|
|
|
|
|
|
button[11] = new JButton("-"); // 减号
|
|
|
|
|
|
|
|
button[12] = new JButton("1");
|
|
|
|
|
|
|
|
button[13] = new JButton("2");
|
|
|
|
|
|
|
|
button[14] = new JButton("3");
|
|
|
|
|
|
|
|
button[15] = new JButton("+"); // 加号
|
|
|
|
|
|
|
|
button[16] = new JButton(" "); // 空格
|
|
|
|
|
|
|
|
button[17] = new JButton("0");
|
|
|
|
|
|
|
|
button[18] = new JButton("."); // 小数点
|
|
|
|
|
|
|
|
button[19] = new JButton("="); // 等于
|
|
|
|
|
|
|
|
for (int i = 0; i < button.length; i++) // 将组件加入容器
|
|
|
|
|
|
|
|
p2.add(button[i]);
|
|
|
|
|
|
|
|
p2.add(button[19]);
|
|
|
|
|
|
|
|
add(p2, BorderLayout.CENTER);
|
|
|
|
|
|
|
|
for (int i = 0; i < button.length; i++) // 为每一个事件(按钮)添加监视器
|
|
|
|
|
|
|
|
button[i].addActionListener(this);
|
|
|
|
|
|
|
|
this.setVisible(true);
|
|
|
|
|
|
|
|
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 点击X号可以关闭程序
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void actionPerformed(ActionEvent e) // 事件处理
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
String lab = e.getActionCommand(); // 得到当前的按钮字符,后面进行匹配
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Character.isDigit(lab.charAt(0)) || lab.equals(".")) { // 处理按钮 0~9 和小数点
|
|
|
|
|
|
|
|
sBuilder.append(lab); // 利用StringBuilder类的对象调用方法,将数据存起来
|
|
|
|
|
|
|
|
text.setText(sBuilder.toString()); // 显示之前所有输入的数据
|
|
|
|
|
|
|
|
} else if (lab.equals("CE")) { // 处理"CE"按钮事件 即清除当前的输入的数据
|
|
|
|
|
|
|
|
sBuilder.deleteCharAt(sBuilder.length() - 1); // StringBuilder的实例化对象调用方法下标减1即可
|
|
|
|
|
|
|
|
text.setText(sBuilder.toString());
|
|
|
|
|
|
|
|
} else if (lab.equals("C")) { // 处理"C"按钮事件 即清除之前的输入所有的数据
|
|
|
|
|
|
|
|
sBuilder.setLength(0); // 重新创建即可清除之前的输入所有的数据
|
|
|
|
|
|
|
|
text.setText("");
|
|
|
|
|
|
|
|
} else if (lab.equals("+") || lab.equals("-") || lab.equals("x") || lab.equals("÷") || lab.equals("%")) { // 处理运算符按钮事件
|
|
|
|
|
|
|
|
if (sBuilder.length() > 0) {
|
|
|
|
|
|
|
|
a = Double.parseDouble(sBuilder.toString()); // 将前面的输入的数 作为第一个操作数a
|
|
|
|
|
|
|
|
sBuilder.setLength(0); // 将数组清空来存储第二个操作数 b
|
|
|
|
|
|
|
|
lastOperator = lab;
|
|
|
|
|
|
|
|
text.setText("" + a + " " + lastOperator + " ");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (lab.equals("=")) { // 处理"="按钮事件
|
|
|
|
|
|
|
|
if (lastOperator != null && !lastOperator.isEmpty() && sBuilder.length() > 0) {
|
|
|
|
|
|
|
|
b = Double.parseDouble(sBuilder.toString()); // 一遇到"=",我们就能得到第二个操作数b
|
|
|
|
|
|
|
|
calculate(); // 调用计算方法
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (lab.equals(" ")) { // 处理空格按钮事件
|
|
|
|
|
|
|
|
// 空格按键无功能
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void calculate() {
|
|
|
|
|
|
|
|
switch (lastOperator) {
|
|
|
|
|
|
|
|
case "+":
|
|
|
|
|
|
|
|
sum = a + b;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "-":
|
|
|
|
|
|
|
|
sum = a - b;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "x":
|
|
|
|
|
|
|
|
sum = a * b;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "÷":
|
|
|
|
|
|
|
|
if (b == 0) {
|
|
|
|
|
|
|
|
text.setText("除数不能为0");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sum = a / b;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "%":
|
|
|
|
|
|
|
|
if (b == 0) {
|
|
|
|
|
|
|
|
text.setText("除数不能为0");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sum = a % b;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
text.setText(sum + "");
|
|
|
|
|
|
|
|
sBuilder.setLength(0);
|
|
|
|
|
|
|
|
sBuilder.append(sum);
|
|
|
|
|
|
|
|
lastOperator = "=";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|