|
|
|
@ -11,14 +11,20 @@ public class Calculator extends JFrame implements ActionListener {
|
|
|
|
|
|
|
|
|
|
public Calculator() {
|
|
|
|
|
setTitle("计算器");
|
|
|
|
|
|
|
|
|
|
// 设置文本字段样式
|
|
|
|
|
textField = new JTextField("0");
|
|
|
|
|
textField.setEditable(false);
|
|
|
|
|
textField.setHorizontalAlignment(JTextField.RIGHT); // 右对齐显示
|
|
|
|
|
textField.setFont(new Font("Arial", Font.BOLD, 24)); // 增加文本框字体大小
|
|
|
|
|
textField.setHorizontalAlignment(JTextField.RIGHT);
|
|
|
|
|
textField.setFont(new Font("Arial", Font.BOLD, 28));
|
|
|
|
|
textField.setBackground(new Color(240, 240, 240)); // 设置背景颜色
|
|
|
|
|
textField.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); // 设置边框
|
|
|
|
|
|
|
|
|
|
add(textField, BorderLayout.NORTH);
|
|
|
|
|
|
|
|
|
|
JPanel panel = new JPanel();
|
|
|
|
|
panel.setLayout(new GridLayout(5, 4, 10, 10)); // 修改为5行以适应新布局
|
|
|
|
|
panel.setLayout(new GridLayout(5, 4, 10, 10));
|
|
|
|
|
panel.setBackground(new Color(220, 220, 220)); // 设置面板背景颜色
|
|
|
|
|
|
|
|
|
|
String[] buttons = {
|
|
|
|
|
"7", "8", "9", "÷",
|
|
|
|
@ -29,19 +35,24 @@ public class Calculator extends JFrame implements ActionListener {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (String text : buttons) {
|
|
|
|
|
if (!text.isEmpty()) { // 确保不创建空按钮
|
|
|
|
|
if (!text.isEmpty()) {
|
|
|
|
|
JButton button = new JButton(text);
|
|
|
|
|
button.setFont(new Font("Arial", Font.BOLD, 18)); // 增加按钮字体大小
|
|
|
|
|
button.setFont(new Font("Arial", Font.BOLD, 22));
|
|
|
|
|
button.setFocusPainted(false); // 去掉按钮焦点时的边框
|
|
|
|
|
button.setBackground(new Color(180, 180, 180)); // 设置按钮背景颜色
|
|
|
|
|
button.setForeground(Color.BLACK); // 设置按钮文字颜色
|
|
|
|
|
button.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); // 设置按钮边框
|
|
|
|
|
button.addActionListener(this);
|
|
|
|
|
panel.add(button);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
add(panel, BorderLayout.CENTER);
|
|
|
|
|
|
|
|
|
|
// 设置窗口属性
|
|
|
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
|
setSize(400, 600); // 修改窗口大小为宽400像素,高600像素以适应新布局
|
|
|
|
|
setLocationRelativeTo(null); // 窗口居中显示
|
|
|
|
|
pack();
|
|
|
|
|
setSize(420, 640); // 稍微增大窗口以适应新的布局和样式
|
|
|
|
|
setLocationRelativeTo(null);
|
|
|
|
|
setVisible(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -99,7 +110,7 @@ public class Calculator extends JFrame implements ActionListener {
|
|
|
|
|
if (n != 0) {
|
|
|
|
|
result /= n;
|
|
|
|
|
} else {
|
|
|
|
|
JOptionPane.showMessageDialog(this, "除数不能为0");
|
|
|
|
|
JOptionPane.showMessageDialog(this, "除数不能为0", "错误", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
@ -117,13 +128,13 @@ public class Calculator extends JFrame implements ActionListener {
|
|
|
|
|
try {
|
|
|
|
|
double num = Double.parseDouble(textField.getText());
|
|
|
|
|
result = Math.sqrt(num);
|
|
|
|
|
if (result == Math.floor(result)) { // 检查结果是否已经是整数
|
|
|
|
|
textField.setText(String.valueOf((int) result)); // 转换为整数并显示
|
|
|
|
|
if (result == Math.floor(result)) {
|
|
|
|
|
textField.setText(String.valueOf((int) result));
|
|
|
|
|
} else {
|
|
|
|
|
textField.setText(String.valueOf(result)); // 否则显示浮点数
|
|
|
|
|
textField.setText(String.valueOf(result));
|
|
|
|
|
}
|
|
|
|
|
} catch (NumberFormatException ex) {
|
|
|
|
|
JOptionPane.showMessageDialog(this, "请输入一个有效的数字");
|
|
|
|
|
JOptionPane.showMessageDialog(this, "请输入一个有效的数字", "错误", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
}
|
|
|
|
|
calculating = true;
|
|
|
|
|
}
|
|
|
|
|