From 9b7a8779ce9fa6b0b71613d7490bf45025fb37c6 Mon Sep 17 00:00:00 2001 From: 2745873016 <2745873016@qq.com> Date: Fri, 11 Oct 2024 23:07:25 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E5=86=99=E8=AE=A1=E7=AE=97=E5=99=A8?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 ++ .idea/computer.iml | 11 +++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ Calculator.java | 87 +++++++++++++++++++++ out/production/computer/.idea/.gitignore | 8 ++ out/production/computer/.idea/computer.iml | 11 +++ out/production/computer/.idea/misc.xml | 6 ++ out/production/computer/.idea/modules.xml | 8 ++ out/production/computer/.idea/vcs.xml | 6 ++ out/production/computer/Calculator.class | Bin 0 -> 4398 bytes out/production/computer/README.md | 2 + 13 files changed, 167 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/computer.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 Calculator.java create mode 100644 out/production/computer/.idea/.gitignore create mode 100644 out/production/computer/.idea/computer.iml create mode 100644 out/production/computer/.idea/misc.xml create mode 100644 out/production/computer/.idea/modules.xml create mode 100644 out/production/computer/.idea/vcs.xml create mode 100644 out/production/computer/Calculator.class create mode 100644 out/production/computer/README.md diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/computer.iml b/.idea/computer.iml new file mode 100644 index 0000000..b107a2d --- /dev/null +++ b/.idea/computer.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..0548357 --- /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..5d82523 --- /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 new file mode 100644 index 0000000..4ec58ed --- /dev/null +++ b/Calculator.java @@ -0,0 +1,87 @@ +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; + +public class Calculator extends JFrame { + private JTextField inputField; // 输入显示区域 + + public Calculator() { + createUI(); + } + + private void createUI() { + // 设置窗口布局 + setLayout(new BorderLayout()); + + // 创建输入显示区域 + inputField = new JTextField("0"); + inputField.setEditable(false); + inputField.setHorizontalAlignment(JTextField.RIGHT); + add(inputField, BorderLayout.NORTH); + + // 创建面板用于放置按钮 + JPanel panel = new JPanel(); + panel.setLayout(new GridLayout(5, 4, 5, 5)); // 设置间距 + + // 创建数字和运算符按钮 + String[] buttons = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+", "C", "Backspace"}; + for (String b : buttons) { + JButton button = new JButton(b); + panel.add(button); + button.addActionListener(e -> press(e.getActionCommand())); + } + + // 添加面板到窗口 + add(panel, BorderLayout.CENTER); + + // 设置窗口属性 + setSize(300, 400); // 设置窗口大小 + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setTitle("Simple Calculator"); + setVisible(true); + } + + private void press(String command) { + if ("C".equals(command)) { + clear(); + } else if ("Backspace".equals(command)) { + backspace(); + } else if ("=".equals(command)) { + calculate(); + } else { + if (inputField.getText().equals("0") && !command.equals(".")) { + inputField.setText(command); + } else { + inputField.setText(inputField.getText() + command); + } + } + } + + private void clear() { + inputField.setText("0"); + } + + private void backspace() { + String currentText = inputField.getText(); + if (!currentText.isEmpty()) { + inputField.setText(currentText.substring(0, currentText.length() - 1)); + } + } + + private void calculate() { + String expression = inputField.getText(); + try { + ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript"); + Object result = engine.eval(expression); + inputField.setText(result.toString()); + } catch (Exception e) { + JOptionPane.showMessageDialog(this, "Invalid input", "Error", JOptionPane.ERROR_MESSAGE); + } + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(Calculator::new); + } +} \ No newline at end of file diff --git a/out/production/computer/.idea/.gitignore b/out/production/computer/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/out/production/computer/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/out/production/computer/.idea/computer.iml b/out/production/computer/.idea/computer.iml new file mode 100644 index 0000000..b107a2d --- /dev/null +++ b/out/production/computer/.idea/computer.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/computer/.idea/misc.xml b/out/production/computer/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/out/production/computer/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/computer/.idea/modules.xml b/out/production/computer/.idea/modules.xml new file mode 100644 index 0000000..5d82523 --- /dev/null +++ b/out/production/computer/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/out/production/computer/.idea/vcs.xml b/out/production/computer/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/out/production/computer/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/computer/Calculator.class b/out/production/computer/Calculator.class new file mode 100644 index 0000000000000000000000000000000000000000..fd7bf9b2fcc7270ce91d31ed92945cefdf410bf7 GIT binary patch literal 4398 zcma)930xHC75@**?#vFOz$hvvDiTd$0oM}`G>IT8c0tXmDW+{YEF+B0?rdgf6|HUB zG`+9%ZtR^N^loi+G1Ay0O%F}awCS1NM|!1adZw}eZ)RZ^#r(>T@6CMI``-J%|9ju; z`6tev1F%}EQqhXzDthr88P8P_3OcvRc%FjitBBzCC?{Va-tG|Mg$iCImcLlWOH|Cn zaTzaFQG=I>4!6Br!7CKJQo*YfyjsDX3SOh&wF+LR;PndLpx})P-lX8o3hq*HLcv=U zyj8~AR4m2qLdDy4+VyVHeUF0oij&?aV^lofuiyjX`9UE*q+%96EZ#mM#7Bkrn6U5T zLY$QG2^DLFFKh8hAx??)r-dj8F(%`TitBJ(!9C)6whH6;l!|+CO2#<_pH}c08J|_L z4WH8?;eHigz!z0~311c`otJSzf@)>5d2g#_+Jh42B@P;gjgffnh?NgTXiBI~SQ)b~pB^yXJ;s2|5k29g411s9TB2VZtn`MhoCGb= zW!R~_ZFr7Ll}gGr4bR-$L+t~3&vP;o>ZV%I=?`*BUS+mfD36FCvi8X41; zFnj98U1F5ea-f)p&$@ZK?K6^|kvi0CWPMAC2pJ*c7NOTQbKF+fbon*`fShvDX(L0> z!YSJ{TnW(uYRYAe6j7*3kGnx=uk52oP` z(bZbpNDmAeOD4jyWVwVzMfZ&(UfeuPkK&tC;*@Uj-XS($9pE$l0@{*v@@~r9Y6)^w zm*T%n#8Sg9?ACA-cGDR@xI#^*;|`i`!Z_;WJq-`wD>Qw2UJ{1oLWilL4?8t{6<^cv zb$o+C>2q9fSi?8*AOW1R+ouNkFnQiK*BYESZ5O^Jp~jyo;~CFc%eIbfJzJb@HQO4t zcD5MX3bvJOtJvDuma%POYhmkR{5wnJ@K2shvVh{k(r@$RPRH?bo@-=#O>fv4%vDG5 zPz^?^Wqe1&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