如何获取导致NumberFormatException的文本字段?

use*_*610 3 java swing exception-handling jtextfield numberformatexception

我想更改文本字段的边框,导致NumberFormatException:

try {
   input1 = Integer.parseInt(textfield1.getText());
   input2 = Integer.parseInt(textfield2.getText());
} catch (NumberFormatException e) { 
    setBorderBorderFactory.createMatteBorder(2,2,2,2,Color.red);
}
Run Code Online (Sandbox Code Playgroud)

我现在如何获取catch子句中的文本字段,该字段导致NumberFormatException更改边框的颜色?

Mar*_*oun 5

我建议你用以下内容包围每个声明try-catch:

try{
eingabe1 = Integer.parseInt(textfield1.getText());
} catch (NumberFormatException e) { /* Exception from textfield1 */ }
try {
eingabe2 = Integer.parseInt(textfield2.getText());
} catch (NumberFormatException e) { /* Exception from textfield2 */}
Run Code Online (Sandbox Code Playgroud)

当您在try块中放入许多语句时,通常会以相同的方式处理发生的异常,而不是导致异常对象.