fjc 1 month ago
parent d132df781b
commit d4c6458525

@ -37,13 +37,16 @@ public class Operate {
// 显示区域 // 显示区域
displayField = new JTextField(); displayField = new JTextField();
displayField.setEditable(false); displayField.setEditable(false);
displayField.setFont(new Font("Arial", Font.BOLD, 20)); displayField.setFont(new Font("Arial", Font.BOLD, 24));
displayField.setHorizontalAlignment(JTextField.RIGHT); displayField.setHorizontalAlignment(JTextField.RIGHT);
displayField.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
displayField.setBackground(Color.WHITE);
displayField.setForeground(Color.BLACK);
frame.getContentPane().add(displayField, BorderLayout.NORTH); frame.getContentPane().add(displayField, BorderLayout.NORTH);
// 按钮面板 // 按钮面板
JPanel buttonPanel = new JPanel(); JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(5, 4)); buttonPanel.setLayout(new GridLayout(5, 4, 5, 5)); // 增加间距
frame.getContentPane().add(buttonPanel, BorderLayout.CENTER); frame.getContentPane().add(buttonPanel, BorderLayout.CENTER);
// 数字按钮 // 数字按钮
@ -60,8 +63,11 @@ public class Operate {
} }
} }
}); });
button.setFont(new Font("Arial", Font.BOLD, 18));
button.setBackground(Color.LIGHT_GRAY);
button.setForeground(Color.BLACK);
button.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
buttonPanel.add(button); buttonPanel.add(button);
if (i % 3 == 0) buttonPanel.add(new JButton()); // 空位
} }
// 添加0按钮 // 添加0按钮
@ -71,6 +77,10 @@ public class Operate {
displayField.setText(displayField.getText() + "0"); displayField.setText(displayField.getText() + "0");
} }
}); });
buttonZero.setFont(new Font("Arial", Font.BOLD, 18));
buttonZero.setBackground(Color.LIGHT_GRAY);
buttonZero.setForeground(Color.BLACK);
buttonZero.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
buttonPanel.add(buttonZero); buttonPanel.add(buttonZero);
// 小数点按钮 // 小数点按钮
@ -80,6 +90,10 @@ public class Operate {
displayField.setText(displayField.getText() + "."); displayField.setText(displayField.getText() + ".");
} }
}); });
buttonDecimal.setFont(new Font("Arial", Font.BOLD, 18));
buttonDecimal.setBackground(Color.LIGHT_GRAY);
buttonDecimal.setForeground(Color.BLACK);
buttonDecimal.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
buttonPanel.add(buttonDecimal); buttonPanel.add(buttonDecimal);
// 运算符按钮 // 运算符按钮
@ -93,6 +107,13 @@ public class Operate {
} }
return a / b; return 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"); JButton clearButton = new JButton("C");
@ -103,11 +124,19 @@ public class Operate {
operator = ""; operator = "";
newNumber = true; newNumber = true;
}); });
clearButton.setFont(new Font("Arial", Font.BOLD, 18));
clearButton.setBackground(Color.LIGHT_GRAY);
clearButton.setForeground(Color.BLACK);
clearButton.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
buttonPanel.add(clearButton); buttonPanel.add(clearButton);
// 等于号按钮 // 等于号按钮
JButton equalsButton = new JButton("="); JButton equalsButton = new JButton("=");
equalsButton.addActionListener(e -> calculate()); equalsButton.addActionListener(e -> calculate());
equalsButton.setFont(new Font("Arial", Font.BOLD, 18));
equalsButton.setBackground(Color.GREEN);
equalsButton.setForeground(Color.WHITE);
equalsButton.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
buttonPanel.add(equalsButton); buttonPanel.add(equalsButton);
// 设置按钮样式 // 设置按钮样式
@ -116,13 +145,24 @@ public class Operate {
private void addOperatorButton(JPanel panel, String op, BiFunction<Double, Double, Double> operation) { private void addOperatorButton(JPanel panel, String op, BiFunction<Double, Double, Double> operation) {
JButton button = new JButton(op); JButton button = new JButton(op);
button.addActionListener(e -> { button.addActionListener(new ActionListener() {
firstNumber = Double.parseDouble(displayField.getText()); @Override
operator = op; public void actionPerformed(ActionEvent e) {
// 重置 displayField 的内容 if (newNumber && !operator.isEmpty()) {
displayField.setText(""); JOptionPane.showMessageDialog(frame, "连续输入运算符无效,请先输入数字或点击等于号!", "错误", JOptionPane.ERROR_MESSAGE);
newNumber = true; return;
}
firstNumber = Double.parseDouble(displayField.getText());
operator = op;
// 重置 displayField 的内容
displayField.setText("");
newNumber = true;
}
}); });
button.setFont(new Font("Arial", Font.BOLD, 18));
button.setBackground(Color.LIGHT_GRAY);
button.setForeground(Color.BLACK);
button.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
panel.add(button); panel.add(button);
} }
@ -145,6 +185,13 @@ public class Operate {
} }
displayField.setText(String.valueOf(firstNumber / secondNumber)); displayField.setText(String.valueOf(firstNumber / secondNumber));
break; break;
case "%":
if (secondNumber == 0) {
JOptionPane.showMessageDialog(frame, "除数不能为0", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
displayField.setText(String.valueOf(firstNumber % secondNumber));
break;
} }
newNumber = true; newNumber = true;
} }
@ -153,11 +200,13 @@ public class Operate {
for (Component c : panel.getComponents()) { for (Component c : panel.getComponents()) {
if (c instanceof JButton) { if (c instanceof JButton) {
JButton button = (JButton) c; JButton button = (JButton) c;
button.setFont(new Font("Arial", Font.BOLD, 16)); button.setFont(new Font("Arial", Font.BOLD, 18));
button.setBackground(Color.LIGHT_GRAY); button.setBackground(Color.LIGHT_GRAY);
button.setForeground(Color.BLACK); button.setForeground(Color.BLACK);
button.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY)); button.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
} }
} }
} }
} }

Loading…
Cancel
Save