/***
* author: zsui2354
* date : 2024.5.9
*/
package Calculator;
import org.w3c.dom.Text;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
public class Calculator extends JFrame {
/*标签*/ JLabel Display1;
/**
* 设计按钮
*/
/*按钮*/ JButton number1,number2,number3,number4,number5,number6,number7,number8,number9,number0
, Add //加
, Subtract //减
, Multiplication //乘
, Division //除
, Delete //删除
, Clear //清除
, Submit //提交
;
/*文框*/ JTextField
real_timeText = new JTextField(5),
output = new JTextField(0);
/*容器*/ JPanel
Fixed_layout = new JPanel(), //窗口内底层容器
Text_container = new JPanel(), //北部 文本框容器
Text_container2 = new JPanel(), //南部 文本框容器
Button_container = new JPanel(), //按钮容器
ButtonLine1_container = new JPanel(), //按钮行列1
ButtonLine2_container = new JPanel(), //按钮行列2
ButtonLine3_container = new JPanel(), //按钮行列3
ButtonLine4_container = new JPanel(); //按钮行列3
// ScriptEngine engine; //脚本引擎
String[] Ssuanshi_save = new String[100];
FlowLayout fl = new FlowLayout(); //流式布局
BorderLayout Bl = new BorderLayout(); //四周布局
Calculator(){
// ScriptEngineManager manager = new ScriptEngineManager();
// engine = manager.getEngineByName("JavaScript");
Display1 = new JLabel("计算后的结果:");
number1= new JButton("1")
; number2= new JButton("2")
; number3= new JButton("3")
; number4= new JButton("4")
; number5= new JButton("5")
; number6= new JButton("6")
; number7= new JButton("7")
; number8= new JButton("8")
; number9= new JButton("9")
; number0= new JButton("0")
; Add = new JButton("+")
; Subtract = new JButton("-")
; Multiplication = new JButton("*")
; Division = new JButton("/")
; Delete = new JButton("<-")
; Clear = new JButton("C")
; Submit = new JButton("计算")
;
Font buttonFont = new Font("宋体", Font.PLAIN, 21); //批量设置按钮字体和大小
number1.setFont(buttonFont);
number2.setFont(buttonFont);
number3.setFont(buttonFont);
number4.setFont(buttonFont);
number5.setFont(buttonFont);
number6.setFont(buttonFont);
number7.setFont(buttonFont);
number8.setFont(buttonFont);
number9.setFont(buttonFont);
number0.setFont(buttonFont);
Add.setFont(buttonFont);
Subtract.setFont(buttonFont);
Multiplication.setFont(buttonFont);
Division.setFont(buttonFont);
Delete.setFont(buttonFont);
Clear.setFont(buttonFont);
Submit.setFont(buttonFont);
Font textFont = new Font("宋体", Font.PLAIN, 27);
real_timeText.setFont(textFont);
output.setFont(textFont);
number1.setPreferredSize(new Dimension(50, 50)); //批量设置按钮 尺寸
number2.setPreferredSize(new Dimension(50, 50));
number3.setPreferredSize(new Dimension(50, 50));
number4.setPreferredSize(new Dimension(50, 50));
number5.setPreferredSize(new Dimension(50, 50));
number6.setPreferredSize(new Dimension(50, 50));
number7.setPreferredSize(new Dimension(50, 50));
number8.setPreferredSize(new Dimension(50, 50));
number9.setPreferredSize(new Dimension(50, 50));
number0.setPreferredSize(new Dimension(50, 50));
Add.setPreferredSize(new Dimension(50, 50));
Subtract.setPreferredSize(new Dimension(50, 50));
Multiplication.setPreferredSize(new Dimension(50, 50));
Division.setPreferredSize(new Dimension(50, 50));
Delete.setPreferredSize(new Dimension(50, 50));
Clear.setPreferredSize(new Dimension(50, 50));
ButtonListener listener = new ButtonListener();// 批量监听按钮
number1.addActionListener(listener);
number2.addActionListener(listener);
number3.addActionListener(listener);
number4.addActionListener(listener);
number5.addActionListener(listener);
number6.addActionListener(listener);
number7.addActionListener(listener);
number8.addActionListener(listener);
number9.addActionListener(listener);
number0.addActionListener(listener);
Add.addActionListener(listener);
Subtract.addActionListener(listener);
Multiplication.addActionListener(listener);
Division.addActionListener(listener);
Delete.addActionListener(listener);
Clear.addActionListener(listener);
Submit.addActionListener(listener);
super.setLayout(Bl);
super.add(Fixed_layout); //底层四周布局
Fixed_layout.add(Text_container, BorderLayout.NORTH);
Text_container.setLayout(new BorderLayout()); //将Text_container 定为 四周布局
Text_container.add(real_timeText, BorderLayout.CENTER); //将real_timeText 添加进Text_container 中心
super.add(Text_container, BorderLayout.NORTH); // 添加到窗口的上半部分
real_timeText.setPreferredSize(new Dimension(0, 50)); //设置文本框的宽高
/**
* 由于 现代计算器的UI排版我想尽可能的贴近 ,我想用四周型布局,其实是不熟悉网格布局,先用这个凑合凑合得了 :)
*/ Button_container.setLayout(new BorderLayout()); //Button_container 定为 四周布局
super.add(Button_container, BorderLayout.CENTER); //将Button_container 放置 中心
ButtonLine1_container.setLayout(new FlowLayout()); //将按钮行列 设置 流式布局
ButtonLine2_container.setLayout(new FlowLayout());
ButtonLine3_container.setLayout(new FlowLayout());
ButtonLine1_container.add(number1);
ButtonLine1_container.add(number2);
ButtonLine1_container.add(number3);
ButtonLine1_container.add(number4);
ButtonLine1_container.add(number5);
Button_container.add(ButtonLine1_container, BorderLayout.NORTH); //将 ButtonLine1_container 放置 Button_container 北
ButtonLine2_container.add(number6);
ButtonLine2_container.add(number7);
ButtonLine2_container.add(number8);
ButtonLine2_container.add(number9);
ButtonLine2_container.add(number0);
Button_container.add(ButtonLine2_container, BorderLayout.CENTER); //将 ButtonLine2_container 放置 Button_container 中心
ButtonLine3_container.add(Clear);
ButtonLine3_container.add(Division);
ButtonLine3_container.add(Multiplication);
ButtonLine3_container.add(Subtract);
ButtonLine3_container.add(Add);
Button_container.add(ButtonLine3_container, BorderLayout.SOUTH); //将 ButtonLine3_container 放置 Button_container 南
Text_container2.setLayout(new BorderLayout()); //将Text_container2 定为 四周布局
Text_container2.add(output, BorderLayout.CENTER); //将output 添加进Text_container2 中心
//Text_container2.add(Display1, BorderLayout.NORTH); //将 Display1 添加进Text_container 北
Text_container2.add(Submit, BorderLayout.NORTH); //将计算 按钮添加至 Text_container 北,它其实将 Display1 覆盖住了
super.add(Text_container2, BorderLayout.SOUTH); // 添加到窗口的下半部分
output.setPreferredSize(new Dimension(0, 50)); //设置 计算结果 文本框的宽高
//real_timeText.setEditable(false); 我觉得这里其实可以让 用户编辑 用户也可以用键盘控制计算,而不局限于按键
output.setEditable(false); //禁止编辑计算结果
super.setTitle("王果冻的小计算器");
super.setBounds(100,100,350,353);
super.setVisible(true);
super.setResizable(false); //禁用窗口大小的随意调整
}
class ButtonListener implements ActionListener { //监听
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command) {
case "1":
String Text = real_timeText.getText();
String newText = Text + "1";
real_timeText.setText(newText);
break;
case "2":
String Text1 = real_timeText.getText();
String newText1 = Text1 + "2";
real_timeText.setText(newText1);
break;
case "3":
String Text3 = real_timeText.getText();
String newText3 = Text3 + "3";
real_timeText.setText(newText3);
break;
case "4":
String Text4 = real_timeText.getText();
String newText4 = Text4 + "4";
real_timeText.setText(newText4);
break;
case "5":
String Text5 = real_timeText.getText();
String newText5 = Text5 + "5";
real_timeText.setText(newText5);
break;
case "6":
String Text6 = real_timeText.getText();
String newText6 = Text6 + "6";
real_timeText.setText(newText6);
break;
case "7":
String Text7 = real_timeText.getText();
String newText7 = Text7 + "7";
real_timeText.setText(newText7);
break;
case "8":
String Text8 = real_timeText.getText();
String newText8 = Text8 + "8";
real_timeText.setText(newText8);
break;
case "9":
String Text9 = real_timeText.getText();
String newText9 = Text9 + "9";
real_timeText.setText(newText9);
break;
case "0":
String Text0 = real_timeText.getText();
String newText0 = Text0 + "0";
real_timeText.setText(newText0);
break;
case "+":
String Text10 = real_timeText.getText();
String newText10 = Text10 + "+";
real_timeText.setText(newText10);
break;
case "-":
String Text11 = real_timeText.getText();
String newText11 = Text11 + "-";
real_timeText.setText(newText11);
break;
case "/":
String Text12 = real_timeText.getText();
String newText12 = Text12 + "/";
real_timeText.setText(newText12);
break;
case "*":
String Text13 = real_timeText.getText();
String newText13 = Text13 + "*";
real_timeText.setText(newText13);
break;
case "计算":
String Text14 = real_timeText.getText();
Ssuanshi_save = (new String[Text14.length()]);
for (int i=0; i<Ssuanshi_save.length; i++){
Ssuanshi_save[i] = String.valueOf(Text14.charAt(i));
}
for (int i=0; i<Ssuanshi_save.length; i++){
/***
* 试验性废弃代码
*/
// if (Ssuanshi_save[i].equals("*")){
// String str1 = "";
// for (int BL_1 = i; BL_1 > 0; BL_1--) {
// str1 = String.valueOf(Ssuanshi_save[BL_1 - 1]) + str1; // 使用 + 运算符进行字符串拼接
// }
// String str2 = "";
// for (int BL_2 = i; BL_2 > 0; BL_2--) {
// str2 = String.valueOf(Ssuanshi_save[BL_2 - 1]) + str2; // 使用 + 运算符进行字符串拼接
// }
if (Ssuanshi_save[i].equals("*")) {
// 从当前位置向左获取第一个数
StringBuilder str1Builder = new StringBuilder();
for (int BL_1 = i - 1; BL_1 >= 0; BL_1--) {
if (Ssuanshi_save[BL_1].matches("[0-9.]")) {
str1Builder.insert(0, Ssuanshi_save[BL_1]); // 使用 insert 方法在前面插入字符
} else {
break; // 遇到非数字字符,结束循环
}
}
String str1 = str1Builder.toString();
// 从当前位置向右获取第二个数字
StringBuilder str2Builder = new StringBuilder();
for (int BL_2 = i + 1; BL_2 < Ssuanshi_save.length; BL_2++) {
if (Ssuanshi_save[BL_2].matches("[0-9.]")) {
str2Builder.append(Ssuanshi_save[BL_2]); // 使用 append 方法在后面追加字符
} else {
break; // 遇到非数字字符,结束循环
}
}
String str2 = str2Builder.toString();
double operand1 = Double.parseDouble(str1);
double operand2 = Double.parseDouble(str2);
String out = String.valueOf(operand1 * operand2);//计算
output.setText(out);
}else if(Ssuanshi_save[i].equals("/")){
// 从当前位置向左获取第一个数
StringBuilder str1Builder = new StringBuilder();
for (int BL_1 = i - 1; BL_1 >= 0; BL_1--) {
if (Ssuanshi_save[BL_1].matches("[0-9.]")) {
str1Builder.insert(0, Ssuanshi_save[BL_1]); // 使用 insert 方法在前面插入字符
} else {
break; // 遇到非数字字符,结束循环
}
}
String str1 = str1Builder.toString();
// 从当前位置向右获取第二个数字
StringBuilder str2Builder = new StringBuilder();
for (int BL_2 = i + 1; BL_2 < Ssuanshi_save.length; BL_2++) {
if (Ssuanshi_save[BL_2].matches("[0-9.]")) {
str2Builder.append(Ssuanshi_save[BL_2]); // 使用 append 方法在后面追加字符
} else {
break; // 遇到非数字字符,结束循环
}
}
String str2 = str2Builder.toString();
double operand1 = Double.parseDouble(str1);
double operand2 = Double.parseDouble(str2);
String out = String.valueOf(operand1 / operand2);//计算
output.setText(out);
}else if(Ssuanshi_save[i].equals("+")){
// 从当前位置向左获取第一个数
StringBuilder str1Builder = new StringBuilder();
for (int BL_1 = i - 1; BL_1 >= 0; BL_1--) {
if (Ssuanshi_save[BL_1].matches("[0-9.]")) {
str1Builder.insert(0, Ssuanshi_save[BL_1]); // 使用 insert 方法在前面插入字符
} else {
break; // 遇到非数字字符,结束循环
}
}
String str1 = str1Builder.toString();
// 从当前位置向右获取第二个数字
StringBuilder str2Builder = new StringBuilder();
for (int BL_2 = i + 1; BL_2 < Ssuanshi_save.length; BL_2++) {
if (Ssuanshi_save[BL_2].matches("[0-9.]")) {
str2Builder.append(Ssuanshi_save[BL_2]); // 使用 append 方法在后面追加字符
} else {
break; // 遇到非数字字符,结束循环
}
}
String str2 = str2Builder.toString();
double operand1 = Double.parseDouble(str1);
double operand2 = Double.parseDouble(str2);
String out = String.valueOf(operand1 + operand2);//计算
output.setText(out);
}else if(Ssuanshi_save[i].equals("-")){
// 从当前位置向左获取第一个数
StringBuilder str1Builder = new StringBuilder();
for (int BL_1 = i - 1; BL_1 >= 0; BL_1--) {
if (Ssuanshi_save[BL_1].matches("[0-9.]")) {
str1Builder.insert(0, Ssuanshi_save[BL_1]); // 使用 insert 方法在前面插入字符
} else {
break; // 遇到非数字字符,结束循环
}
}
String str1 = str1Builder.toString();
// 从当前位置向右获取第二个数字
StringBuilder str2Builder = new StringBuilder();
for (int BL_2 = i + 1; BL_2 < Ssuanshi_save.length; BL_2++) {
if (Ssuanshi_save[BL_2].matches("[0-9.]")) {
str2Builder.append(Ssuanshi_save[BL_2]); // 使用 append 方法在后面追加字符
} else {
break; // 遇到非数字字符,结束循环
}
}
String str2 = str2Builder.toString();
double operand1 = Double.parseDouble(str1);
double operand2 = Double.parseDouble(str2);
String out = String.valueOf(operand1 - operand2);//计算
output.setText(out);
}
}
break;
case "C": //这里的C代表清除 是Clear的首字母
real_timeText.setText("");
output.setText("");
break;
}
}
}
/***
* 本来想用下面 Script engine 这个 calculateExpression 处理计算 ,发现用时空指针, 可能是初始化 或 环境不完整 ,而引起的 空指针异常
* 感到有点 烦躁 ,我弃用了它,将其注释
* author:王果冻
*/
// public double calculateExpression(String expression) throws ScriptException {
// return (double) engine.eval(expression);
// }
public static void main(String[] args) {
Calculator OBJ = new Calculator();
}
}