From 756a86ca0d4f57f69cdc4eef5e4be3426b3c4937 Mon Sep 17 00:00:00 2001 From: pjj123 <1514193827@qq.com> Date: Thu, 10 Oct 2024 16:46:33 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A1=E7=AE=97=E5=99=A8=E7=9A=84=E7=AC=AC?= =?UTF-8?q?=E4=BA=8C=E6=AC=A1=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 29 ++++++ .idea/.gitignore | 8 ++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ double_team.iml | 11 +++ src/Main.java | 23 +++++ src/compute/SimpleCalculator.java | 159 ++++++++++++++++-------------- 8 files changed, 175 insertions(+), 75 deletions(-) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 double_team.iml create mode 100644 src/Main.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file 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/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..fb243bb --- /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..1095a15 --- /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..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/double_team.iml b/double_team.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/double_team.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..f2e280e --- /dev/null +++ b/src/Main.java @@ -0,0 +1,23 @@ +/** + * ClassName:${NAME} + * Package: + * Description: + * Author Pjj + * + * @Create 2024/10/10 16:14 + * @Version 1.0 + *///TIP To Run code, press or +// click the icon in the gutter. +public class Main { + public static void main(String[] args) { + //TIP Press with your caret at the highlighted text + // to see how IntelliJ IDEA suggests fixing it. + System.out.printf("Hello and welcome!"); + + for (int i = 1; i <= 5; i++) { + //TIP Press to start debugging your code. We have set one breakpoint + // for you, but you can always add more by pressing . + System.out.println("i = " + i); + } + } +} \ No newline at end of file diff --git a/src/compute/SimpleCalculator.java b/src/compute/SimpleCalculator.java index b1e2456..d09d3cc 100644 --- a/src/compute/SimpleCalculator.java +++ b/src/compute/SimpleCalculator.java @@ -1,6 +1,9 @@ package compute; import javax.swing.*; import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; public class SimpleCalculator { @@ -14,71 +17,71 @@ public class SimpleCalculator { frame = new JFrame("简单计算器"); inputField = new JTextField(); inputField.setEditable(false); + inputField.setFont(new Font("Arial", Font.PLAIN, 24)); + inputField.setHorizontalAlignment(JTextField.RIGHT); // 创建按钮 - JButton btn1 = new JButton("1"); - JButton btn2 = new JButton("2"); - JButton btn3 = new JButton("3"); - JButton btn4 = new JButton("4"); - JButton btn5 = new JButton("5"); - JButton btn6 = new JButton("6"); - JButton btn7 = new JButton("7"); - JButton btn8 = new JButton("8"); - JButton btn9 = new JButton("9"); - JButton btn0 = new JButton("0"); - JButton btnAdd = new JButton("+"); - JButton btnSubtract = new JButton("-"); - JButton btnMultiply = new JButton("*"); - JButton btnDivide = new JButton("/"); - JButton btnEqual = new JButton("="); - JButton btnClear = new JButton("C"); + JButton[] buttons = new JButton[16]; + String[] buttonLabels = {"7", "8", "9", "/", + "4", "5", "6", "*", + "1", "2", "3", "-", + "C", "0", "=", "+"}; + + for (int i = 0; i < buttonLabels.length; i++) { + buttons[i] = new JButton(buttonLabels[i]); + buttons[i].setFont(new Font("Arial", Font.PLAIN, 24)); + buttons[i].setFocusable(false); + buttons[i].setBackground(Color.LIGHT_GRAY); + buttons[i].setBorder(BorderFactory.createLineBorder(Color.GRAY)); + final String label = buttonLabels[i]; + buttons[i].addActionListener(e -> handleButtonClick(label)); + } // 设置布局 JPanel panel = new JPanel(); - panel.setLayout(new GridLayout(4, 4)); - panel.add(btn7); - panel.add(btn8); - panel.add(btn9); - panel.add(btnDivide); - panel.add(btn4); - panel.add(btn5); - panel.add(btn6); - panel.add(btnMultiply); - panel.add(btn1); - panel.add(btn2); - panel.add(btn3); - panel.add(btnSubtract); - panel.add(btn0); - panel.add(btnEqual); - panel.add(btnClear); - panel.add(btnAdd); - - // 添加事件监听器 - btn1.addActionListener(e -> appendToInputField("1")); - btn2.addActionListener(e -> appendToInputField("2")); - btn3.addActionListener(e -> appendToInputField("3")); - btn4.addActionListener(e -> appendToInputField("4")); - btn5.addActionListener(e -> appendToInputField("5")); - btn6.addActionListener(e -> appendToInputField("6")); - btn7.addActionListener(e -> appendToInputField("7")); - btn8.addActionListener(e -> appendToInputField("8")); - btn9.addActionListener(e -> appendToInputField("9")); - btn0.addActionListener(e -> appendToInputField("0")); - - btnAdd.addActionListener(e -> setOperator("+")); - btnSubtract.addActionListener(e -> setOperator("-")); - btnMultiply.addActionListener(e -> setOperator("*")); - btnDivide.addActionListener(e -> setOperator("/")); - - btnEqual.addActionListener(e -> calculateResult()); - btnClear.addActionListener(e -> clearInput()); + panel.setLayout(new GridLayout(4, 4, 10, 10)); + for (JButton button : buttons) { + panel.add(button); + } // 设置窗体 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 400); + frame.setLayout(new BorderLayout()); frame.add(inputField, BorderLayout.NORTH); - frame.add(panel); + frame.add(panel, BorderLayout.CENTER); frame.setVisible(true); + frame.setLocationRelativeTo(null); // 窗口居中 + frame.addKeyListener(new KeyAdapter() { // 支持键盘输入 + @Override + public void keyPressed(KeyEvent e) { + char keyChar = e.getKeyChar(); + if (Character.isDigit(keyChar) || keyChar == '.' || "+-*/=".indexOf(keyChar) >= 0) { + handleButtonClick(String.valueOf(keyChar)); + } + if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { + clearInput(); + } + } + }); + } + + private void handleButtonClick(String label) { + switch (label) { + case "C": + clearInput(); + break; + case "=": + calculateResult(); + break; + default: + if ("+-*/".contains(label)) { + setOperator(label); + } else { + appendToInputField(label); + } + break; + } } private void appendToInputField(String value) { @@ -92,33 +95,39 @@ public class SimpleCalculator { } private void calculateResult() { - num2 = Double.parseDouble(inputField.getText()); - double result = 0; + try { + num2 = Double.parseDouble(inputField.getText()); + double result = 0; - switch (operator) { - case "+": - result = num1 + num2; - break; - case "-": - result = num1 - num2; - break; - case "*": - result = num1 * num2; - break; - case "/": - if (num2 != 0) { - result = num1 / num2; - } else { - JOptionPane.showMessageDialog(frame, "错误:除数不能为零。"); - return; - } - break; + switch (operator) { + case "+": + result = num1 + num2; + break; + case "-": + result = num1 - num2; + break; + case "*": + result = num1 * num2; + break; + case "/": + if (num2 != 0) { + result = num1 / num2; + } else { + JOptionPane.showMessageDialog(frame, "错误:除数不能为零。"); + return; + } + break; + } + inputField.setText(String.valueOf(result)); + } catch (NumberFormatException e) { + JOptionPane.showMessageDialog(frame, "无效输入,请检查。"); } - inputField.setText(String.valueOf(result)); } private void clearInput() { inputField.setText(""); + num1 = num2 = 0; + operator = ""; } public static void main(String[] args) {