parent
							
								
									5f2968f652
								
							
						
					
					
						commit
						9c5a32ec7e
					
				| @ -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; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| @ -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); | ||||
|         }); | ||||
|     } | ||||
| } | ||||
					Loading…
					
					
				
		Reference in new issue