JOptionPane取消按钮并获取输入

时间:2017-03-18 19:23:19

标签: java joptionpane

我试图让用户输入名称,如果它留空,它会再次询问,如果他们填写它设置[(2, 1), (1, 0), (1, 0), (1,0), (2,1), (2, 1)] 或点击取消就可以退出。
我的最后一句if语句错误,它不喜欢[(2, 1), (1, 0), (2, 1)]

# specific comment line
if [ -d "/home/somewhere" ];then

2 个答案:

答案 0 :(得分:2)

JOptionPane.CANCEL_OPTION是一个静态int字段,您无法将Stringint==进行比较。

良好做法

在您的情况下,您想要一次性使用确定和取消按钮JOptionPane.showConfirmDialogJOptionPane.showInputDialog(),这是不可能的,我建议使用此代码:

JTextField nameF = new JTextField(20);//create TextField

JPanel myPanel = new JPanel();//cerate JPanel
myPanel.add(new JLabel("Name"));
myPanel.add(nameF);//add your JTextField to your panel

int result;
do {
    result = JOptionPane.showConfirmDialog(null, myPanel,
            "Title of Panel", JOptionPane.OK_CANCEL_OPTION);//add your panel to JOptionPane
    if (result == JOptionPane.OK_OPTION) {//if the user press OK then
        if (nameF.getText().isEmpty()) {//check if the input is empty
            //if this condition is true JOption stays until name is entered or canceled 
        } else if (!nameF.getText().matches("[a-zA-Z]+")) {//check if the input match with your regex
            //name match exactly
            //name.setText(nameF.getText());
        }
    }
} while (result != JOptionPane.CANCEL_OPTION);//If the user hit cancel then exit

答案 1 :(得分:1)

根据JOptionPane API,如果用户取消对话框,则会返回null

因此,正确的解决方案是不使用equals,而是检查null的返回值并首先执行此操作,检查其长度之前。

public Player() {
    //setBackground(Color.green);
    setSize(600, 400);
    name = new JLabel();//Input hint
    JOptionPane nameOption = new JOptionPane();
    String nameEnt = nameOption.showInputDialog("First Name: ");
    if (nameEnt == null) {
        // user canceled. get out of here. 
        System.exit(0);

        // or return;  
        // or throw some exception
    }
    if (!nameEnt.matches("[a-zA-Z]+")) {
        name.setText(nameEnt);
    }
    if (nameEnt.length() == 0) {
        //if this condition is true JOption stays until name is entered or canceled 
    }
    // if (nameEnt == nameOption.CANCEL_OPTION) {
       //  System.exit(0);
    // }
}

但是你为什么用这种方式创建一个JOptionPane呢?最好使用静态创建方法。

// don't use null as the first parameter if the GUI is already showing
String nameEnt = JOptionPane.showInputDialog(null, "First Name: ");
if (nameEnt == null) {
    // user canceled. get out of here. 
    System.exit(0);
}

或者类似这样的事情,如果你试图循环获取输入:

public Player() {
    setSize(600, 400);  // This is not good to do. Ask for details and I'll tell.

    name = new JLabel();// Don't forget to add this to the GUI!

    String nameEnt = "";
    while (nameEnt.trim().isEmpty()) {
        // if the GUI is already showing, pass a component from it as the first param here, not null
        nameEnt = JOptionPane.showInputDialog(null, "First Name: ");
        if (nameEnt == null) {
            // user canceled. get out of here. 
            System.exit(0);

            // or return;  
            // or throw some exception
        } else if (!nameEnt.matches("[a-zA-Z]+")) {
            name.setText(nameEnt);
        } else {
            // set it to "" so that we keep looping
            nameEnt = "";
        }
    }
}