JOptionPane 取消按钮和获取输入

JOptionPane cancel button and getting input

我试图让用户输入姓名,如果它留空,它会再次询问,如果他们填写它,它会设置 JLabel 或点击取消退出。
我的最后一个 if 语句是错误的,它不喜欢 nameEnt.

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.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.CANCEL_OPTION是一个静态整型字段,不能比较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

根据 JOptionPane API,如果用户取消对话,null 将被 return 编辑。

因此正确的解决方案是不要使用等于,而是检查 return 值是否为 null 并首先执行此操作,before 检查其长度。

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 = "";
        }
    }
}