1

I want to use NumberFormatException but my code have error, because I have TextField and a button in my program. If you enter the number in textfield, there is not any problem. If you enter a letter, I want to get error message but I don't use. please help me?
my code

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

你在错误的地方尝试/捕捉。您需要将其放在 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 回答