From 1fba01512d107c48e2c683548fb6358d16fdb36b Mon Sep 17 00:00:00 2001 From: xiaolai <1372917576@qq.com> Date: Thu, 17 Oct 2024 16:17:55 +0800 Subject: [PATCH 1/2] 1 --- Calculator.java | 116 +++++++++++++++++++----------------------------- 1 file changed, 46 insertions(+), 70 deletions(-) diff --git a/Calculator.java b/Calculator.java index af20010..79d227d 100644 --- a/Calculator.java +++ b/Calculator.java @@ -1,72 +1,48 @@ -import java.util.Scanner; - -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 IllegalArgumentException("Denominator cannot be zero."); - } - return a / b; - } - - // 取余 - public double remainder(double a, double b) { - return a % b; - } - - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - Calculator calc = new Calculator(); - - System.out.println("Enter first number:"); - double num1 = scanner.nextDouble(); - - System.out.println("Enter second number:"); - double num2 = scanner.nextDouble(); - - System.out.println("Choose operation (+, -, *, /, %):"); - char operation = scanner.next().charAt(0); - - double result; - switch (operation) { - case '+': - result = calc.add(num1, num2); - break; - case '-': - result = calc.subtract(num1, num2); - break; - case '*': - result = calc.multiply(num1, num2); - break; - case '/': - result = calc.divide(num1, num2); - break; - case '%': - result = calc.remainder(num1, num2); - break; - default: - System.out.println("Invalid operation"); - scanner.close(); - return; +import javax.swing.*; +import java.awt.*; + + +public class SimpleCalculator extends JFrame { + + private JTextField display; + private JPanel panel; + private double operand1 = 0; + private double operand2 = 0; + private char operator; + private boolean userIsTypingSecondNumber = false; + + public SimpleCalculator() { + setTitle("Simple Calculator"); + setSize(400, 500); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLocationRelativeTo(null); + + // Display text field + display = new JTextField(); + display.setEditable(false); + display.setHorizontalAlignment(SwingConstants.RIGHT); + add(display, BorderLayout.NORTH); + + // Panel for buttons + panel = new JPanel(); + panel.setLayout(new GridLayout(5, 4, 10, 10)); + + // Add numbers and operations to panel + String[] buttons = { + "7", "8", "9", "/", + "4", "5", "6", "*", + "1", "2", "3", "-", + "0", ".", "=", "+", + "C" + }; + + for (String text : buttons) { + JButton button = new JButton(text); + button.setFont(new Font("Verdana", Font.PLAIN, 24)); + button.setFocusable(false); + panel.add(button); + button.addActionListener(new ButtonClickListener()); } - System.out.println("Result: " + result); - scanner.close(); - } -} \ No newline at end of file + add(panel, BorderLayout.CENTER); + } \ No newline at end of file From 158acf6c8d215e691e4b4c602b54532937f81d46 Mon Sep 17 00:00:00 2001 From: xiaolai <1372917576@qq.com> Date: Thu, 17 Oct 2024 16:24:25 +0800 Subject: [PATCH 2/2] 2 --- Calculator.java => SimpleCalculator.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Calculator.java => SimpleCalculator.java (100%) diff --git a/Calculator.java b/SimpleCalculator.java similarity index 100% rename from Calculator.java rename to SimpleCalculator.java