package cdk; 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.Label; import javafx.scene.control.TextField; import javafx.scene.layout.GridPane; import javafx.stage.Stage; public class CalculatorApp extends Application { private TextField display; private double firstNumber = 0; private boolean userIsTypingSecondNumber = false; private String currentOperator = ""; @Override public void start(Stage primaryStage) { primaryStage.setTitle("计算器"); display = new TextField(); display.setEditable(false); display.setPrefColumnCount(16); GridPane grid = new GridPane(); grid.setAlignment(Pos.CENTER); grid.setPadding(new Insets(10, 10, 10, 10)); grid.setVgap(8); grid.setHgap(8); // 添加数字按钮 for (int i = 1; i <= 9; i++) { Button btn = new Button(String.valueOf(i)); int finalI = i; btn.setOnAction(e -> appendNumber(finalI)); grid.add(btn, i % 3, i / 3 + 1); } // 添加0按钮 Button btn0 = new Button("0"); btn0.setOnAction(e -> appendNumber(0)); grid.add(btn0, 0, 4); // 添加点按钮 Button btnDot = new Button("."); btnDot.setOnAction(e -> appendDot()); grid.add(btnDot, 1, 4); // 添加操作符按钮 String[] operators = {"+", "-", "*", "/"}; int row = 5; for (String op : operators) { Button btnOp = new Button(op); btnOp.setOnAction(e -> setOperator(op)); grid.add(btnOp, (op.equals("/") ? 3 : (op.equals("*") ? 2 : (op.equals("-") ? 1 : 0))), row); } // 添加等号按钮 Button btnEquals = new Button("="); btnEquals.setOnAction(e -> calculate()); grid.add(btnEquals, 1, 5, 2, 1); // 添加清除按钮 Button btnClear = new Button("C"); btnClear.setOnAction(e -> clear()); grid.add(btnClear, 3, 5); // 将显示框添加到网格的第一行 grid.add(display, 0, 0, 4, 1); Scene scene = new Scene(grid, 300, 400); primaryStage.setScene(scene); primaryStage.show(); } private void appendNumber(int number) { if (userIsTypingSecondNumber) { display.setText(display.getText() + number); } else { firstNumber = firstNumber * 10 + number; display.setText(String.valueOf(firstNumber)); } } private void appendDot() { if (!display.getText().contains(".")) { display.setText(display.getText() + "."); } } private void setOperator(String operator) { if (!currentOperator.isEmpty()) { calculate(); } currentOperator = operator; userIsTypingSecondNumber = true; display.setText(""); } private void calculate() { double secondNumber = Double.parseDouble(display.getText()); double result = 0; boolean valid = true; switch (currentOperator) { case "+": result = firstNumber + secondNumber; break; case "-": result = firstNumber - secondNumber; break; case "*": result = firstNumber * secondNumber; break; case "/": if (secondNumber == 0) { display.setText("错误"); valid = false; } else { result = firstNumber / secondNumber; } break; } if (valid) { display.setText(String.valueOf(result)); firstNumber = result; currentOperator = ""; userIsTypingSecondNumber = false; } } private void clear() { display.setText(""); firstNumber = 0; userIsTypingSecondNumber = false; currentOperator = ""; } public static void main(String[] args) { launch(args); } }