0

私はJavaが初めてで、10進数を取り、それをバイナリに、またはその逆に変換するコンバーターに取り組んでいます。例外処理を実装したいのですが、概念を完全に理解するのに苦労しています。別のクラスで作成した aをキャッチしNumberFormatExceptionてスローするプログラムが必要です。新しい例外をスローすることはできますが、エラーをクリアしてテキスト フィールドにフォーカスを設定しながら、ユーザーにバイナリ形式で数値を入力するように求めるプログラムの上に表示されるNotInBinaryException例外を正常にキャッチして表示する方法が不明です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 に答える