From d4c645852589f611d7e0d3775b0e971c8e6cbdf5 Mon Sep 17 00:00:00 2001 From: fjc <2499842112@qq.com> Date: Thu, 10 Oct 2024 22:52:40 +0800 Subject: [PATCH] fjc --- Operate.java | 71 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 60 insertions(+), 11 deletions(-) diff --git a/Operate.java b/Operate.java index 668a3d0..356fee1 100644 --- a/Operate.java +++ b/Operate.java @@ -37,13 +37,16 @@ public class Operate { // 显示区域 displayField = new JTextField(); displayField.setEditable(false); - displayField.setFont(new Font("Arial", Font.BOLD, 20)); + displayField.setFont(new Font("Arial", Font.BOLD, 24)); 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); // 按钮面板 JPanel buttonPanel = new JPanel(); - buttonPanel.setLayout(new GridLayout(5, 4)); + buttonPanel.setLayout(new GridLayout(5, 4, 5, 5)); // 增加间距 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); - if (i % 3 == 0) buttonPanel.add(new JButton()); // 空位 } // 添加0按钮 @@ -71,6 +77,10 @@ public class Operate { 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); // 小数点按钮 @@ -80,6 +90,10 @@ public class Operate { 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); // 运算符按钮 @@ -93,6 +107,13 @@ public class Operate { } 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"); @@ -103,11 +124,19 @@ public class Operate { operator = ""; 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); // 等于号按钮 JButton equalsButton = new JButton("="); 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); // 设置按钮样式 @@ -116,13 +145,24 @@ public class Operate { private void addOperatorButton(JPanel panel, String op, BiFunction operation) { JButton button = new JButton(op); - button.addActionListener(e -> { - firstNumber = Double.parseDouble(displayField.getText()); - operator = op; - // 重置 displayField 的内容 - displayField.setText(""); - newNumber = true; + 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; + } }); + 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); } @@ -145,6 +185,13 @@ public class Operate { } 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; } @@ -153,11 +200,13 @@ public class Operate { for (Component c : panel.getComponents()) { if (c instanceof JButton) { 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.setForeground(Color.BLACK); - button.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY)); + button.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2)); } } } } + +