Java JOptionPane,在验证JTextField时关闭和取消不工作

Java JOptionPane,在验证JTextField时关闭和取消不工作,java,swing,dialog,joptionpane,Java,Swing,Dialog,Joptionpane,我有一个带有JTextField的JOptionPane: private void imageFilePanel(){ JRadioButton go = new JRadioButton("Grid Object"); JRadioButton ti = new JRadioButton("Tile Image"); JTextField fn = new JTextField(15); JPanel panel = new JPanel(); pan

我有一个带有JTextField的JOptionPane:

private void imageFilePanel(){
    JRadioButton go = new JRadioButton("Grid Object");
    JRadioButton ti = new JRadioButton("Tile Image");
    JTextField fn = new JTextField(15);
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
    panel.add(fn);
    panel.add(go);
    panel.add(ti);
    JOptionPane.showOptionDialog(null, panel, "Name Image", JOptionPane.CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);

    if(fn.getText().equals("") || !(go.isSelected()) && !(ti.isSelected())){
        JOptionPane.showMessageDialog(frame, "Complete required fields.", "Error Message", JOptionPane.ERROR_MESSAGE);
        imageFilePanel();
    }
}
我不希望用户继续下一步,除非他们填写JTextField“fn”并选择一个JRadioButtons(“go”或“ti”)

但是,当用户单击窗口关闭或取消按钮时,我的代码现在将通过if语句。这不是我想要的行为;我希望仅当用户尝试按下“OK”按钮而不填写“fn”、不选择“go”或“ti”或两者都不填写时,才会对if语句进行求值


欢迎任何意见

实际上,如果您锁定在
showOptionDialog
上,您将看到它会返回一个按住对话框中按下的按钮的
int
,因此您可以使用该
int
变量来确定是否执行语句

例如:

int result = JOptionPane.showOptionDialog(null, panel, "Name Image", JOptionPane.CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);

if(result == JOptionPane.YES_OPTION) {
     //your code.
}
你可以用 “ItemListener”

就像“ActionListener”。if语句应位于“ItemListener”中。这将检查项目并生成所需的弹出窗口“JOptionPane” 退房还有一件事;如果您说“(“go”或“ti”),您的if语句必须如下所示

if(fn.getText().equals("") || !(go.isSelected()) || !(ti.isSelected())){