You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
5.6 KiB

2 months ago
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.function.BiFunction;
public class Operate {
private JFrame frame;
private JTextField displayField;
private double firstNumber = 0;
private double secondNumber = 0;
private String operator = "";
private boolean newNumber = true;
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
try {
Operate window = new Operate();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
});
}
public Operate() {
initialize();
}
private void initialize() {
frame = new JFrame("Improved Calculator");
frame.setBounds(100, 100, 300, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
frame.setLocationRelativeTo(null);
// 显示区域
displayField = new JTextField();
displayField.setEditable(false);
displayField.setFont(new Font("Arial", Font.BOLD, 20));
displayField.setHorizontalAlignment(JTextField.RIGHT);
frame.getContentPane().add(displayField, BorderLayout.NORTH);
// 按钮面板
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(5, 4));
frame.getContentPane().add(buttonPanel, BorderLayout.CENTER);
// 数字按钮
for (int i = 1; i <= 9; i++) {
JButton button = new JButton(Integer.toString(i));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (newNumber) {
displayField.setText(e.getActionCommand());
newNumber = false;
} else {
displayField.setText(displayField.getText() + e.getActionCommand());
}
}
});
buttonPanel.add(button);
if (i % 3 == 0) buttonPanel.add(new JButton()); // 空位
}
// 添加0按钮
JButton buttonZero = new JButton("0");
buttonZero.addActionListener(e -> {
if (!displayField.getText().equals("0")) {
displayField.setText(displayField.getText() + "0");
}
});
buttonPanel.add(buttonZero);
// 小数点按钮
JButton buttonDecimal = new JButton(".");
buttonDecimal.addActionListener(e -> {
if (!displayField.getText().contains(".")) {
displayField.setText(displayField.getText() + ".");
}
});
buttonPanel.add(buttonDecimal);
// 运算符按钮
addOperatorButton(buttonPanel, "+", (a, b) -> a + b);
addOperatorButton(buttonPanel, "-", (a, b) -> a - b);
addOperatorButton(buttonPanel, "*", (a, b) -> a * b);
addOperatorButton(buttonPanel, "/", (a, b) -> {
if (b == 0) {
JOptionPane.showMessageDialog(frame, "除数不能为0", "错误", JOptionPane.ERROR_MESSAGE);
return Double.NaN;
}
return a / b;
});
// 清除按钮
JButton clearButton = new JButton("C");
clearButton.addActionListener(e -> {
displayField.setText("");
firstNumber = 0;
secondNumber = 0;
operator = "";
newNumber = true;
});
buttonPanel.add(clearButton);
// 等于号按钮
JButton equalsButton = new JButton("=");
equalsButton.addActionListener(e -> calculate());
buttonPanel.add(equalsButton);
// 设置按钮样式
setButtonStyle(buttonPanel);
}
private void addOperatorButton(JPanel panel, String op, BiFunction<Double, Double, Double> operation) {
JButton button = new JButton(op);
button.addActionListener(e -> {
firstNumber = Double.parseDouble(displayField.getText());
operator = op;
// 重置 displayField 的内容
displayField.setText("");
newNumber = true;
});
panel.add(button);
}
private void calculate() {
secondNumber = Double.parseDouble(displayField.getText());
switch (operator) {
case "+":
displayField.setText(String.valueOf(firstNumber + secondNumber));
break;
case "-":
displayField.setText(String.valueOf(firstNumber - secondNumber));
break;
case "*":
displayField.setText(String.valueOf(firstNumber * secondNumber));
break;
case "/":
if (secondNumber == 0) {
JOptionPane.showMessageDialog(frame, "除数不能为0", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
displayField.setText(String.valueOf(firstNumber / secondNumber));
break;
}
newNumber = true;
}
private void setButtonStyle(JPanel panel) {
for (Component c : panel.getComponents()) {
if (c instanceof JButton) {
JButton button = (JButton) c;
button.setFont(new Font("Arial", Font.BOLD, 16));
button.setBackground(Color.LIGHT_GRAY);
button.setForeground(Color.BLACK);
button.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
}
}
}
}