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.

213 lines
8.1 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);
2 months ago
displayField.setFont(new Font("Arial", Font.BOLD, 24));
2 months ago
displayField.setHorizontalAlignment(JTextField.RIGHT);
2 months ago
displayField.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
displayField.setBackground(Color.WHITE);
displayField.setForeground(Color.BLACK);
2 months ago
frame.getContentPane().add(displayField, BorderLayout.NORTH);
// 按钮面板
JPanel buttonPanel = new JPanel();
2 months ago
buttonPanel.setLayout(new GridLayout(5, 4, 5, 5)); // 增加间距
2 months ago
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());
}
}
});
2 months ago
button.setFont(new Font("Arial", Font.BOLD, 18));
button.setBackground(Color.LIGHT_GRAY);
button.setForeground(Color.BLACK);
button.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
2 months ago
buttonPanel.add(button);
}
// 添加0按钮
JButton buttonZero = new JButton("0");
buttonZero.addActionListener(e -> {
if (!displayField.getText().equals("0")) {
displayField.setText(displayField.getText() + "0");
}
});
2 months ago
buttonZero.setFont(new Font("Arial", Font.BOLD, 18));
buttonZero.setBackground(Color.LIGHT_GRAY);
buttonZero.setForeground(Color.BLACK);
buttonZero.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
2 months ago
buttonPanel.add(buttonZero);
// 小数点按钮
JButton buttonDecimal = new JButton(".");
buttonDecimal.addActionListener(e -> {
if (!displayField.getText().contains(".")) {
displayField.setText(displayField.getText() + ".");
}
});
2 months ago
buttonDecimal.setFont(new Font("Arial", Font.BOLD, 18));
buttonDecimal.setBackground(Color.LIGHT_GRAY);
buttonDecimal.setForeground(Color.BLACK);
buttonDecimal.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
2 months ago
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;
});
2 months ago
addOperatorButton(buttonPanel, "%", (a, b) -> {
if (b == 0) {
JOptionPane.showMessageDialog(frame, "除数不能为0", "错误", JOptionPane.ERROR_MESSAGE);
return Double.NaN;
}
return a % b;
});
2 months ago
// 清除按钮
JButton clearButton = new JButton("C");
clearButton.addActionListener(e -> {
displayField.setText("");
firstNumber = 0;
secondNumber = 0;
operator = "";
newNumber = true;
});
2 months ago
clearButton.setFont(new Font("Arial", Font.BOLD, 18));
clearButton.setBackground(Color.LIGHT_GRAY);
clearButton.setForeground(Color.BLACK);
clearButton.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
2 months ago
buttonPanel.add(clearButton);
// 等于号按钮
JButton equalsButton = new JButton("=");
equalsButton.addActionListener(e -> calculate());
2 months ago
equalsButton.setFont(new Font("Arial", Font.BOLD, 18));
equalsButton.setBackground(Color.GREEN);
equalsButton.setForeground(Color.WHITE);
equalsButton.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
2 months ago
buttonPanel.add(equalsButton);
// 设置按钮样式
setButtonStyle(buttonPanel);
}
private void addOperatorButton(JPanel panel, String op, BiFunction<Double, Double, Double> operation) {
JButton button = new JButton(op);
2 months ago
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (newNumber && !operator.isEmpty()) {
JOptionPane.showMessageDialog(frame, "连续输入运算符无效,请先输入数字或点击等于号!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
firstNumber = Double.parseDouble(displayField.getText());
operator = op;
// 重置 displayField 的内容
displayField.setText("");
newNumber = true;
}
2 months ago
});
2 months ago
button.setFont(new Font("Arial", Font.BOLD, 18));
button.setBackground(Color.LIGHT_GRAY);
button.setForeground(Color.BLACK);
button.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
2 months ago
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;
2 months ago
case "%":
if (secondNumber == 0) {
JOptionPane.showMessageDialog(frame, "除数不能为0", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
displayField.setText(String.valueOf(firstNumber % secondNumber));
break;
2 months ago
}
newNumber = true;
}
private void setButtonStyle(JPanel panel) {
for (Component c : panel.getComponents()) {
if (c instanceof JButton) {
JButton button = (JButton) c;
2 months ago
button.setFont(new Font("Arial", Font.BOLD, 18));
2 months ago
button.setBackground(Color.LIGHT_GRAY);
button.setForeground(Color.BLACK);
2 months ago
button.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
2 months ago
}
}
}
}
2 months ago