You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
147 lines
4.2 KiB
147 lines
4.2 KiB
package jsq6267;
|
|
|
|
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.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; // 使用 finalI 来捕获 i 的值
|
|
btn.setOnAction(e -> appendNumber(finalI));
|
|
grid.add(btn, (i - 1) % 3, (i - 1) / 3 + 1);
|
|
}
|
|
|
|
// 添加0按钮
|
|
Button btn0 = new Button("0");
|
|
btn0.setOnAction(e -> appendNumber(0));
|
|
grid.add(btn0, 0, 4, 2, 1); // 占据两列
|
|
|
|
// 添加点按钮
|
|
Button btnDot = new Button(".");
|
|
btnDot.setOnAction(e -> appendDot());
|
|
grid.add(btnDot, 2, 4);
|
|
|
|
// 添加操作符按钮
|
|
String[] operators = {"+", "-", "*", "/"};
|
|
for (int i = 0; i < operators.length; i++) {
|
|
Button btnOp = new Button(operators[i]);
|
|
String finalOp = operators[i]; // 使用 finalOp 来捕获 operators[i] 的值
|
|
btnOp.setOnAction(e -> setOperator(finalOp));
|
|
grid.add(btnOp, 3, i + 1);
|
|
}
|
|
|
|
// 添加等号按钮
|
|
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);
|
|
}
|
|
}
|