From d1ead7bbb96e0a88cc8f1f427688178e8c2d7fe5 Mon Sep 17 00:00:00 2001 From: liwenjun <2684903586@qq.com> Date: Wed, 16 Oct 2024 19:45:00 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=EF=BC=9A=E8=AE=A1=E7=AE=97=E5=99=A8=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 3 + .idea/misc.xml | 6 ++ .idea/modules.xml | 9 +++ .idea/new.iml | 9 +++ .idea/vcs.xml | 6 ++ Calculator.java | 119 --------------------------------- Calculator/Calculator.iml | 11 +++ Calculator/src/Calculator.java | 110 ++++++++++++++++++++++++++++++ 8 files changed, 154 insertions(+), 119 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/new.iml create mode 100644 .idea/vcs.xml delete mode 100644 Calculator.java create mode 100644 Calculator/Calculator.iml create mode 100644 Calculator/src/Calculator.java 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/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..065cbba --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/new.iml b/.idea/new.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/new.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ 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 deleted file mode 100644 index ac5b027..0000000 --- a/Calculator.java +++ /dev/null @@ -1,119 +0,0 @@ -package java6324.rj; - -import javax.swing.*; -import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - -public class Calculator extends JFrame { - private JTextField input1, input2, result; - private JButton addButton, subtractButton, multiplyButton, divideButton, modButton; - private JPanel panel; - public Calculator() { - setTitle("Simple Calculator"); - setSize(300, 200); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setLayout(new GridLayout(5, 4, 5, 5)); - panel = new JPanel(); - panel.setLayout(new GridLayout(5, 4, 5, 5)); - - input1 = new JTextField(5); - input2 = new JTextField(5); - result = new JTextField(5); - result.setEditable(false); - - addButton = new JButton("+"); - subtractButton = new JButton("-"); - multiplyButton = new JButton("*"); - divideButton = new JButton("/"); - modButton = new JButton("%"); - - addButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - calculate(1); - } - }); - - subtractButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - calculate(2); - } - }); - - multiplyButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - calculate(3); - } - }); - - divideButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - calculate(4); - } - }); - - modButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - calculate(5); - } - }); - - panel.add(new JLabel("Input 1:")); - panel.add(input1); - panel.add(new JLabel("Input 2:")); - panel.add(input2); - panel.add(addButton); - panel.add(subtractButton); - panel.add(multiplyButton); - panel.add(divideButton); - panel.add(modButton); - panel.add(new JLabel()); - panel.add(result); - - add(panel); - setVisible(true); - } - - private void calculate(int operation) { - try { - double num1 = Double.parseDouble(input1.getText()); - double num2 = Double.parseDouble(input2.getText()); - double res = 0; - - switch (operation) { - case 1: - res = num1 + num2; - break; - case 2: - res = num1 - num2; - break; - case 3: - res = num1 * num2; - break; - case 4: - if (num2 != 0) { - res = num1 / num2; - } else { - result.setText("Error"); - return; - } - break; - case 5: - if (num2 != 0) { - res = num1 % num2; - } else { - result.setText("Error"); - return; - } - break; - } - result.setText(String.valueOf(res)); - } catch (NumberFormatException e) { - result.setText("Invalid Input"); - } - } - - public static void main(String[] args) { - new Calculator(); - } -} diff --git a/Calculator/Calculator.iml b/Calculator/Calculator.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Calculator/Calculator.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Calculator/src/Calculator.java b/Calculator/src/Calculator.java new file mode 100644 index 0000000..1a26029 --- /dev/null +++ b/Calculator/src/Calculator.java @@ -0,0 +1,110 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class Calculator extends JFrame implements ActionListener { + private final JTextField textField; // 显示输入和结果的文本框 + private double result = 0; // 计算结果 + private String operator = ""; // 运算符 + private boolean calculating = true; // 是否正在计算中 + + public Calculator() { + setTitle("计算器"); + textField = new JTextField("0"); + textField.setEditable(false); + textField.setHorizontalAlignment(JTextField.RIGHT); // 右对齐显示 + textField.setFont(new Font("Arial", Font.BOLD, 24)); // 增加文本框字体大小 + add(textField, BorderLayout.NORTH); + + JPanel panel = new JPanel(); + panel.setLayout(new GridLayout(5, 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.setFont(new Font("Arial", Font.BOLD, 18)); // 增加按钮字体大小 + button.addActionListener(this); + panel.add(button); + } + + add(panel, BorderLayout.CENTER); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(800, 500); // 设置窗口大小为宽800像素,高500像素 + setLocationRelativeTo(null); // 窗口居中显示 + pack(); + setVisible(true); + } + + public void actionPerformed(ActionEvent e) { + String command = e.getActionCommand(); + if ('0' <= command.charAt(0) && command.charAt(0) <= '9' || command.equals(".")) { + if (calculating) { + textField.setText(command); + calculating = false; + } else { + textField.setText(textField.getText() + command); + } + } else { + switch (command) { + case "C": + textField.setText("0"); + operator = ""; + result = 0; + calculating = true; + break; + case "+": + case "-": + case "*": + case "/": + if (!calculating) { + result = Double.parseDouble(textField.getText()); + operator = command; + calculating = true; + } + break; + case "=": + if (!calculating) { + calculate(Double.parseDouble(textField.getText())); + } + break; + } + } + } + + private void calculate(double n) { + switch (operator) { + case "+": + result += n; + break; + case "-": + result -= n; + break; + case "*": + result *= n; + break; + case "/": + if (n != 0) { + result /= n; + } else { + JOptionPane.showMessageDialog(this, "除数不能为0"); + return; + } + break; + } + textField.setText("" + result); + operator = ""; + calculating = true; + } + + public static void main(String[] args) { + new Calculator(); + } +} \ No newline at end of file