Java Swing GUI Try/Catch Block

后端 未结 3 1108
刺人心
刺人心 2021-01-29 05:04

I\'m just learning Java exception handling and Java in general. I\'ve made a Swing GUI where the user will enter integers into two fields and then click a radio button with an

3条回答
  •  耶瑟儿~
    2021-01-29 05:50

    You'll get exception if you won't check your every event method, so you can do something like that to solve it,

    private void aRadioButtonActionPerformed(ActionEvent evt) {
        String a = aTextField.getText();
        String b = bTextField.getText();
    
    
    
        // you may get empty string here so check if the texbox is empty ?
     int i=0,j=0;
    
            try{
    
        if(a.length()>0 || b.length()>0){
             i = Integer.parseInt(a);
             j = Integer.parseInt(b);
        }else{
            if(a.length()<1 && b.length()>0){
    
                i = Integer.parseInt(a);
            }else{
                if(b.length()<1 && a.length()>0)
                j = Integer.parseInt(b);
            }
        }
    
    
        int k = i + j;
            }catch(Exception e){
              System.out.println("Exception:"+e);
              }
        cTextField.setText(Integer.toString(k));
    }
    

    do the same thing for you -,*,/ radio button listeners too

提交回复
热议问题