0

我是 Java 新手,一直在研究一个转换器,它将采用 dec 数字并将其转换为二进制,反之亦然。我想实现异常处理,但很难完全理解这个概念。我希望程序捕获NumberFormatException并抛出NotInBinaryException我在单独的类中创建的 a。我能够抛出新异常,但不确定如何成功捕获异常以显示JOptionPane将在程序上显示的异常,提示用户以二进制格式输入数字,同时清除错误并将焦点设置在文本字段上错误存在的地方。到目前为止,这是我创建的代码。如果能帮助我重回正轨,我将不胜感激。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class ImprovedBaseGui extends JFrame implements ActionListener
{
 private JTextField txtBaseTen;
 private JTextField txtBaseTwo;
 private JButton btnBaseTen;
 private JButton btnBaseTwo;
 private JButton btnClear;
 public ImprovedBaseGui()
{
    this.setTitle("Base 10/2 Converter");    
    Container canvas = this.getContentPane();

    canvas.add(createCenterPanel(), BorderLayout.CENTER);
    canvas.add(createSouthPanel(), BorderLayout.SOUTH);


    this.setResizable(false);
    this.setSize(475, 150);
    this.setLocation(800, 500);
    this.setVisible(true);
    this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}

private JPanel createSouthPanel()
    {
        JPanel pnlSouth = new JPanel();

        btnBaseTen = new JButton("Base 10");
        btnBaseTen.addActionListener(this);
        btnBaseTen.setToolTipText( "Use to convert Base 2 to Base 10" );
        btnBaseTen.setBackground(Color.CYAN);
        pnlSouth.add(btnBaseTen);

        btnBaseTwo = new JButton("Base 2");
        btnBaseTwo.addActionListener(this);
        btnBaseTwo.setToolTipText( "Use to convert Base 10 to Base 2" );
        btnBaseTwo.setBackground(Color.YELLOW);
        pnlSouth.add(btnBaseTwo);

        btnClear = new JButton("Clear");
        btnClear.addActionListener(this);
        btnClear.setBackground(Color.RED);
        pnlSouth.add(btnClear);

        return pnlSouth;
    }

private JPanel createCenterPanel()
    {
        JPanel pnlCenter = new JPanel();
        pnlCenter.setLayout(new GridLayout(2,2));

        pnlCenter.add(wrapMeInAPanel(new JLabel ("Base 10")));
        txtBaseTen = new JTextField(16);
        txtBaseTen.setBackground(Color.YELLOW);
        pnlCenter.add(wrapMeInAPanel(txtBaseTen));

        pnlCenter.add(wrapMeInAPanel(new JLabel("Base 2")));
        txtBaseTwo = new JTextField(16);
        txtBaseTwo.setBackground(Color.CYAN);
        pnlCenter.add(wrapMeInAPanel(txtBaseTwo));

        return pnlCenter;
    }

    public static void main(String[] args)
    {
      new ImprovedBaseGui();
    }

    @Override
    public void actionPerformed(ActionEvent e)
        {

            if(e.getSource() == btnClear)
            {
                txtBaseTen.setText("");
                txtBaseTwo.setText("");
            }
            if(e.getSource() == btnBaseTwo)
            {
                try
                    {
txtBaseTwo.setText(Integer.toBinaryString(Integer.parseInt(txtBaseTen.getText())));
                    }
                catch(NumberFormatException err)
                    {
JOptionPane.showMessageDialog(this, txtBaseTen.getText()+"");
                    txtBaseTen.setText("");
                    txtBaseTen.grabFocus();
                    }

            }
            if(e.getSource() == btnBaseTen)
            {
                try
                    {
txtBaseTen.setText(Integer.toString(Integer.parseInt(txtBaseTwo.getText(), 2)));
            }
            catch(NumberFormatException err)
            {
            throw new NotInBinaryException();
                }

            }

        }

     private JPanel wrapMeInAPanel(Component c)
            {
                JPanel panel = new JPanel();
                panel.add(c);
                return panel;
            }
}
4

2 回答 2

1

NotInBinaryException如果您的方法无法处理NumberFormatException来自二进制解析的 ,您应该只抛出。但它可以处理它,你应该处理它。你不需要你的NotInBinaryException这里。

只需处理NumberFormatException与您在txtBaseTen案例中的处理方式类似的处理方式,尽管您可能希望为两者选择更用户友好的错误消息。

try
{
    txtBaseTen.setText(Integer.toString(Integer.parseInt(txtBaseTwo.getText(), 2)));
}
catch(NumberFormatException err)
{
    JOptionPane.showMessageDialog(this, txtBaseTwo.getText()+"");
    txtBaseTwo.setText("");
    txtBaseTwo.grabFocus();
}
于 2013-03-07T19:05:09.220 回答
0

如果您坚持使用NotInBinaryException,则让另一种方法为您执行转换,并让它抛出NotInBinaryException. 如果 your是 ,则该throws子句是不必要的。NotInBinaryExceptionRuntimeException

private String getDecimalText(String binaryText) throws NotInBinaryException
{
    try
    {
        return Integer.toString(Integer.parseInt(decimalText, 2));
    }
    catch (NumberFormatException err)
    {
        throw new NotInBinaryException();
    }
}

然后在你的actionPerformed方法中捕捉它。

try
{
    txtBaseTen.setText(getDecimalText(txtBaseTwo.getText()));
}
catch(NotInBinaryException err)
{
    JOptionPane.showMessageDialog(this, "Number entered was not in binary: " + txtBaseTwo.getText());
    txtBaseTwo.setText("");
    txtBaseTwo.grabFocus();
}
于 2013-03-07T19:39:57.580 回答