diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..359bb53
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,3 @@
+# 默认忽略的文件
+/shelf/
+/workspace.xml
diff --git a/.idea/Calculator.iml b/.idea/Calculator.iml
new file mode 100644
index 0000000..b107a2d
--- /dev/null
+++ b/.idea/Calculator.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..e1f830b
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..9fd3a30
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Calculator.java b/Calculator.java
index 47fcb96..2cc6251 100644
--- a/Calculator.java
+++ b/Calculator.java
@@ -1,97 +1,20 @@
-import javax.swing.*;
-import java.awt.*;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
+import javafx.application.Application;
+import javafx.fxml.FXMLLoader;
+import javafx.scene.Scene;
+import javafx.stage.Stage;
-public class Calculator extends JFrame implements ActionListener {
- private JTextField display;
- private double num1 = 0, num2 = 0, result = 0;
- private String operation = "";
-
- public Calculator() {
- setTitle("简易计算器");
- setSize(400, 600);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLayout(new BorderLayout());
-
- // 显示屏
- display = new JTextField();
- display.setEditable(false);
- display.setHorizontalAlignment(JTextField.RIGHT);
- add(display, BorderLayout.NORTH);
-
- // 按钮面板
- JPanel panel = new JPanel();
- panel.setLayout(new GridLayout(4, 4, 10, 10));
- String[] buttons = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "C", "0", "=", "+"};
- for (String text : buttons) {
- JButton button = new JButton(text);
- button.addActionListener(this);
- panel.add(button);
- }
- add(panel, BorderLayout.CENTER);
-
- setVisible(true);
- }
+public class Calculator extends Application {
@Override
- public void actionPerformed(ActionEvent e) {
- String command = e.getActionCommand();
- if (command.equals("C")) {
- clearDisplay();
- } else if (command.equals("=")) {
- calculateResult();
- } else {
- handleInput(command);
- }
- }
-
- private void handleInput(String input) {
- if (input.matches("[0-9]")) {
- display.setText(display.getText() + input);
- } else {
- operation = input;
- num1 = Double.parseDouble(display.getText());
- display.setText("");
- }
- }
-
- private void calculateResult() {
- num2 = Double.parseDouble(display.getText());
- switch (operation) {
- case "+":
- result = num1 + num2;
- break;
- case "-":
- result = num1 - num2;
- break;
- case "*":
- result = num1 * num2;
- break;
- case "/":
- if (num2 != 0) {
- result = num1 / num2;
- } else {
- display.setText("Error");
- return;
- }
- break;
- case "%":
- result = num1 % num2;
- break;
- }
- display.setText(String.valueOf(result));
- }
-
- private void clearDisplay() {
- display.setText("");
- num1 = 0;
- num2 = 0;
- result = 0;
- operation = "";
+ public void start(Stage primaryStage) throws Exception {
+ FXMLLoader loader = new FXMLLoader(getClass().getResource("Calculator.fxml"));
+ Scene scene = new Scene(loader.load());
+ primaryStage.setTitle("简易计算器");
+ primaryStage.setScene(scene);
+ primaryStage.show();
}
public static void main(String[] args) {
- new Calculator();
+ launch(args);
}
}
diff --git a/CalculatorController.java b/CalculatorController.java
new file mode 100644
index 0000000..0df6d32
--- /dev/null
+++ b/CalculatorController.java
@@ -0,0 +1,122 @@
+import javafx.fxml.FXML;
+import javafx.scene.control.TextField;
+import javafx.scene.control.Button;
+import javafx.event.ActionEvent;
+
+public class CalculatorController {
+
+ @FXML
+ private TextField inputField;
+ @FXML
+ private TextField inputField1; // 用于显示计算记录
+
+ private double firstNumber = 0;
+ private String operator = "";
+ private boolean isOperationClicked = false;
+
+ @FXML
+ protected void handleNumberAction(ActionEvent event) {
+ Button button = (Button) event.getSource();
+ String number = button.getText();
+ if (isOperationClicked) {
+ inputField.clear();
+ isOperationClicked = false;
+ }
+ inputField.setText(inputField.getText() + number);
+ }
+
+ @FXML
+ protected void handleAddAction(ActionEvent event) {
+ performOperation("+");
+ }
+
+ @FXML
+ protected void handleSubtractAction(ActionEvent event) {
+ performOperation("-");
+ }
+
+ @FXML
+ protected void handleMultiplyAction(ActionEvent event) {
+ performOperation("*");
+ }
+
+ @FXML
+ protected void handleDivideAction(ActionEvent event) {
+ performOperation("/");
+ }
+
+ @FXML
+ protected void handleModulusAction(ActionEvent event) {
+ performOperation("%");
+ }
+
+ private void performOperation(String op) {
+ firstNumber = Double.parseDouble(inputField.getText());
+ operator = op;
+ isOperationClicked = true;
+ }
+
+ @FXML
+ protected void handleEqualAction(ActionEvent event) {
+ double secondNumber = Double.parseDouble(inputField.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 {
+ inputField.setText("Error");
+ return;
+ }
+ break;
+ case "%":
+ result = firstNumber % secondNumber;
+ break;
+ default:
+ return;
+ }
+
+ inputField.setText(String.valueOf(result));
+ recordOperation(firstNumber, secondNumber, result);
+ }
+
+ private void recordOperation(double a, double b, double result) {
+ String operation = a + " " + operator + " " + b + " = " + result;
+ inputField1.setText(inputField1.getText() + "\n" + operation);
+ }
+
+ @FXML
+ protected void handleClearAction(ActionEvent event) {
+ inputField.clear();
+ firstNumber = 0;
+ operator = "";
+ isOperationClicked = false;
+ }
+
+ @FXML
+ protected void handleClearsAction(ActionEvent event) {
+ inputField1.clear();
+ }
+
+ @FXML
+ protected void handlePointAction(ActionEvent event) {
+ if (!inputField.getText().contains(".")) {
+ inputField.setText(inputField.getText() + ".");
+ }
+ }
+
+ @FXML
+ protected void handleRemoveAction(ActionEvent event) {
+ inputField1.clear();
+ }
+}
diff --git a/out/production/Calculator/.idea/.gitignore b/out/production/Calculator/.idea/.gitignore
new file mode 100644
index 0000000..359bb53
--- /dev/null
+++ b/out/production/Calculator/.idea/.gitignore
@@ -0,0 +1,3 @@
+# 默认忽略的文件
+/shelf/
+/workspace.xml
diff --git a/out/production/Calculator/.idea/Calculator.iml b/out/production/Calculator/.idea/Calculator.iml
new file mode 100644
index 0000000..b107a2d
--- /dev/null
+++ b/out/production/Calculator/.idea/Calculator.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/out/production/Calculator/.idea/misc.xml b/out/production/Calculator/.idea/misc.xml
new file mode 100644
index 0000000..e1f830b
--- /dev/null
+++ b/out/production/Calculator/.idea/misc.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/out/production/Calculator/.idea/modules.xml b/out/production/Calculator/.idea/modules.xml
new file mode 100644
index 0000000..9fd3a30
--- /dev/null
+++ b/out/production/Calculator/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/out/production/Calculator/.idea/vcs.xml b/out/production/Calculator/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/out/production/Calculator/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/out/production/Calculator/Calculator.class b/out/production/Calculator/Calculator.class
new file mode 100644
index 0000000..2e6a5f0
Binary files /dev/null and b/out/production/Calculator/Calculator.class differ
diff --git a/out/production/Calculator/CalculatorController.class b/out/production/Calculator/CalculatorController.class
new file mode 100644
index 0000000..bf45578
Binary files /dev/null and b/out/production/Calculator/CalculatorController.class differ
diff --git a/out/production/Calculator/CalculatorView.fxml b/out/production/Calculator/CalculatorView.fxml
new file mode 100644
index 0000000..4cc1c31
--- /dev/null
+++ b/out/production/Calculator/CalculatorView.fxml
@@ -0,0 +1,116 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/out/production/Calculator/README.md b/out/production/Calculator/README.md
new file mode 100644
index 0000000..dc7115b
--- /dev/null
+++ b/out/production/Calculator/README.md
@@ -0,0 +1,2 @@
+# Calculator
+