Compare commits

..

4 Commits
main ... master

@ -1,4 +1,5 @@
package jisuanji; package jisuanji;
public class Calculator { public class Calculator {
// 加法 // 加法
public double add(double a, double b) { public double add(double a, double b) {
@ -17,15 +18,18 @@ public class Calculator {
// 除法 // 除法
public double divide(double a, double b) { public double divide(double a, double b) {
if (b != 0) { if (b == 0) {
return a / b; throw new ArithmeticException("除数不能为零");
} else {
throw new IllegalArgumentException("除数不能为零");
} }
return a / b;
} }
// 取余 // 取余
public double modulus(double a, double b) { public double modulus(double a, double b) {
if (b == 0) {
throw new ArithmeticException("除数不能为零");
}
return a % b; return a % b;
} }
} }

@ -5,88 +5,156 @@ import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
public class CalculatorGUI extends JFrame { public class CalculatorGUI extends JFrame implements ActionListener {
private JTextField display; private JTextField display;
private Calculator calculator = new Calculator(); private JPanel panel;
private double firstNumber = 0; private JButton[] numberButtons;
private double secondNumber = 0; private JButton[] functionButtons;
private String operation = ""; private JButton addButton, subButton, mulButton, divButton, modButton, equalsButton, clearButton;
private boolean isOperationClicked = false; private Calculator calculator;
public CalculatorGUI() { public CalculatorGUI() {
setTitle("简易计算器"); calculator = new Calculator();
setSize(400, 600); setTitle("Simple Calculator");
setSize(400, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); setLocationRelativeTo(null);
display = new JTextField(); display = new JTextField();
display.setEditable(false); display.setEditable(false);
display.setFont(new Font("Arial", Font.PLAIN, 40)); display.setFont(new Font("Arial", Font.BOLD, 24));
display.setHorizontalAlignment(JTextField.RIGHT); add(display, BorderLayout.NORTH);
display.setBorder(BorderFactory.createLineBorder(Color.BLACK));
panel = new JPanel();
JPanel panel = new JPanel(); panel.setLayout(new GridLayout(5, 4, 10, 10));
panel.setLayout(new GridLayout(4, 4, 10, 10));
// 数字按钮
String[] buttons = { numberButtons = new JButton[10];
"7", "8", "9", "/", for (int i = 0; i < 10; i++) {
"4", "5", "6", "*", numberButtons[i] = new JButton(String.valueOf(i));
"1", "2", "3", "-", numberButtons[i].setFont(new Font("Arial", Font.BOLD, 24));
"0", ".", "=", "+" numberButtons[i].addActionListener(this);
}; panel.add(numberButtons[i]);
for (String buttonText : buttons) {
JButton button = new JButton(buttonText);
button.setFont(new Font("Arial", Font.PLAIN, 24));
button.addActionListener(new ButtonClickListener());
panel.add(button);
} }
add(display, BorderLayout.NORTH); // 功能按钮
add(panel, BorderLayout.CENTER); 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));
setVisible(true); 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);
} }
private class ButtonClickListener implements ActionListener {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand(); for (int i = 0; i < 10; i++) {
if (e.getSource() == numberButtons[i]) {
display.setText(display.getText() + i);
}
}
if ("0123456789.".indexOf(command) >= 0) { if (e.getSource() == addButton) {
display.setText(display.getText() + command); currentOperation = '+';
isOperationClicked = false; firstNumber = Double.parseDouble(display.getText());
} else if ("+*/-=".indexOf(command) >= 0) { display.setText("");
if (!isOperationClicked) { } else if (e.getSource() == subButton) {
currentOperation = '-';
firstNumber = Double.parseDouble(display.getText()); firstNumber = Double.parseDouble(display.getText());
operation = command;
isOperationClicked = true;
display.setText(""); display.setText("");
} else if (command.equals("=")) { } 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()); secondNumber = Double.parseDouble(display.getText());
double result = 0; double result = 0;
switch (operation) { switch (currentOperation) {
case "+": case '+':
result = calculator.add(firstNumber, secondNumber); result = calculator.add(firstNumber, secondNumber);
break; break;
case "-": case '-':
result = calculator.subtract(firstNumber, secondNumber); result = calculator.subtract(firstNumber, secondNumber);
break; break;
case "*": case '*':
result = calculator.multiply(firstNumber, secondNumber); result = calculator.multiply(firstNumber, secondNumber);
break; break;
case "/": case '/':
try {
result = calculator.divide(firstNumber, secondNumber); result = calculator.divide(firstNumber, secondNumber);
break; } catch (ArithmeticException ex) {
display.setText("错误: 除数不能为零");
return;
} }
display.setText(String.valueOf(result)); break;
isOperationClicked = false; 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) { public static void main(String[] args) {
new CalculatorGUI(); SwingUtilities.invokeLater(() -> {
CalculatorGUI calculatorGUI = new CalculatorGUI();
calculatorGUI.setVisible(true);
});
} }
} }

@ -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);
});
}
}

@ -0,0 +1,33 @@
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,158 @@
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…
Cancel
Save