为我的程序设置 Java 异常处理

问题描述 投票:0回答:1

我试图处理我正在开发的素数检测器代码中的一些异常。这是我当前的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.math.BigInteger;

public class Frame1 {
  private JFrame frame;
  private JPanel panel;
  private JTextField textField;
  private JLabel label;
  private JButton button;
  private JLabel DisplayLabel;
  private JPanel DisplayPanel;

public Frame1() {
    initialize();
}

private void initialize() {
    frame = new JFrame();
    frame.setTitle("Prime Detector");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setSize(420, 420);
    frame.setLayout(new BorderLayout(10, 10));
    frame.setLocationRelativeTo(null);

    panel = new JPanel();
    panel.setLayout(new FlowLayout());
    panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    label = new JLabel("Enter a number");
    label.setFont(new Font("Times New Roman", Font.BOLD, 20));
    panel.add(label);


    DisplayPanel = new JPanel();
    DisplayPanel.setLayout(new FlowLayout());
    DisplayPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    DisplayPanel.setVisible(false);

    DisplayLabel = new JLabel();
    DisplayLabel.setFont(new Font("Times New Roman", Font.BOLD, 30));


    textField = createJTextField();
    panel.add(textField);

    button = new JButton("Enter");
    button.setFocusable(false);
    button.setBackground(Color.lightGray);
    button.setBorder(BorderFactory.createEtchedBorder());
    button.setForeground(Color.cyan);
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            TheCode code = new TheCode();
            BigInteger number = new BigInteger(textField.getText());
            try{
            code.primeDetector(number);


            if (TheCode.IsItPrime) {
                DisplayLabel.setText(number + " is prime.");
                DisplayLabel.setVisible(true);
                DisplayPanel.setVisible(true);
                DisplayPanel.add(DisplayLabel);
            } else {
                DisplayLabel.setText(number + " isn't prime.");
                DisplayLabel.setVisible(true);
                DisplayPanel.setVisible(true);
                DisplayPanel.add(DisplayLabel);
                }
            }catch(Exception exception){
             DisplayLabel.setText("Enter a Valid Number");
            }
        }
    });

    panel.add(button);

    frame.add(panel, BorderLayout.NORTH);
    frame.add(DisplayPanel, BorderLayout.CENTER);
}


public void show() {
    frame.setVisible(true);
}

private JTextField createJTextField() {
    JTextField textField = new JTextField(10);

    return textField;

   }
}

但是,当我运行此代码并尝试输入无效内容时,它仍然在控制台中显示红色异常消息,这正是我试图阻止的。有人可以帮助我吗? (顺便说一句, primeDetector 方法和 TheCode 类定义良好并且正常工作)

java exception
1个回答
0
投票
    BigInteger number = new BigInteger(textField.getText());
    try{
        code.primeDetector(number);

这里的问题是 BigInteger 构造不在 try 块内,因此它不会被相应的 catch 块处理。所以应该搬进去。

    try{
        BigInteger number = new BigInteger(textField.getText());
        code.primeDetector(number);
© www.soinside.com 2019 - 2024. All rights reserved.