1

使用したいのですが、プログラムにボタンNumberFormatExceptionがあるため、コードにエラーがあります。TextFieldテキストフィールドに数値を入力すれば問題ありません。文字を入力するとエラーメッセージを出したいのですが、使いません。私を助けてください?
私のコード

private  JTextField t1=new JTextField(10);
private  JButton o88 = new JButton("send");

try{
   o88.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e) {
           int a = 0;
           a = Integer.parseInt(t1.getText()); 
       }
   });
}
catch (NumberFormatException e){
    System.out.println( e.getMessage());
}
4

1 に答える 1

2

try/catch の場所が間違っています。actionPerformed メソッド内に配置する必要があります。

 public void actionPerformed(ActionEvent e ) {
   try {
    int a = Integer.parseInt( t1.getText() );
   }
   catch(NumberFormatException e ) {
     System.out.println( e.getMessage() );
   }
 }
于 2013-12-21T20:57:52.203 に答える