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 0000000..fd7bf9b Binary files /dev/null and b/out/production/computer/Calculator.class differ diff --git a/out/production/computer/README.md b/out/production/computer/README.md new file mode 100644 index 0000000..1e192cb --- /dev/null +++ b/out/production/computer/README.md @@ -0,0 +1,2 @@ +# computer +