|
|
|
@ -1,6 +1,9 @@
|
|
|
|
|
package compute;
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
import java.awt.event.KeyAdapter;
|
|
|
|
|
import java.awt.event.KeyEvent;
|
|
|
|
|
|
|
|
|
|
public class SimpleCalculator {
|
|
|
|
|
|
|
|
|
@ -14,71 +17,71 @@ public class SimpleCalculator {
|
|
|
|
|
frame = new JFrame("简单计算器");
|
|
|
|
|
inputField = new JTextField();
|
|
|
|
|
inputField.setEditable(false);
|
|
|
|
|
inputField.setFont(new Font("Arial", Font.PLAIN, 24));
|
|
|
|
|
inputField.setHorizontalAlignment(JTextField.RIGHT);
|
|
|
|
|
|
|
|
|
|
// 创建按钮
|
|
|
|
|
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[] buttons = new JButton[16];
|
|
|
|
|
String[] buttonLabels = {"7", "8", "9", "/",
|
|
|
|
|
"4", "5", "6", "*",
|
|
|
|
|
"1", "2", "3", "-",
|
|
|
|
|
"C", "0", "=", "+"};
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < buttonLabels.length; i++) {
|
|
|
|
|
buttons[i] = new JButton(buttonLabels[i]);
|
|
|
|
|
buttons[i].setFont(new Font("Arial", Font.PLAIN, 24));
|
|
|
|
|
buttons[i].setFocusable(false);
|
|
|
|
|
buttons[i].setBackground(Color.LIGHT_GRAY);
|
|
|
|
|
buttons[i].setBorder(BorderFactory.createLineBorder(Color.GRAY));
|
|
|
|
|
final String label = buttonLabels[i];
|
|
|
|
|
buttons[i].addActionListener(e -> handleButtonClick(label));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置布局
|
|
|
|
|
JPanel panel = new JPanel();
|
|
|
|
|
panel.setLayout(new GridLayout(4, 4));
|
|
|
|
|
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(btnEqual);
|
|
|
|
|
panel.add(btnClear);
|
|
|
|
|
panel.add(btnAdd);
|
|
|
|
|
|
|
|
|
|
// 添加事件监听器
|
|
|
|
|
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"));
|
|
|
|
|
|
|
|
|
|
btnAdd.addActionListener(e -> setOperator("+"));
|
|
|
|
|
btnSubtract.addActionListener(e -> setOperator("-"));
|
|
|
|
|
btnMultiply.addActionListener(e -> setOperator("*"));
|
|
|
|
|
btnDivide.addActionListener(e -> setOperator("/"));
|
|
|
|
|
|
|
|
|
|
btnEqual.addActionListener(e -> calculateResult());
|
|
|
|
|
btnClear.addActionListener(e -> clearInput());
|
|
|
|
|
panel.setLayout(new GridLayout(4, 4, 10, 10));
|
|
|
|
|
for (JButton button : buttons) {
|
|
|
|
|
panel.add(button);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置窗体
|
|
|
|
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
|
frame.setSize(400, 400);
|
|
|
|
|
frame.setLayout(new BorderLayout());
|
|
|
|
|
frame.add(inputField, BorderLayout.NORTH);
|
|
|
|
|
frame.add(panel);
|
|
|
|
|
frame.add(panel, BorderLayout.CENTER);
|
|
|
|
|
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 void handleButtonClick(String label) {
|
|
|
|
|
switch (label) {
|
|
|
|
|
case "C":
|
|
|
|
|
clearInput();
|
|
|
|
|
break;
|
|
|
|
|
case "=":
|
|
|
|
|
calculateResult();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
if ("+-*/".contains(label)) {
|
|
|
|
|
setOperator(label);
|
|
|
|
|
} else {
|
|
|
|
|
appendToInputField(label);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void appendToInputField(String value) {
|
|
|
|
@ -92,6 +95,7 @@ public class SimpleCalculator {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void calculateResult() {
|
|
|
|
|
try {
|
|
|
|
|
num2 = Double.parseDouble(inputField.getText());
|
|
|
|
|
double result = 0;
|
|
|
|
|
|
|
|
|
@ -115,10 +119,15 @@ public class SimpleCalculator {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
inputField.setText(String.valueOf(result));
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
JOptionPane.showMessageDialog(frame, "无效输入,请检查。");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void clearInput() {
|
|
|
|
|
inputField.setText("");
|
|
|
|
|
num1 = num2 = 0;
|
|
|
|
|
operator = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|