简体   繁体   English

JOptionPane取消按钮并获取输入

[英]JOptionPane cancel button and getting input

I'm trying to get the user to input there name if it is left blank it will ask again, if they fill it out it sets a JLabel or hit cancel to get out. 我试图让用户输入名称,如果它留空,它将再次询问,如果他们填写它设置JLabel或点击取消退出。
My last if statement is wrong it does not like nameEnt . 我的最后一个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);
    }
}

The JOptionPane.CANCEL_OPTION is a static int field, and you can't compare String with int with == . JOptionPane.CANCEL_OPTION是一个静态int字段,您不能将Stringint==进行比较。


Good practice 好的做法

In your case you want to use ok and cancel button JOptionPane.showConfirmDialog and JOptionPane.showInputDialog() in one shot and this is not possible, i suggest to use this instead : 在你的情况下,你想一次性使用确定和取消按钮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

As per the JOptionPane API , if the user cancels the dialog, null is returned. 根据JOptionPane API ,如果用户取消该对话框,则返回null

And so the correct solution is to to not to use equals, but rather to check the return value for null and to do this first, before checking its length. 因此,正确的解决方案是不使用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);
    // }
}

But why are you creating a JOptionPane this way? 但是你为什么要用这种方式创建一个JOptionPane呢? Better to use the static method of creation. 最好使用静态创建方法。

// 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);
}

Or maybe something like this, if you're trying to loop to get input: 或者类似这样的事情,如果你想循环获取输入:

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

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

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