简体   繁体   English

JOptionPane输入对话框如何确定值是2还是JOptionPane.CANCEL_OPTION

[英]JOptionPane Input Dialog how to decide if the value is 2 or JOptionPane.CANCEL_OPTION

i use the following Code to get an InputDialog: 我使用以下代码来获取InputDialog:

String c = JOptionPane.showInputDialog("Select number",JOptionPane.OK_OPTION);

I also want that the User only uses IntegerValues between 0 and 100. I handle this by the following code: 我还希望User只使用介于0和100之间的IntegerValues。我通过以下代码处理:

while(notAllowed){
    try{
        int t =Integer.parseInt(c);
        if(t==JOptionPane.CANCEL_OPTION)
        {
            notAllowed=false;
            cancel=true;
        }
        if(t<=100 && t>0 &&notAllowed)
            notAllowed=false;
    }
    catch( Exception err)
    {}
    if(notAllowed)
        c = JOptionPane.showInputDialog("Only Numbers between 1 and 100 are allowed");
    }

Now if the uses types the number 2 its like clicking the Cancel Button because the Value of JOptionPane.CANCEL_OPTION is also 2. So how can i find out if Cancel is clicked or the input Value is 2. 现在,如果使用类型数字2,就像单击取消按钮一样,因为JOptionPane.CANCEL_OPTION的值也是2.那么如何找出是否单击取消或输入值是否为2。

JOptionPane returns null if the user clicks on cancel. 如果用户单击取消,JOptionPane将返回null。 Otherwise the value will be returned. 否则返回值。 I figured that out using this small example: 我想出了这个小例子:

public class JOptionPaneTest {

    public static void main(String[] args) {
        Object obj = JOptionPane.showInputDialog(null, "test", "test-text");
        System.out.println(obj);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM