|
|
|
|
@ -1,60 +0,0 @@
|
|
|
|
|
package qw;
|
|
|
|
|
import javafx.application.Application;
|
|
|
|
|
import javafx.scene.Scene;
|
|
|
|
|
import javafx.scene.control.*;
|
|
|
|
|
import javafx.scene.layout.*;
|
|
|
|
|
import javafx.stage.Stage;
|
|
|
|
|
import javafx.geometry.Insets;
|
|
|
|
|
import javafx.geometry.Pos;
|
|
|
|
|
public class Calculator extends Application { private double result = 0; private String operator = "";
|
|
|
|
|
private boolean start = true; // 用于判断是否开始新的输入
|
|
|
|
|
//aaaa
|
|
|
|
|
//bbb
|
|
|
|
|
private TextField display;
|
|
|
|
|
// 显示框
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
launch(args);
|
|
|
|
|
} @Override public void start(Stage primaryStage) { primaryStage.setTitle("计算器"); // 创建显示框
|
|
|
|
|
display = new TextField(); display.setEditable(false);
|
|
|
|
|
display.setStyle("-fx-font-size: 32px;" +
|
|
|
|
|
" -fx-alignment: CENTER_RIGHT;" +
|
|
|
|
|
" -fx-padding: 10;");
|
|
|
|
|
display.setAlignment(Pos.CENTER_RIGHT); // 创建按钮
|
|
|
|
|
Button[] numberButtons = new Button[10];
|
|
|
|
|
for (int i = 0; i < 10; i++) { numberButtons[i] = createNumberButton(String.valueOf(i)); } Button addButton = createOperatorButton("+"); Button subButton = createOperatorButton("-"); Button mulButton = createOperatorButton("×");
|
|
|
|
|
Button divButton = createOperatorButton("÷");
|
|
|
|
|
Button modButton = createOperatorButton("%");
|
|
|
|
|
Button equalButton = createEqualButton("=");
|
|
|
|
|
Button clearButton = createClearButton("C"); // 创建布局
|
|
|
|
|
GridPane grid = new GridPane();
|
|
|
|
|
grid.setPadding(new Insets(20));
|
|
|
|
|
grid.setVgap(15); grid.setHgap(15); // 将按钮添加到布局
|
|
|
|
|
grid.add(display, 0, 0, 4, 1); grid.add(numberButtons[7], 0, 1);
|
|
|
|
|
grid.add(numberButtons[8], 1, 1); grid.add(numberButtons[9], 2, 1); grid.add(divButton, 3, 1);
|
|
|
|
|
grid.add(numberButtons[4], 0, 2); grid.add(numberButtons[5], 1, 2); grid.add(numberButtons[6], 2, 2);
|
|
|
|
|
grid.add(mulButton, 3, 2);
|
|
|
|
|
grid.add(numberButtons[1], 0, 3);
|
|
|
|
|
grid.add(numberButtons[2], 1, 3);
|
|
|
|
|
grid.add(numberButtons[3], 2, 3);
|
|
|
|
|
grid.add(subButton, 3, 3);
|
|
|
|
|
grid.add(clearButton, 0, 4);
|
|
|
|
|
grid.add(numberButtons[0], 1, 4);
|
|
|
|
|
grid.add(equalButton, 2, 4);
|
|
|
|
|
grid.add(addButton, 3, 4);
|
|
|
|
|
grid.add(modButton, 3, 5);
|
|
|
|
|
for (Button button : numberButtons) { button.setPrefSize(70, 70);
|
|
|
|
|
|
|
|
|
|
button.setStyle("-fx-font-size: 24px; -fx-background-color: #87CEFA;");
|
|
|
|
|
} addButton.setPrefSize(70, 70);
|
|
|
|
|
subButton.setPrefSize(70, 70);
|
|
|
|
|
mulButton.setPrefSize(70, 70);
|
|
|
|
|
divButton.setPrefSize(70, 70);
|
|
|
|
|
modButton.setPrefSize(70, 70);
|
|
|
|
|
equalButton.setPrefSize(70, 70);
|
|
|
|
|
clearButton.setPrefSize(70, 70);
|
|
|
|
|
// 设置操作符按钮样式
|
|
|
|
|
addButton.setStyle("-fx-font-size: 24px; -fx-background-color: #FFD700;");
|
|
|
|
|
subButton.setStyle("-fx-font-size: 24px; -fx-background-color: #FFD700;");
|
|
|
|
|
mulButton.setStyle("-fx-font-size: 24px; -fx-background-color: #FFD700;");
|
|
|
|
|
divButton.setStyle("-fx-font-size: 24px; -fx-background-color: #FFD700;");
|
|
|
|
|
modButton.setStyle("-fx-font-size: 24px; -fx-background-color: #FFD700;"); equalButton.setStyle("-fx-font-size: 24px; -fx-background-color: #32CD32; -fx-text-fill: white;"); clearButton.setStyle("-fx-font-size: 24px; -fx-background-color: #FF6347; -fx-text-fill: white;"); // 设置窗口大小 Scene scene = new Scene(grid, 400, 500); primaryStage.setScene(scene); primaryStage.show(); } private Button createNumberButton(String number) { Button button = new Button(number); button.setStyle("-fx-font-size: 24px;"); button.setOnAction(e -> handleNumberInput(number)); return button; } private Button createOperatorButton(String operator) { Button button = new Button(operator); button.setStyle("-fx-font-size: 24px;"); button.setOnAction(e -> handleOperatorInput(operator)); return button; }
|