|
|
@ -1,9 +1,7 @@
|
|
|
|
package compute;
|
|
|
|
package compute;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
|
|
|
import java.awt.event.KeyAdapter;
|
|
|
|
|
|
|
|
import java.awt.event.KeyEvent;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class SimpleCalculator {
|
|
|
|
public class SimpleCalculator {
|
|
|
|
|
|
|
|
|
|
|
@ -12,80 +10,126 @@ public class SimpleCalculator {
|
|
|
|
private double num1;
|
|
|
|
private double num1;
|
|
|
|
private double num2;
|
|
|
|
private double num2;
|
|
|
|
private String operator;
|
|
|
|
private String operator;
|
|
|
|
|
|
|
|
private double memory; // Memory for M+, M-, MR, and MC functionality
|
|
|
|
|
|
|
|
|
|
|
|
public SimpleCalculator() {
|
|
|
|
public SimpleCalculator() {
|
|
|
|
frame = new JFrame("简单计算器");
|
|
|
|
frame = new JFrame("简单计算器pjj");
|
|
|
|
inputField = new JTextField();
|
|
|
|
inputField = new JTextField();
|
|
|
|
inputField.setEditable(false);
|
|
|
|
inputField.setEditable(false);
|
|
|
|
inputField.setFont(new Font("Arial", Font.PLAIN, 24));
|
|
|
|
inputField.setHorizontalAlignment(SwingConstants.RIGHT);
|
|
|
|
inputField.setHorizontalAlignment(JTextField.RIGHT);
|
|
|
|
inputField.setFont(new Font("Arial", Font.BOLD, 24));
|
|
|
|
|
|
|
|
inputField.setPreferredSize(new Dimension(400, 50));
|
|
|
|
|
|
|
|
|
|
|
|
// 创建按钮
|
|
|
|
// 创建按钮
|
|
|
|
JButton[] buttons = new JButton[16];
|
|
|
|
JButton btn1 = createButton("1");
|
|
|
|
String[] buttonLabels = {"7", "8", "9", "/",
|
|
|
|
JButton btn2 = createButton("2");
|
|
|
|
"4", "5", "6", "*",
|
|
|
|
JButton btn3 = createButton("3");
|
|
|
|
"1", "2", "3", "-",
|
|
|
|
JButton btn4 = createButton("4");
|
|
|
|
"C", "0", "=", "+"};
|
|
|
|
JButton btn5 = createButton("5");
|
|
|
|
|
|
|
|
JButton btn6 = createButton("6");
|
|
|
|
for (int i = 0; i < buttonLabels.length; i++) {
|
|
|
|
JButton btn7 = createButton("7");
|
|
|
|
buttons[i] = new JButton(buttonLabels[i]);
|
|
|
|
JButton btn8 = createButton("8");
|
|
|
|
buttons[i].setFont(new Font("Arial", Font.PLAIN, 24));
|
|
|
|
JButton btn9 = createButton("9");
|
|
|
|
buttons[i].setFocusable(false);
|
|
|
|
JButton btn0 = createButton("0");
|
|
|
|
buttons[i].setBackground(Color.LIGHT_GRAY);
|
|
|
|
JButton btnAdd = createButton("+");
|
|
|
|
buttons[i].setBorder(BorderFactory.createLineBorder(Color.GRAY));
|
|
|
|
JButton btnSubtract = createButton("-");
|
|
|
|
final String label = buttonLabels[i];
|
|
|
|
JButton btnMultiply = createButton("*");
|
|
|
|
buttons[i].addActionListener(e -> handleButtonClick(label));
|
|
|
|
JButton btnDivide = createButton("/");
|
|
|
|
}
|
|
|
|
JButton btnEqual = createButton("=");
|
|
|
|
|
|
|
|
JButton btnClear = createButton("C");
|
|
|
|
|
|
|
|
JButton btnDecimal = createButton(".");
|
|
|
|
|
|
|
|
JButton btnNegate = createButton("+/-");
|
|
|
|
|
|
|
|
JButton btnMPlus = createButton("M+");
|
|
|
|
|
|
|
|
JButton btnMMinus = createButton("M-");
|
|
|
|
|
|
|
|
JButton btnMR = createButton("MR");
|
|
|
|
|
|
|
|
JButton btnMC = createButton("MC");
|
|
|
|
|
|
|
|
|
|
|
|
// 设置布局
|
|
|
|
// 设置布局
|
|
|
|
JPanel panel = new JPanel();
|
|
|
|
JPanel panel = new JPanel();
|
|
|
|
panel.setLayout(new GridLayout(4, 4, 10, 10));
|
|
|
|
panel.setLayout(new GridLayout(6, 4, 5, 5)); // 网格布局,间距为5像素
|
|
|
|
for (JButton button : buttons) {
|
|
|
|
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
|
|
|
|
panel.add(button);
|
|
|
|
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(btnDecimal);
|
|
|
|
|
|
|
|
panel.add(btnNegate);
|
|
|
|
|
|
|
|
panel.add(btnAdd);
|
|
|
|
|
|
|
|
panel.add(btnClear);
|
|
|
|
|
|
|
|
panel.add(btnEqual);
|
|
|
|
|
|
|
|
panel.add(btnMPlus);
|
|
|
|
|
|
|
|
panel.add(btnMMinus);
|
|
|
|
|
|
|
|
panel.add(btnMR);
|
|
|
|
|
|
|
|
panel.add(btnMC);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 添加事件监听器
|
|
|
|
|
|
|
|
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"));
|
|
|
|
|
|
|
|
btnDecimal.addActionListener(e -> appendDecimal());
|
|
|
|
|
|
|
|
btnNegate.addActionListener(e -> negateValue());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
btnAdd.addActionListener(e -> setOperator("+"));
|
|
|
|
|
|
|
|
btnSubtract.addActionListener(e -> setOperator("-"));
|
|
|
|
|
|
|
|
btnMultiply.addActionListener(e -> setOperator("*"));
|
|
|
|
|
|
|
|
btnDivide.addActionListener(e -> setOperator("/"));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
btnEqual.addActionListener(e -> calculateResult());
|
|
|
|
|
|
|
|
btnClear.addActionListener(e -> clearInput());
|
|
|
|
|
|
|
|
btnMPlus.addActionListener(e -> memoryPlus());
|
|
|
|
|
|
|
|
btnMMinus.addActionListener(e -> memoryMinus());
|
|
|
|
|
|
|
|
btnMR.addActionListener(e -> memoryRecall());
|
|
|
|
|
|
|
|
btnMC.addActionListener(e -> memoryClear());
|
|
|
|
|
|
|
|
|
|
|
|
// 设置窗体
|
|
|
|
// 设置窗体
|
|
|
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
frame.setSize(400, 400);
|
|
|
|
frame.setSize(400, 600); // 调整窗体大小
|
|
|
|
frame.setLayout(new BorderLayout());
|
|
|
|
frame.setLayout(new BorderLayout(5, 5));
|
|
|
|
frame.add(inputField, BorderLayout.NORTH);
|
|
|
|
frame.add(inputField, BorderLayout.NORTH);
|
|
|
|
frame.add(panel, BorderLayout.CENTER);
|
|
|
|
frame.add(panel, BorderLayout.CENTER);
|
|
|
|
frame.setVisible(true);
|
|
|
|
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 JButton createButton(String text) {
|
|
|
|
|
|
|
|
JButton button = new JButton(text);
|
|
|
|
|
|
|
|
button.setFont(new Font("Arial", Font.PLAIN, 18));
|
|
|
|
|
|
|
|
button.setPreferredSize(new Dimension(80, 80));
|
|
|
|
|
|
|
|
button.setBackground(Color.LIGHT_GRAY);
|
|
|
|
|
|
|
|
button.setFocusPainted(false);
|
|
|
|
|
|
|
|
return button;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void handleButtonClick(String label) {
|
|
|
|
private void appendToInputField(String value) {
|
|
|
|
switch (label) {
|
|
|
|
inputField.setText(inputField.getText() + value);
|
|
|
|
case "C":
|
|
|
|
|
|
|
|
clearInput();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "=":
|
|
|
|
|
|
|
|
calculateResult();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
if ("+-*/".contains(label)) {
|
|
|
|
|
|
|
|
setOperator(label);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
appendToInputField(label);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
private void appendDecimal() {
|
|
|
|
|
|
|
|
if (!inputField.getText().contains(".")) {
|
|
|
|
|
|
|
|
inputField.setText(inputField.getText() + ".");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void appendToInputField(String value) {
|
|
|
|
private void negateValue() {
|
|
|
|
inputField.setText(inputField.getText() + value);
|
|
|
|
if (!inputField.getText().isEmpty()) {
|
|
|
|
|
|
|
|
double value = Double.parseDouble(inputField.getText());
|
|
|
|
|
|
|
|
inputField.setText(String.valueOf(value * -1));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void setOperator(String op) {
|
|
|
|
private void setOperator(String op) {
|
|
|
@ -120,16 +164,42 @@ public class SimpleCalculator {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inputField.setText(String.valueOf(result));
|
|
|
|
inputField.setText(String.valueOf(result));
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
JOptionPane.showMessageDialog(frame, "无效输入,请检查。");
|
|
|
|
JOptionPane.showMessageDialog(frame, "错误:无效的输入。");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void clearInput() {
|
|
|
|
private void clearInput() {
|
|
|
|
inputField.setText("");
|
|
|
|
inputField.setText("");
|
|
|
|
num1 = num2 = 0;
|
|
|
|
num1 = 0;
|
|
|
|
|
|
|
|
num2 = 0;
|
|
|
|
operator = "";
|
|
|
|
operator = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void memoryPlus() {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
memory += Double.parseDouble(inputField.getText());
|
|
|
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
|
|
|
JOptionPane.showMessageDialog(frame, "错误:无效的输入。");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void memoryMinus() {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
memory -= Double.parseDouble(inputField.getText());
|
|
|
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
|
|
|
JOptionPane.showMessageDialog(frame, "错误:无效的输入。");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void memoryRecall() {
|
|
|
|
|
|
|
|
inputField.setText(String.valueOf(memory));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void memoryClear() {
|
|
|
|
|
|
|
|
memory = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
SwingUtilities.invokeLater(SimpleCalculator::new);
|
|
|
|
SwingUtilities.invokeLater(SimpleCalculator::new);
|
|
|
|
}
|
|
|
|
}
|
|
|
|