From b919f69dd4aa62af23f3f82ba664fba61cfdd7e6 Mon Sep 17 00:00:00 2001 From: beixiang123456 <1850174958@qq.com> Date: Wed, 9 Oct 2024 11:11:36 +0800 Subject: [PATCH 1/7] Calculator --- Calculator.java | 83 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 Calculator.java diff --git a/Calculator.java b/Calculator.java new file mode 100644 index 0000000..6ad4096 --- /dev/null +++ b/Calculator.java @@ -0,0 +1,83 @@ +package hello.demo; +import javafx.application.Application; +import javafx.geometry.Insets; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.TextField; +import javafx.scene.layout.GridPane; +import javafx.stage.Stage; + +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; + +public class Calculator extends Application { + + private TextField display; + + @Override + public void start(Stage primaryStage) { + primaryStage.setTitle("计算器"); + + display = new TextField(); + display.setEditable(false); + + GridPane grid = new GridPane(); + grid.setPadding(new Insets(10)); + grid.setVgap(5); + grid.setHgap(5); + + // 按钮定义,包括取余运算 "%" + String[] buttons = { + "7", "8", "9", "/", + "4", "5", "6", "*", + "1", "2", "3", "-", + "0", "C", "=", "+", + "%" + }; + + int index = 0; + for (int row = 0; row < 5; row++) { // 修改行数以添加新按钮 + for (int col = 0; col < 4; col++) { + if (index >= buttons.length) break; // 防止数组越界 + Button btn = new Button(buttons[index]); + btn.setMinSize(50, 50); + btn.setOnAction(e -> handleButtonClick(btn.getText())); + grid.add(btn, col, row); + index++; + } + } + + grid.add(display, 0, 5, 4, 1); // 显示区域在最后一行 + + Scene scene = new Scene(grid, 250, 350); + primaryStage.setScene(scene); + primaryStage.show(); + } + + private void handleButtonClick(String text) { + if ("C".equals(text)) { + display.clear(); + } else if ("=".equals(text)) { + calculateResult(); + } else { + display.appendText(text); + } + } + + private void calculateResult() { + try { + String input = display.getText(); + // 使用脚本引擎执行表达式 + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine engine = manager.getEngineByName("JavaScript"); + Object result = engine.eval(input); + display.setText(result.toString()); + } catch (Exception e) { + display.setText("错误"); + } + } + + public static void main(String[] args) { + launch(args); + } +} -- 2.34.1 From 25994c81614333b1e6b9ca5b6472735039fd9b2d Mon Sep 17 00:00:00 2001 From: fdzcxy212202301 <1850174958@qq.com> Date: Wed, 9 Oct 2024 11:12:36 +0800 Subject: [PATCH 2/7] Initial commit --- README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..d1774f0 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# Calculator1 + -- 2.34.1 From d96b52213eeab761c0e44abcd4a2d3c5d9bb6c98 Mon Sep 17 00:00:00 2001 From: beixiang123456 <1850174958@qq.com> Date: Wed, 9 Oct 2024 11:22:14 +0800 Subject: [PATCH 3/7] Calculator1 --- Calculator1.java | 288 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 288 insertions(+) create mode 100644 Calculator1.java diff --git a/Calculator1.java b/Calculator1.java new file mode 100644 index 0000000..1983574 --- /dev/null +++ b/Calculator1.java @@ -0,0 +1,288 @@ +package hello.demo; + +import javafx.application.Application; +import javafx.geometry.Insets; +import javafx.geometry.Pos; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.TextField; +import javafx.scene.input.KeyCode; +import javafx.scene.layout.GridPane; +import javafx.stage.Stage; + +public class Calculator1 extends Application { + + // 定义变量,用于存储数值和操作符 + private double num1 = 0;// + private double num2 = 0; + private String operator = "";//运算符 + private boolean decimal = false;//小数点 + private boolean newCalculation = true; + + @Override + public void start(Stage primaryStage) { + // 设置窗口标题 + primaryStage.setTitle("Calculator"); + + // 创建一个网格面板,并进行布局 + GridPane grid = new GridPane(); + grid.setAlignment(Pos.CENTER);//位置居中 + // 设置水平和垂直间距为 10 + grid.setHgap(10); + grid.setVgap(10); + //设置网格面板的填充为 30(可以删除) + grid.setPadding(new Insets(30, 30, 30, 30)); + // 创建一个文本框,用于显示计算结果 + TextField display = new TextField(); + display.setEditable(false);//结果是显示栏不可编辑 + + // 设置文本框的水平对齐方式为右对齐,并将其宽度设置为 200 + display.setAlignment(Pos.CENTER_RIGHT); + display.setPrefWidth(200); + grid.add(display, 0, 0, 4, 1);//将文本框添加到网格面板中,占据第 0 列、第 0 行,跨度为 4 行和 1 列 + + Button btn1 = new Button("1");//创建按钮1 + // 将数字按钮1添加到网格面板中 + btn1.setOnAction(event -> handleNumberButton("1", display)); + grid.add(btn1, 0, 1);//位置占0列1行 + + Button btn2 = new Button("2"); + btn2.setOnAction(event -> handleNumberButton("2", display)); + grid.add(btn2, 1, 1); + + Button btn3 = new Button("3"); + btn3.setOnAction(event -> handleNumberButton("3", display)); + grid.add(btn3, 2, 1); + + Button btn4 = new Button("4"); + btn4.setOnAction(event -> handleNumberButton("4", display)); + grid.add(btn4, 0, 2); + + Button btn5 = new Button("5"); + btn5.setOnAction(event -> handleNumberButton("5", display)); + grid.add(btn5, 1, 2); + + Button btn6 = new Button("6"); + btn6.setOnAction(event -> handleNumberButton("6", display)); + grid.add(btn6, 2, 2); + + Button btn7 = new Button("7"); + btn7.setOnAction(event -> handleNumberButton("7", display)); + grid.add(btn7, 0, 3); + + Button btn8 = new Button("8"); + btn8.setOnAction(event -> handleNumberButton("8", display)); + grid.add(btn8, 1, 3); + + Button btn9 = new Button("9"); + btn9.setOnAction(event -> handleNumberButton("9", display)); + grid.add(btn9, 2, 3); + + Button btn0 = new Button("0"); + btn0.setOnAction(event -> handleNumberButton("0", display)); + grid.add(btn0, 1, 4); + + Button btnDecimal = new Button("."); + btnDecimal.setOnAction(event -> handleDecimalButton(display)); + grid.add(btnDecimal, 2, 4); + + // 创建运算符按钮,并将其添加到网格面板中 + Button btnAdd = new Button("+"); + btnAdd.setOnAction(event -> handleOperatorButton("+", display)); + grid.add(btnAdd, 3, 1); + + Button btnSubtract = new Button("-"); + btnSubtract.setOnAction(event -> handleOperatorButton("-", display)); + grid.add(btnSubtract, 3, 2); + + Button btnMultiply = new Button("*"); + btnMultiply.setOnAction(event -> handleOperatorButton("*", display)); + grid.add(btnMultiply, 3, 3); + + Button btnDivide = new Button("/"); + btnDivide.setOnAction(event -> handleOperatorButton("/", display)); + grid.add(btnDivide, 3, 4); + + Button btnClear = new Button("C"); + btnClear.setOnAction(event -> handleClearButton(display)); + grid.add(btnClear, 0, 4); + + Button btnSign = new Button("+/-"); + btnSign.setOnAction(event -> handleSignButton(display)); + grid.add(btnSign, 0, 5); + + Button btnPercent = new Button("%"); + btnPercent.setOnAction(event -> handlePercentButton(display)); + grid.add(btnPercent, 1, 5); + + Button btnEquals = new Button("="); + btnEquals.setOnAction(e -> handleEqualsButton(display)); + grid.add(btnEquals, 3, 5); + + // 创建一个场景,并将网格面板添加到其中 + Scene scene = new Scene(grid, 240, 250); + primaryStage.setScene(scene); + + // 监听键盘事件 + scene.setOnKeyPressed(event -> { + KeyCode keyCode = event.getCode(); + if (keyCode.isDigitKey()) { + handleNumberButton(keyCode.getName(), display); + } else if (keyCode == KeyCode.ADD || keyCode == KeyCode.PLUS) { + handleOperatorButton("+", display); + } else if (keyCode == KeyCode.SUBTRACT || keyCode == KeyCode.MINUS) { + handleOperatorButton("-", display); + } else if (keyCode == KeyCode.MULTIPLY) { + handleOperatorButton("*", display); + } else if (keyCode == KeyCode.DIVIDE) { + handleOperatorButton("/", display); + } else if (keyCode == KeyCode.PERIOD) { + handleDecimalButton(display); + } else if (keyCode == KeyCode.ENTER) { + handleEqualsButton(display); + } + }); + + // 显示窗口 + primaryStage.show(); + } + + /** + * 处理数字按钮的事件 + * + * @param number 按下的数字 + * @param display 显示结果的文本框 + */ + private void handleNumberButton(String number, TextField display) { + if (newCalculation) { + display.clear();//清除文本框 + newCalculation = false; + } + String text = display.getText(); + if (text.equals("0")) { + display.setText(number); + } else if (!decimal || !text.contains(".")) { + display.setText(text + number); + } + } + + /** + * 处理运算符按钮的事件 + * @param operator 按下的运算符号 + * @param display 显示结果的文本框 + */ + private void handleOperatorButton(String operator, TextField display) { + if (!this.operator.equals("")) {//判断当前计算器是否已经有运算符被按下,如果有,则调用 handleEqualsButton 方法进行计算 + handleEqualsButton(display); + } + num1 = Double.parseDouble(display.getText()); + this.operator = operator; + display.setText(num1 + " " + operator + " "); + decimal = false; + newCalculation = false; + } + + /** + * 处理小数点按钮的事件 + * + * @param display 显示结果的文本框 + */ + private void handleDecimalButton(TextField display) { + if (!decimal) { + String text = display.getText(); + if (newCalculation) { + display.clear(); + newCalculation = false; + } + display.setText(text + "."); + decimal = true; + } + } + + /** + * 处理清除按钮的事件 + * @param display 显示结果的文本框 + */ + private void handleClearButton(TextField display) { + display.clear(); + num1 = 0; + num2 = 0; + operator = ""; + decimal = false; + newCalculation = true; + } + + /** + * 处理正负号按钮的事件 + * + * @param display 显示结果的文本框 + */ + private void handleSignButton(TextField display) { + String text = display.getText();//获取文本框中的数字或运算符 + if (!text.equals("") && !text.equals("0")) { + if (text.startsWith("-")) { + display.setText(text.substring(1)); + } else { + display.setText("-" + text); + } + } + } + + /** + * 处理百分号按钮的事件 + * + * @param display 显示结果的文本框 + */ + private void handlePercentButton(TextField display) { + double num = Double.parseDouble(display.getText()); + display.setText(Double.toString(num / 100)); + newCalculation = true; + } + + /** + * 处理等于按钮的事件 + * + * @param display 显示结果的文本框 + */ + private void handleEqualsButton(TextField display) { + if (!operator.equals("")) { + String text = display.getText(); + int index = text.indexOf(operator); + num2 = Double.parseDouble(text.substring(index + operator.length())); + switch (operator) { + case "+": + num1 += num2; + break; + case "-": + num1 -= num2; + break; + case "*": + num1 *= num2; + break; + case "/": + if (num2 == 0) { + display.setText("除零错误"); + num1 = 0; + num2 = 0; + operator = ""; + decimal = false; + newCalculation = true; + return; + } + num1 /= num2; + break; + } + //重置计算器为起始状态 + display.setText(Double.toString(num1));//输入框没有字符则为第一个操作数 + operator = "";//无操作数 + decimal = false;//没有输入小数点 + newCalculation = true;//开始新的一次计算 + } + } + + public static void main(String[] args) { + // 启动 JavaFX 应用程序 + launch(args); + } + +} \ No newline at end of file -- 2.34.1 From e99b5a03997ce14acbe03f66d01bf1b6adbf51b2 Mon Sep 17 00:00:00 2001 From: beixiang123456 <1850174958@qq.com> Date: Wed, 9 Oct 2024 11:27:06 +0800 Subject: [PATCH 4/7] Calculator2 --- Test.java | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Test.java diff --git a/Test.java b/Test.java new file mode 100644 index 0000000..76c9b61 --- /dev/null +++ b/Test.java @@ -0,0 +1,11 @@ +package hello.demo; + +import java.util.Scanner; + +public class Test { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.println("请输入你想要选择的算法:"); + + } +} -- 2.34.1 From cb05d778296493f736d357e983bfe75f5fa88aec Mon Sep 17 00:00:00 2001 From: fdzcxy212202301 <1850174958@qq.com> Date: Wed, 9 Oct 2024 11:38:43 +0800 Subject: [PATCH 5/7] Delete 'Test.java' --- Test.java | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 Test.java diff --git a/Test.java b/Test.java deleted file mode 100644 index 76c9b61..0000000 --- a/Test.java +++ /dev/null @@ -1,11 +0,0 @@ -package hello.demo; - -import java.util.Scanner; - -public class Test { - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - System.out.println("请输入你想要选择的算法:"); - - } -} -- 2.34.1 From 9bab71cea674442f6bca930129e4492ed931af1a Mon Sep 17 00:00:00 2001 From: fdzcxy212202301 <1850174958@qq.com> Date: Wed, 9 Oct 2024 11:39:51 +0800 Subject: [PATCH 6/7] Delete 'Calculator1.java' --- Calculator1.java | 288 ----------------------------------------------- 1 file changed, 288 deletions(-) delete mode 100644 Calculator1.java diff --git a/Calculator1.java b/Calculator1.java deleted file mode 100644 index 1983574..0000000 --- a/Calculator1.java +++ /dev/null @@ -1,288 +0,0 @@ -package hello.demo; - -import javafx.application.Application; -import javafx.geometry.Insets; -import javafx.geometry.Pos; -import javafx.scene.Scene; -import javafx.scene.control.Button; -import javafx.scene.control.TextField; -import javafx.scene.input.KeyCode; -import javafx.scene.layout.GridPane; -import javafx.stage.Stage; - -public class Calculator1 extends Application { - - // 定义变量,用于存储数值和操作符 - private double num1 = 0;// - private double num2 = 0; - private String operator = "";//运算符 - private boolean decimal = false;//小数点 - private boolean newCalculation = true; - - @Override - public void start(Stage primaryStage) { - // 设置窗口标题 - primaryStage.setTitle("Calculator"); - - // 创建一个网格面板,并进行布局 - GridPane grid = new GridPane(); - grid.setAlignment(Pos.CENTER);//位置居中 - // 设置水平和垂直间距为 10 - grid.setHgap(10); - grid.setVgap(10); - //设置网格面板的填充为 30(可以删除) - grid.setPadding(new Insets(30, 30, 30, 30)); - // 创建一个文本框,用于显示计算结果 - TextField display = new TextField(); - display.setEditable(false);//结果是显示栏不可编辑 - - // 设置文本框的水平对齐方式为右对齐,并将其宽度设置为 200 - display.setAlignment(Pos.CENTER_RIGHT); - display.setPrefWidth(200); - grid.add(display, 0, 0, 4, 1);//将文本框添加到网格面板中,占据第 0 列、第 0 行,跨度为 4 行和 1 列 - - Button btn1 = new Button("1");//创建按钮1 - // 将数字按钮1添加到网格面板中 - btn1.setOnAction(event -> handleNumberButton("1", display)); - grid.add(btn1, 0, 1);//位置占0列1行 - - Button btn2 = new Button("2"); - btn2.setOnAction(event -> handleNumberButton("2", display)); - grid.add(btn2, 1, 1); - - Button btn3 = new Button("3"); - btn3.setOnAction(event -> handleNumberButton("3", display)); - grid.add(btn3, 2, 1); - - Button btn4 = new Button("4"); - btn4.setOnAction(event -> handleNumberButton("4", display)); - grid.add(btn4, 0, 2); - - Button btn5 = new Button("5"); - btn5.setOnAction(event -> handleNumberButton("5", display)); - grid.add(btn5, 1, 2); - - Button btn6 = new Button("6"); - btn6.setOnAction(event -> handleNumberButton("6", display)); - grid.add(btn6, 2, 2); - - Button btn7 = new Button("7"); - btn7.setOnAction(event -> handleNumberButton("7", display)); - grid.add(btn7, 0, 3); - - Button btn8 = new Button("8"); - btn8.setOnAction(event -> handleNumberButton("8", display)); - grid.add(btn8, 1, 3); - - Button btn9 = new Button("9"); - btn9.setOnAction(event -> handleNumberButton("9", display)); - grid.add(btn9, 2, 3); - - Button btn0 = new Button("0"); - btn0.setOnAction(event -> handleNumberButton("0", display)); - grid.add(btn0, 1, 4); - - Button btnDecimal = new Button("."); - btnDecimal.setOnAction(event -> handleDecimalButton(display)); - grid.add(btnDecimal, 2, 4); - - // 创建运算符按钮,并将其添加到网格面板中 - Button btnAdd = new Button("+"); - btnAdd.setOnAction(event -> handleOperatorButton("+", display)); - grid.add(btnAdd, 3, 1); - - Button btnSubtract = new Button("-"); - btnSubtract.setOnAction(event -> handleOperatorButton("-", display)); - grid.add(btnSubtract, 3, 2); - - Button btnMultiply = new Button("*"); - btnMultiply.setOnAction(event -> handleOperatorButton("*", display)); - grid.add(btnMultiply, 3, 3); - - Button btnDivide = new Button("/"); - btnDivide.setOnAction(event -> handleOperatorButton("/", display)); - grid.add(btnDivide, 3, 4); - - Button btnClear = new Button("C"); - btnClear.setOnAction(event -> handleClearButton(display)); - grid.add(btnClear, 0, 4); - - Button btnSign = new Button("+/-"); - btnSign.setOnAction(event -> handleSignButton(display)); - grid.add(btnSign, 0, 5); - - Button btnPercent = new Button("%"); - btnPercent.setOnAction(event -> handlePercentButton(display)); - grid.add(btnPercent, 1, 5); - - Button btnEquals = new Button("="); - btnEquals.setOnAction(e -> handleEqualsButton(display)); - grid.add(btnEquals, 3, 5); - - // 创建一个场景,并将网格面板添加到其中 - Scene scene = new Scene(grid, 240, 250); - primaryStage.setScene(scene); - - // 监听键盘事件 - scene.setOnKeyPressed(event -> { - KeyCode keyCode = event.getCode(); - if (keyCode.isDigitKey()) { - handleNumberButton(keyCode.getName(), display); - } else if (keyCode == KeyCode.ADD || keyCode == KeyCode.PLUS) { - handleOperatorButton("+", display); - } else if (keyCode == KeyCode.SUBTRACT || keyCode == KeyCode.MINUS) { - handleOperatorButton("-", display); - } else if (keyCode == KeyCode.MULTIPLY) { - handleOperatorButton("*", display); - } else if (keyCode == KeyCode.DIVIDE) { - handleOperatorButton("/", display); - } else if (keyCode == KeyCode.PERIOD) { - handleDecimalButton(display); - } else if (keyCode == KeyCode.ENTER) { - handleEqualsButton(display); - } - }); - - // 显示窗口 - primaryStage.show(); - } - - /** - * 处理数字按钮的事件 - * - * @param number 按下的数字 - * @param display 显示结果的文本框 - */ - private void handleNumberButton(String number, TextField display) { - if (newCalculation) { - display.clear();//清除文本框 - newCalculation = false; - } - String text = display.getText(); - if (text.equals("0")) { - display.setText(number); - } else if (!decimal || !text.contains(".")) { - display.setText(text + number); - } - } - - /** - * 处理运算符按钮的事件 - * @param operator 按下的运算符号 - * @param display 显示结果的文本框 - */ - private void handleOperatorButton(String operator, TextField display) { - if (!this.operator.equals("")) {//判断当前计算器是否已经有运算符被按下,如果有,则调用 handleEqualsButton 方法进行计算 - handleEqualsButton(display); - } - num1 = Double.parseDouble(display.getText()); - this.operator = operator; - display.setText(num1 + " " + operator + " "); - decimal = false; - newCalculation = false; - } - - /** - * 处理小数点按钮的事件 - * - * @param display 显示结果的文本框 - */ - private void handleDecimalButton(TextField display) { - if (!decimal) { - String text = display.getText(); - if (newCalculation) { - display.clear(); - newCalculation = false; - } - display.setText(text + "."); - decimal = true; - } - } - - /** - * 处理清除按钮的事件 - * @param display 显示结果的文本框 - */ - private void handleClearButton(TextField display) { - display.clear(); - num1 = 0; - num2 = 0; - operator = ""; - decimal = false; - newCalculation = true; - } - - /** - * 处理正负号按钮的事件 - * - * @param display 显示结果的文本框 - */ - private void handleSignButton(TextField display) { - String text = display.getText();//获取文本框中的数字或运算符 - if (!text.equals("") && !text.equals("0")) { - if (text.startsWith("-")) { - display.setText(text.substring(1)); - } else { - display.setText("-" + text); - } - } - } - - /** - * 处理百分号按钮的事件 - * - * @param display 显示结果的文本框 - */ - private void handlePercentButton(TextField display) { - double num = Double.parseDouble(display.getText()); - display.setText(Double.toString(num / 100)); - newCalculation = true; - } - - /** - * 处理等于按钮的事件 - * - * @param display 显示结果的文本框 - */ - private void handleEqualsButton(TextField display) { - if (!operator.equals("")) { - String text = display.getText(); - int index = text.indexOf(operator); - num2 = Double.parseDouble(text.substring(index + operator.length())); - switch (operator) { - case "+": - num1 += num2; - break; - case "-": - num1 -= num2; - break; - case "*": - num1 *= num2; - break; - case "/": - if (num2 == 0) { - display.setText("除零错误"); - num1 = 0; - num2 = 0; - operator = ""; - decimal = false; - newCalculation = true; - return; - } - num1 /= num2; - break; - } - //重置计算器为起始状态 - display.setText(Double.toString(num1));//输入框没有字符则为第一个操作数 - operator = "";//无操作数 - decimal = false;//没有输入小数点 - newCalculation = true;//开始新的一次计算 - } - } - - public static void main(String[] args) { - // 启动 JavaFX 应用程序 - launch(args); - } - -} \ No newline at end of file -- 2.34.1 From b9eedc09c569dbfb9e7f50561f34df1e992fa008 Mon Sep 17 00:00:00 2001 From: Lqx <3055930454@qq.com> Date: Wed, 16 Oct 2024 10:43:05 +0800 Subject: [PATCH 7/7] lqx --- Calculator11.java | 83 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 Calculator11.java diff --git a/Calculator11.java b/Calculator11.java new file mode 100644 index 0000000..6ad4096 --- /dev/null +++ b/Calculator11.java @@ -0,0 +1,83 @@ +package hello.demo; +import javafx.application.Application; +import javafx.geometry.Insets; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.TextField; +import javafx.scene.layout.GridPane; +import javafx.stage.Stage; + +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; + +public class Calculator extends Application { + + private TextField display; + + @Override + public void start(Stage primaryStage) { + primaryStage.setTitle("计算器"); + + display = new TextField(); + display.setEditable(false); + + GridPane grid = new GridPane(); + grid.setPadding(new Insets(10)); + grid.setVgap(5); + grid.setHgap(5); + + // 按钮定义,包括取余运算 "%" + String[] buttons = { + "7", "8", "9", "/", + "4", "5", "6", "*", + "1", "2", "3", "-", + "0", "C", "=", "+", + "%" + }; + + int index = 0; + for (int row = 0; row < 5; row++) { // 修改行数以添加新按钮 + for (int col = 0; col < 4; col++) { + if (index >= buttons.length) break; // 防止数组越界 + Button btn = new Button(buttons[index]); + btn.setMinSize(50, 50); + btn.setOnAction(e -> handleButtonClick(btn.getText())); + grid.add(btn, col, row); + index++; + } + } + + grid.add(display, 0, 5, 4, 1); // 显示区域在最后一行 + + Scene scene = new Scene(grid, 250, 350); + primaryStage.setScene(scene); + primaryStage.show(); + } + + private void handleButtonClick(String text) { + if ("C".equals(text)) { + display.clear(); + } else if ("=".equals(text)) { + calculateResult(); + } else { + display.appendText(text); + } + } + + private void calculateResult() { + try { + String input = display.getText(); + // 使用脚本引擎执行表达式 + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine engine = manager.getEngineByName("JavaScript"); + Object result = engine.eval(input); + display.setText(result.toString()); + } catch (Exception e) { + display.setText("错误"); + } + } + + public static void main(String[] args) { + launch(args); + } +} -- 2.34.1