From f6aa2550cc2930dac689e9f818aa361fd49cf6ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BF=97=E5=98=89?= <282227179@qq.com> Date: Thu, 10 Oct 2024 17:22:52 +0800 Subject: [PATCH] modi --- .idea/.gitignore | 3 + .idea/Calculator.iml | 11 ++ .idea/misc.xml | 6 + .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 + Calculator.java | 101 ++------------- CalculatorController.java | 122 ++++++++++++++++++ out/production/Calculator/.idea/.gitignore | 3 + .../Calculator/.idea/Calculator.iml | 11 ++ out/production/Calculator/.idea/misc.xml | 6 + out/production/Calculator/.idea/modules.xml | 8 ++ out/production/Calculator/.idea/vcs.xml | 6 + out/production/Calculator/Calculator.class | Bin 0 -> 1261 bytes .../Calculator/CalculatorController.class | Bin 0 -> 3750 bytes out/production/Calculator/CalculatorView.fxml | 116 +++++++++++++++++ out/production/Calculator/README.md | 2 + 16 files changed, 320 insertions(+), 89 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/Calculator.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 CalculatorController.java create mode 100644 out/production/Calculator/.idea/.gitignore create mode 100644 out/production/Calculator/.idea/Calculator.iml create mode 100644 out/production/Calculator/.idea/misc.xml create mode 100644 out/production/Calculator/.idea/modules.xml create mode 100644 out/production/Calculator/.idea/vcs.xml create mode 100644 out/production/Calculator/Calculator.class create mode 100644 out/production/Calculator/CalculatorController.class create mode 100644 out/production/Calculator/CalculatorView.fxml create mode 100644 out/production/Calculator/README.md 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 0000000000000000000000000000000000000000..2e6a5f0122969f305631b311757cf91631d8244e GIT binary patch literal 1261 zcmZuxOH&rH0`9R;?5u7&V{;k z=ZYCO#gQ4uwf{r$S2&)V6ha*)Gw0rW?s#8g3WmBYH|=9gD(eGBL4e@oJD3xq)(Mw(&SG^3v~T|x^zCanZ3;pNWy@$0!M6Hem{VMwA6 zXVFVI`l+x!l3^+lWKE4k6E0wYb^_xVt&G-a_A3%WV(~nIc!&@a$BVp35JZueNrsU5 E2WU?vDF6Tf literal 0 HcmV?d00001 diff --git a/out/production/Calculator/CalculatorController.class b/out/production/Calculator/CalculatorController.class new file mode 100644 index 0000000000000000000000000000000000000000..bf455781210909f6f2807922b4cf5701e8047ef0 GIT binary patch literal 3750 zcmb7G+jARN8UGzg)~@BX>?pP)Oyj1PwY0W3S!$sib!8epB4e1(eb*D&&v1D zY52T`FDRt!YOUeV*p^dK$j{z3x6HNeQoU?dty0;o`kw2QmaOgY#6^YN^+wgVH?3v6 zZm&Alm1@=XP2YB_b%jjp`r7uUQ<`~kZk8ANb_6^@YR&fQ{(NI|)$$b7l!EToEYI{^ zlHvW5V^-Hoi@s-9*Lgo<*B62llribp0ttspYQKs)UOs9om)Gek{r4{+4XVs}tkcDs17>{4QN{4`C zVQOkh5S7WXs?ZzeNf3xgz@%fDUQ0k^Prz$y9vytawX1D^Lg}vj$G#v;qu_;f#S2#x=ZQ;7j-HcWk+);30^|n>^ zcd^1MRmetWf1oZHw{^=8ol6;o(IDwVgF`%Ky2c~YV;(-x?g59pdbQn_Xm43kfi+{+ zYF3pEVys(!cE}E)BWF_u-FI8IX!Vvh9kx%oji8zI)l9E$wWzLDAU$ovtZ#(2;h#WA=W~ObKPQzMQlM_>&PHOw$U^jk;0n)5ZTiWn}PTMCpOmC6Wt!ml2 zIJ$x(dcn0UGi~zq&V7YVo zR1?YHK$|<=gg#&V1$ypdfJ<)^sX&PAe}%rA4`77s{wC6698C-~F(^93`5Xg>`QQS3 zp<{?z409gA0qjE_IXsX3fj-4hW~2|L;{q;HN1d8o!n4$^pV~b~hs1dYd}q}^k=Jlp z!jaKqs7ksRxzxp$iQwLOGN6NMI z`2?xNg=B0(%O#u0{)}M!z0D!DT;g~=qJ`HQx46F)(X-L&owdXV%;Kqs{cZCDdkHaO%bWqO1|x`mYSRgRabd8|-A zpP;B z*7Ni=Sekt-%QOr2x0L^T7RetljXz=re_|;ALXZEIz3Ok6#Xm5w;#g1#EGh#_Y5>b> z5I5C9tf-TCDTr9m22`OHG48@P(5DU&|iuE!6n#_3iB~i*&goL z+b(GQoZsnu`6i_EWsY#lX`FWYUo7Zz8f7Sl5Oje z?b`Wnw549Aj#8Dn_<&L$?x_x?Y<3{)ZDhSG@^Txw5!o{y+S8wTyosYf!BHW1oJ~su zXY*r+pTl=1^z&W5a2|c;cW5!*)?z%;LXw9MXlYk&B=E_$I{(YiZ5n#7L&SeKR53~A HwZHivH|Dz~ literal 0 HcmV?d00001 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 +