钟欣桦修改结果

main
Eterlaze 10 months ago
parent 9e0e656e47
commit 3ad1c31d2e

@ -12,29 +12,25 @@ public class Calculater extends Application {
private double firstNumber = 0; private double firstNumber = 0;
private String operator = ""; private String operator = "";
private boolean startNewNumber = true; private boolean startNewNumber = true;
public static void main(String[] args) { public static void main(String[] args) {
launch(args); launch(args);
} }
@Override @Override
public void start(Stage primaryStage) { public void start(Stage primaryStage) {
// Create display label
display = new Label("0.0"); display = new Label("0.0");
display.setStyle("-fx-font-size: 30px;"); display.setStyle("-fx-font-size: 30px;");
display.setMinWidth(200); // Adjust width for display display.setMinWidth(200);
display.setAlignment(Pos.CENTER_RIGHT); // Align text to the right display.setAlignment(Pos.CENTER_RIGHT);
// Set up the buttons
GridPane gridPane = createButtonGrid(); GridPane gridPane = createButtonGrid();
// Create the layout
VBox layout = new VBox(10); VBox layout = new VBox(10);
layout.setPadding(new Insets(10)); layout.setPadding(new Insets(10));
layout.setAlignment(Pos.CENTER); layout.setAlignment(Pos.CENTER);
layout.getChildren().addAll(display, gridPane); layout.getChildren().addAll(display, gridPane);
// Set up the scene and stage
Scene scene = new Scene(layout, 300, 400); Scene scene = new Scene(layout, 300, 400);
primaryStage.setTitle("Calculator"); primaryStage.setTitle("Calculator");
primaryStage.setScene(scene); primaryStage.setScene(scene);
@ -47,7 +43,6 @@ public class Calculater extends Application {
grid.setVgap(10); grid.setVgap(10);
grid.setAlignment(Pos.CENTER); grid.setAlignment(Pos.CENTER);
// Create buttons with a similar layout to your screenshot
String[][] buttonText = { String[][] buttonText = {
{"AC", "±", "%", "÷"}, {"AC", "±", "%", "÷"},
{"7", "8", "9", "×"}, {"7", "8", "9", "×"},
@ -56,17 +51,15 @@ public class Calculater extends Application {
{"0", ".", "=", ""} {"0", ".", "=", ""}
}; };
// Set button size
int buttonSize = 50; int buttonSize = 50;
// Add buttons to the grid
int row = 0; int row = 0;
for (int i = 0; i < buttonText.length; i++) { for (int i = 0; i < buttonText.length; i++) {
for (int j = 0; j < buttonText[i].length; j++) { for (int j = 0; j < buttonText[i].length; j++) {
if (!buttonText[i][j].isEmpty()) { if (!buttonText[i][j].isEmpty()) {
Button button = createButton(buttonText[i][j], buttonSize); Button button = createButton(buttonText[i][j], buttonSize);
if (buttonText[i][j].equals("0")) { if (buttonText[i][j].equals("0")) {
grid.add(button, j, row, 2, 1); // Make '0' span two columns grid.add(button, j, row, 2, 1);
} else { } else {
grid.add(button, j, row); grid.add(button, j, row);
} }

Loading…
Cancel
Save