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, 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, 5, 5)); // 增加间距 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()); } } }); 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); } // 添加0按钮 JButton buttonZero = new JButton("0"); buttonZero.addActionListener(e -> { if (!displayField.getText().equals("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); // 小数点按钮 JButton buttonDecimal = new JButton("."); buttonDecimal.addActionListener(e -> { if (!displayField.getText().contains(".")) { 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); // 运算符按钮 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; }); 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; }); 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); // 设置按钮样式 setButtonStyle(buttonPanel); } private void addOperatorButton(JPanel panel, String op, BiFunction operation) { JButton button = new JButton(op); 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); } 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; 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, 18)); button.setBackground(Color.LIGHT_GRAY); button.setForeground(Color.BLACK); button.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2)); } } } }