From 9c5a32ec7eb1ef27d59f675bc064e7dd99a68a7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=99=A8=E7=BA=B2?= <3864568673@qq.com> Date: Thu, 10 Oct 2024 19:11:47 +0800 Subject: [PATCH] =?UTF-8?q?=E9=AB=98=E6=99=A8=E7=BA=B2=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jisuanji11/Calculator.java | 35 ++++++++ jisuanji11/CalculatorGUI.java | 160 ++++++++++++++++++++++++++++++++++ 2 files changed, 195 insertions(+) create mode 100644 jisuanji11/Calculator.java create mode 100644 jisuanji11/CalculatorGUI.java diff --git a/jisuanji11/Calculator.java b/jisuanji11/Calculator.java new file mode 100644 index 0000000..784d702 --- /dev/null +++ b/jisuanji11/Calculator.java @@ -0,0 +1,35 @@ +package jisuanji11; + +public class Calculator { + // 加法 + public double add(double a, double b) { + return a + b; + } + + // 减法 + public double subtract(double a, double b) { + return a - b; + } + + // 乘法 + public double multiply(double a, double b) { + return a * b; + } + + // 除法 + public double divide(double a, double b) { + if (b == 0) { + throw new ArithmeticException("除数不能为零"); + } + return a / b; + } + + // 取余 + public double modulus(double a, double b) { + if (b == 0) { + throw new ArithmeticException("除数不能为零"); + } + return a % b; + } +} + diff --git a/jisuanji11/CalculatorGUI.java b/jisuanji11/CalculatorGUI.java new file mode 100644 index 0000000..e8d957e --- /dev/null +++ b/jisuanji11/CalculatorGUI.java @@ -0,0 +1,160 @@ +package jisuanji11; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class CalculatorGUI extends JFrame implements ActionListener { + private JTextField display; + private JPanel panel; + private JButton[] numberButtons; + private JButton[] functionButtons; + private JButton addButton, subButton, mulButton, divButton, modButton, equalsButton, clearButton; + private Calculator calculator; + + public CalculatorGUI() { + calculator = new Calculator(); + setTitle("Simple Calculator"); + setSize(400, 500); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLocationRelativeTo(null); + + display = new JTextField(); + display.setEditable(false); + display.setFont(new Font("Arial", Font.BOLD, 24)); + add(display, BorderLayout.NORTH); + + panel = new JPanel(); + panel.setLayout(new GridLayout(5, 4, 10, 10)); + + // 数字按钮 + numberButtons = new JButton[10]; + for (int i = 0; i < 10; i++) { + numberButtons[i] = new JButton(String.valueOf(i)); + numberButtons[i].setFont(new Font("Arial", Font.BOLD, 24)); + numberButtons[i].addActionListener(this); + panel.add(numberButtons[i]); + } + + // 功能按钮 + addButton = new JButton("+"); + subButton = new JButton("-"); + mulButton = new JButton("*"); + divButton = new JButton("/"); + modButton = new JButton("%"); + equalsButton = new JButton("="); + clearButton = new JButton("C"); + + addButton.setFont(new Font("Arial", Font.BOLD, 24)); + subButton.setFont(new Font("Arial", Font.BOLD, 24)); + mulButton.setFont(new Font("Arial", Font.BOLD, 24)); + divButton.setFont(new Font("Arial", Font.BOLD, 24)); + modButton.setFont(new Font("Arial", Font.BOLD, 24)); + equalsButton.setFont(new Font("Arial", Font.BOLD, 24)); + clearButton.setFont(new Font("Arial", Font.BOLD, 24)); + + addButton.addActionListener(this); + subButton.addActionListener(this); + mulButton.addActionListener(this); + divButton.addActionListener(this); + modButton.addActionListener(this); + equalsButton.addActionListener(this); + clearButton.addActionListener(this); + + panel.add(addButton); + panel.add(subButton); + panel.add(mulButton); + panel.add(divButton); + panel.add(modButton); + panel.add(equalsButton); + panel.add(clearButton); + + // 空白按钮占位 + JButton space1 = new JButton(" "); + JButton space2 = new JButton(" "); + panel.add(space1); + panel.add(space2); + + add(panel, BorderLayout.CENTER); + } + + @Override + public void actionPerformed(ActionEvent e) { + for (int i = 0; i < 10; i++) { + if (e.getSource() == numberButtons[i]) { + display.setText(display.getText() + i); + } + } + + if (e.getSource() == addButton) { + currentOperation = '+'; + firstNumber = Double.parseDouble(display.getText()); + display.setText(""); + } else if (e.getSource() == subButton) { + currentOperation = '-'; + firstNumber = Double.parseDouble(display.getText()); + display.setText(""); + } else if (e.getSource() == mulButton) { + currentOperation = '*'; + firstNumber = Double.parseDouble(display.getText()); + display.setText(""); + } else if (e.getSource() == divButton) { + currentOperation = '/'; + firstNumber = Double.parseDouble(display.getText()); + display.setText(""); + } else if (e.getSource() == modButton) { + currentOperation = '%'; + firstNumber = Double.parseDouble(display.getText()); + display.setText(""); + } else if (e.getSource() == equalsButton) { + secondNumber = Double.parseDouble(display.getText()); + double result = 0; + switch (currentOperation) { + case '+': + result = calculator.add(firstNumber, secondNumber); + break; + case '-': + result = calculator.subtract(firstNumber, secondNumber); + break; + case '*': + result = calculator.multiply(firstNumber, secondNumber); + break; + case '/': + try { + result = calculator.divide(firstNumber, secondNumber); + } catch (ArithmeticException ex) { + display.setText("错误: 除数不能为零"); + return; + } + break; + case '%': + try { + result = calculator.modulus(firstNumber, secondNumber); + } catch (ArithmeticException ex) { + display.setText("错误: 除数不能为零"); + return; + } + break; + } + display.setText(String.valueOf(result)); + currentOperation = '\0'; // 重置操作符 + } else if (e.getSource() == clearButton) { + display.setText(""); + currentOperation = '\0'; // 重置操作符 + firstNumber = 0; + secondNumber = 0; + } + } + + private char currentOperation = '\0'; + private double firstNumber = 0; + private double secondNumber = 0; + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + CalculatorGUI calculatorGUI = new CalculatorGUI(); + calculatorGUI.setVisible(true); + }); + } +} \ No newline at end of file