diff --git a/Calculater/.gitignore b/Calculater/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/Calculater/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/Calculater/.idea/.gitignore b/Calculater/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/Calculater/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/Calculater/.idea/.name b/Calculater/.idea/.name new file mode 100644 index 0000000..def5595 --- /dev/null +++ b/Calculater/.idea/.name @@ -0,0 +1 @@ +Calculater.java \ No newline at end of file diff --git a/Calculater/.idea/misc.xml b/Calculater/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/Calculater/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Calculater/.idea/modules.xml b/Calculater/.idea/modules.xml new file mode 100644 index 0000000..c239d47 --- /dev/null +++ b/Calculater/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Calculater/.idea/vcs.xml b/Calculater/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/Calculater/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Calculater/Calculater.iml b/Calculater/Calculater.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Calculater/Calculater.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Calculater/src/Calculater.java b/Calculater/src/Calculater.java new file mode 100644 index 0000000..75c0461 --- /dev/null +++ b/Calculater/src/Calculater.java @@ -0,0 +1,153 @@ +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.layout.GridPane; +import javafx.scene.layout.VBox; +import javafx.stage.Stage; +public class Calculater extends Application { + private Label display; + private double firstNumber = 0; + private String operator = ""; + private boolean startNewNumber = true; + + public static void main(String[] args) { + launch(args); + } + + @Override + public void start(Stage primaryStage) { + // Create display label + display = new Label("0.0"); + display.setStyle("-fx-font-size: 30px;"); + display.setMinWidth(200); // Adjust width for display + display.setAlignment(Pos.CENTER_RIGHT); // Align text to the right + + // Set up the buttons + GridPane gridPane = createButtonGrid(); + + // Create the layout + VBox layout = new VBox(10); + layout.setPadding(new Insets(10)); + layout.setAlignment(Pos.CENTER); + layout.getChildren().addAll(display, gridPane); + + // Set up the scene and stage + Scene scene = new Scene(layout, 300, 400); + primaryStage.setTitle("Calculator"); + primaryStage.setScene(scene); + primaryStage.show(); + } + + private GridPane createButtonGrid() { + GridPane grid = new GridPane(); + grid.setHgap(10); + grid.setVgap(10); + grid.setAlignment(Pos.CENTER); + + // Create buttons with a similar layout to your screenshot + String[][] buttonText = { + {"AC", "±", "%", "÷"}, + {"7", "8", "9", "×"}, + {"4", "5", "6", "-"}, + {"1", "2", "3", "+"}, + {"0", ".", "=", ""} + }; + + // Set button size + int buttonSize = 50; + + // Add buttons to the grid + int row = 0; + for (int i = 0; i < buttonText.length; i++) { + for (int j = 0; j < buttonText[i].length; j++) { + if (!buttonText[i][j].isEmpty()) { + Button button = createButton(buttonText[i][j], buttonSize); + if (buttonText[i][j].equals("0")) { + grid.add(button, j, row, 2, 1); // Make '0' span two columns + } else { + grid.add(button, j, row); + } + } + } + row++; + } + + return grid; + } + + private Button createButton(String text, int size) { + Button button = new Button(text); + button.setMinSize(size, size); + button.setStyle("-fx-font-size: 18px; -fx-background-color: white; -fx-border-color: lightgray;"); + button.setOnAction(e -> handleButtonPress(text)); + return button; + } + + private void handleButtonPress(String text) { + if (text.matches("\\d") || text.equals(".")) { + handleNumberInput(text); + } else if (text.equals("AC")) { + handleClear(); + } else if (text.equals("=")) { + handleEquals(); + } else { + handleOperator(text); + } + } + + private void handleNumberInput(String text) { + if (startNewNumber) { + display.setText(text); + startNewNumber = false; + } else { + display.setText(display.getText() + text); + } + } + + private void handleClear() { + display.setText("0"); + firstNumber = 0; + operator = ""; + startNewNumber = true; + } + + private void handleOperator(String text) { + firstNumber = Double.parseDouble(display.getText()); + operator = text; + startNewNumber = true; + } + + private void handleEquals() { + double secondNumber = Double.parseDouble(display.getText()); + double result = 0; + + switch (operator) { + case "+": + result = firstNumber + secondNumber; + break; + case "-": + result = firstNumber - secondNumber; + break; + case "×": + result = firstNumber * secondNumber; + break; + case "÷": + if (secondNumber != 0) { + result = firstNumber / secondNumber; + } else { + display.setText("Error"); + return; + } + break; + case "%": + result = firstNumber % secondNumber; + break; + } + + display.setText(String.valueOf(result)); + startNewNumber = true; + } +} \ No newline at end of file diff --git a/Calculater/src/Main.java b/Calculater/src/Main.java new file mode 100644 index 0000000..1e87232 --- /dev/null +++ b/Calculater/src/Main.java @@ -0,0 +1,17 @@ +// 按两次 Shift 打开“随处搜索”对话框并输入 `show whitespaces`, +// 然后按 Enter 键。现在,您可以在代码中看到空格字符。 +public class Main { + public static void main(String[] args) { + // 当文本光标位于高亮显示的文本处时按 Alt+Enter, + // 可查看 IntelliJ IDEA 对于如何修正该问题的建议。 + System.out.printf("Hello and welcome!"); + + // 按 Shift+F10 或点击装订区域中的绿色箭头按钮以运行脚本。 + for (int i = 1; i <= 5; i++) { + + // 按 Shift+F9 开始调试代码。我们已为您设置了一个断点, + // 但您始终可以通过按 Ctrl+F8 添加更多断点。 + System.out.println("i = " + i); + } + } +} \ No newline at end of file