StringUtils.isNumeric阻止JOptionPane取消选项

时间:2016-02-06 15:37:45

标签: java joptionpane

这是我用来处理整数输入的代码:

public static int getIntInput(String message) {
    String numberStr = JOptionPane.showInputDialog(message);
    while (!StringUtils.isNumeric(numberStr) || Integer.parseInt(numberStr) == 0) {
        numberStr = JOptionPane.showInputDialog(message);
    }
    return Integer.parseInt(numberStr);
}

它工作正常,除非我想在JOption窗口按“取消”或关闭按钮。当我这样做时,JOptionPane窗口再次出现。

当按下取消或关闭按钮时,如何正确关闭JOptionPane窗口?

1 个答案:

答案 0 :(得分:0)

while (!StringUtils.isNumeric(numberStr) || Integer.parseInt(numberStr) == 0) {

        if (numberStr == null) {
           return 0; 
        }
        else {
            numberStr = JOptionPane.showInputDialog(message);
        }
}

这就是诀窍。