|
|
|
@ -1,4 +1,5 @@
|
|
|
|
|
package compute;
|
|
|
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
|
|
|
|
@ -9,33 +10,44 @@ public class SimpleCalculator {
|
|
|
|
|
private double num1;
|
|
|
|
|
private double num2;
|
|
|
|
|
private String operator;
|
|
|
|
|
private double memory; // Memory for M+, M-, MR, and MC functionality
|
|
|
|
|
|
|
|
|
|
public SimpleCalculator() {
|
|
|
|
|
frame = new JFrame("简单计算器");
|
|
|
|
|
inputField = new JTextField();
|
|
|
|
|
inputField.setEditable(false);
|
|
|
|
|
inputField.setHorizontalAlignment(SwingConstants.RIGHT);
|
|
|
|
|
inputField.setFont(new Font("Arial", Font.BOLD, 24));
|
|
|
|
|
inputField.setPreferredSize(new Dimension(400, 50));
|
|
|
|
|
|
|
|
|
|
// 创建按钮
|
|
|
|
|
JButton btn1 = new JButton("1");
|
|
|
|
|
JButton btn2 = new JButton("2");
|
|
|
|
|
JButton btn3 = new JButton("3");
|
|
|
|
|
JButton btn4 = new JButton("4");
|
|
|
|
|
JButton btn5 = new JButton("5");
|
|
|
|
|
JButton btn6 = new JButton("6");
|
|
|
|
|
JButton btn7 = new JButton("7");
|
|
|
|
|
JButton btn8 = new JButton("8");
|
|
|
|
|
JButton btn9 = new JButton("9");
|
|
|
|
|
JButton btn0 = new JButton("0");
|
|
|
|
|
JButton btnAdd = new JButton("+");
|
|
|
|
|
JButton btnSubtract = new JButton("-");
|
|
|
|
|
JButton btnMultiply = new JButton("*");
|
|
|
|
|
JButton btnDivide = new JButton("/");
|
|
|
|
|
JButton btnEqual = new JButton("=");
|
|
|
|
|
JButton btnClear = new JButton("C");
|
|
|
|
|
JButton btn1 = createButton("1");
|
|
|
|
|
JButton btn2 = createButton("2");
|
|
|
|
|
JButton btn3 = createButton("3");
|
|
|
|
|
JButton btn4 = createButton("4");
|
|
|
|
|
JButton btn5 = createButton("5");
|
|
|
|
|
JButton btn6 = createButton("6");
|
|
|
|
|
JButton btn7 = createButton("7");
|
|
|
|
|
JButton btn8 = createButton("8");
|
|
|
|
|
JButton btn9 = createButton("9");
|
|
|
|
|
JButton btn0 = createButton("0");
|
|
|
|
|
JButton btnAdd = createButton("+");
|
|
|
|
|
JButton btnSubtract = createButton("-");
|
|
|
|
|
JButton btnMultiply = createButton("*");
|
|
|
|
|
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();
|
|
|
|
|
panel.setLayout(new GridLayout(4, 4));
|
|
|
|
|
panel.setLayout(new GridLayout(6, 4, 5, 5)); // 网格布局,间距为5像素
|
|
|
|
|
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
|
|
|
|
|
panel.add(btn7);
|
|
|
|
|
panel.add(btn8);
|
|
|
|
|
panel.add(btn9);
|
|
|
|
@ -49,9 +61,15 @@ public class SimpleCalculator {
|
|
|
|
|
panel.add(btn3);
|
|
|
|
|
panel.add(btnSubtract);
|
|
|
|
|
panel.add(btn0);
|
|
|
|
|
panel.add(btnEqual);
|
|
|
|
|
panel.add(btnClear);
|
|
|
|
|
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"));
|
|
|
|
@ -64,6 +82,8 @@ public class SimpleCalculator {
|
|
|
|
|
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("-"));
|
|
|
|
@ -72,19 +92,46 @@ public class SimpleCalculator {
|
|
|
|
|
|
|
|
|
|
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.setSize(400, 400);
|
|
|
|
|
frame.setSize(400, 600); // 调整窗体大小
|
|
|
|
|
frame.setLayout(new BorderLayout(5, 5));
|
|
|
|
|
frame.add(inputField, BorderLayout.NORTH);
|
|
|
|
|
frame.add(panel);
|
|
|
|
|
frame.add(panel, BorderLayout.CENTER);
|
|
|
|
|
frame.setVisible(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 appendToInputField(String value) {
|
|
|
|
|
inputField.setText(inputField.getText() + value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void appendDecimal() {
|
|
|
|
|
if (!inputField.getText().contains(".")) {
|
|
|
|
|
inputField.setText(inputField.getText() + ".");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void negateValue() {
|
|
|
|
|
if (!inputField.getText().isEmpty()) {
|
|
|
|
|
double value = Double.parseDouble(inputField.getText());
|
|
|
|
|
inputField.setText(String.valueOf(value * -1));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setOperator(String op) {
|
|
|
|
|
num1 = Double.parseDouble(inputField.getText());
|
|
|
|
|
operator = op;
|
|
|
|
@ -92,33 +139,64 @@ public class SimpleCalculator {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void calculateResult() {
|
|
|
|
|
num2 = Double.parseDouble(inputField.getText());
|
|
|
|
|
double result = 0;
|
|
|
|
|
|
|
|
|
|
switch (operator) {
|
|
|
|
|
case "+":
|
|
|
|
|
result = num1 + num2;
|
|
|
|
|
break;
|
|
|
|
|
case "-":
|
|
|
|
|
result = num1 - num2;
|
|
|
|
|
break;
|
|
|
|
|
case "*":
|
|
|
|
|
result = num1 * num2;
|
|
|
|
|
break;
|
|
|
|
|
case "/":
|
|
|
|
|
if (num2 != 0) {
|
|
|
|
|
result = num1 / num2;
|
|
|
|
|
} else {
|
|
|
|
|
JOptionPane.showMessageDialog(frame, "错误:除数不能为零。");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
try {
|
|
|
|
|
num2 = Double.parseDouble(inputField.getText());
|
|
|
|
|
double result = 0;
|
|
|
|
|
|
|
|
|
|
switch (operator) {
|
|
|
|
|
case "+":
|
|
|
|
|
result = num1 + num2;
|
|
|
|
|
break;
|
|
|
|
|
case "-":
|
|
|
|
|
result = num1 - num2;
|
|
|
|
|
break;
|
|
|
|
|
case "*":
|
|
|
|
|
result = num1 * num2;
|
|
|
|
|
break;
|
|
|
|
|
case "/":
|
|
|
|
|
if (num2 != 0) {
|
|
|
|
|
result = num1 / num2;
|
|
|
|
|
} else {
|
|
|
|
|
JOptionPane.showMessageDialog(frame, "错误:除数不能为零。");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
inputField.setText(String.valueOf(result));
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
JOptionPane.showMessageDialog(frame, "错误:无效的输入。");
|
|
|
|
|
}
|
|
|
|
|
inputField.setText(String.valueOf(result));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void clearInput() {
|
|
|
|
|
inputField.setText("");
|
|
|
|
|
num1 = 0;
|
|
|
|
|
num2 = 0;
|
|
|
|
|
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) {
|
|
|
|
|