Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取导致NumberFormatException的文本字段?_Java_Swing_Exception Handling_Jtextfield_Numberformatexception - Fatal编程技术网

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

Java 如何获取导致NumberFormatException的文本字段?,java,swing,exception-handling,jtextfield,numberformatexception,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);

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

try {
   input1 = Integer.parseInt(textfield1.getText());
   input2 = Integer.parseInt(textfield2.getText());
} catch (NumberFormatException e) { 
    setBorderBorderFactory.createMatteBorder(2,2,2,2,Color.red);
}
现在如何获取catch子句中的文本字段,该字段会导致
NumberFormatException
,以更改边框的颜色?

您似乎忘记了“”


我建议您在每个语句的周围加上
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 */}

当您在
try
块中放入许多语句时,您通常以相同的方式处理发生的异常,独立于导致异常的对象。

将用于执行该特殊操作的代码及其错误处理放在单独的方法中:

private int parseAndHandleException(JTextField textfield) {
    int eingabe;
    try {
        eingabe = Integer.parseInt(textfield.getText());
    } catch (NumberFormatException e) {
       ...setBorderBorderFactory.createMatteBorder(2,2,2,2,Color.red));
    }
    return eingabe;
}
然后像这样打电话:

eingabe1 = parseAndHandleException(textfield1);
eingabe2 = parseAndHandleException(textfield2);

您可能希望使用JSlider,如下面的代码,来消除
NumberFormatException

        int minValue = 0;
        int maxValue = 100;
        int defultValue = 10;
        JSlider s = new JSlider(minValue, maxValue, defultValue);
        s.setLabelTable(s.createStandardLabels(10));
        s.setMinorTickSpacing(2);
        s.setMajorTickSpacing(10);
        s.setPaintTicks(true);
        s.setPaintLabels(true);

要获取
JSlider
的值,请调用始终返回整数的方法
getValue()

您缺少一些右括号。发布错误日志??感谢您的快速回答。例如,有人在textfield1中输入了非法的数字格式。我只想更改这个文本字段的边框,以便显示错误输入的位置made@Subburaj没有错误日志。OP想知道如果出现错误,要突出显示哪个文本框。使用
JSpinner
SpinnerNumberModel
可以解决整个问题。这并不能回答他的问题。OP中的代码显然只是一个伪代码,可能包含错误。我认为,问题不在于编译错误,而在于找出哪个文本字段的输入无效,这会导致NumberformatException。+1听起来正是OP想要的。很惊讶你能理解。我当然没有:)如果你有多个输入要解析,你可能想创建一个小助手方法,它可以在没有重复的情况下完成这项工作。@downvoter,请说明原因,以便我可以从错误中吸取教训,downvote毫无意义。我考虑过这个解决方案,但如果你有很多文本字段,我认为它并不理想。必须有另一个解决方案……这更接近OPs要求,因为重复更少(+1)。
        int minValue = 0;
        int maxValue = 100;
        int defultValue = 10;
        JSlider s = new JSlider(minValue, maxValue, defultValue);
        s.setLabelTable(s.createStandardLabels(10));
        s.setMinorTickSpacing(2);
        s.setMajorTickSpacing(10);
        s.setPaintTicks(true);
        s.setPaintLabels(true);