Java 从JOptionPane创建对话框并使用OK\u CANCEL\u选项

Java 从JOptionPane创建对话框并使用OK\u CANCEL\u选项,java,swing,joptionpane,Java,Swing,Joptionpane,我有一个自定义对话框,从用户那里收集两个字符串。在创建对话框时,我对选项类型使用OK\u CANCEL\u选项。除用户单击“取消”或关闭对话框外,所有提示均有效,其效果与单击“确定”按钮相同 如何处理取消和关闭事件 下面是我要说的代码: JTextField topicTitle = new JTextField(); JTextField topicDesc = new JTextField(); Object[] message = {"Title: ", topicTitle, "Desc

我有一个自定义对话框,从用户那里收集两个字符串。在创建对话框时,我对选项类型使用OK\u CANCEL\u选项。除用户单击“取消”或关闭对话框外,所有提示均有效,其效果与单击“确定”按钮相同

如何处理取消和关闭事件

下面是我要说的代码:

JTextField topicTitle = new JTextField();
JTextField topicDesc = new JTextField();
Object[] message = {"Title: ", topicTitle, "Description: ", topicDesc};
JOptionPane pane = new JOptionPane(message,  JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
JDialog getTopicDialog =  pane.createDialog(null, "New Topic");
getTopicDialog.setVisible(true);
//按“确定”时在此处执行操作,但按“取消”时只需处理即可

/注意:请不要向我推荐JOptionPane的方式。ShowOptionDialog******;**对于此问题,因为我知道这种方式,但我需要上述方式来执行和设置“确定”和“取消”按钮的操作。*/

根据您可以使用的窗格.getValue了解单击了哪个按钮。 从文件:

直接使用:要直接创建和使用JOptionPane,标准模式大致如下:

     JOptionPane pane = new JOptionPane(arguments);
     pane.set.Xxxx(...); // Configure
     JDialog dialog = pane.createDialog(parentComponent, title);
     dialog.show();
     Object selectedValue = pane.getValue();
     if(selectedValue == null)
       return CLOSED_OPTION;
     //If there is not an array of option buttons:
     if(options == null) {
       if(selectedValue instanceof Integer)
          return ((Integer)selectedValue).intValue();
       return CLOSED_OPTION;
     }
     //If there is an array of option buttons:
     for(int counter = 0, maxCounter = options.length;
        counter < maxCounter; counter++) {
        if(options[counter].equals(selectedValue))
        return counter;
     }
     return CLOSED_OPTION;
希望对您有所帮助,

根据您可以使用pane.getValue了解单击了哪个按钮。 从文件:

直接使用:要直接创建和使用JOptionPane,标准模式大致如下:

     JOptionPane pane = new JOptionPane(arguments);
     pane.set.Xxxx(...); // Configure
     JDialog dialog = pane.createDialog(parentComponent, title);
     dialog.show();
     Object selectedValue = pane.getValue();
     if(selectedValue == null)
       return CLOSED_OPTION;
     //If there is not an array of option buttons:
     if(options == null) {
       if(selectedValue instanceof Integer)
          return ((Integer)selectedValue).intValue();
       return CLOSED_OPTION;
     }
     //If there is an array of option buttons:
     for(int counter = 0, maxCounter = options.length;
        counter < maxCounter; counter++) {
        if(options[counter].equals(selectedValue))
        return counter;
     }
     return CLOSED_OPTION;
希望能有所帮助,

这对我很有用:

...
JOptionPane pane = new JOptionPane(message,  JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
JDialog getTopicDialog =  pane.createDialog(null, "New Topic");
getTopicDialog.setVisible(true);
if(null == pane.getValue()) {
    System.out.println("User closed dialog");
}
else {
    switch(((Integer)pane.getValue()).intValue()) {
    case JOptionPane.OK_OPTION:
        System.out.println("User selected OK");
        break;
    case JOptionPane.CANCEL_OPTION:
        System.out.println("User selected Cancel");
        break;
    default:
        System.out.println("User selected " + pane.getValue());
    }
}
这对我很有用:

...
JOptionPane pane = new JOptionPane(message,  JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
JDialog getTopicDialog =  pane.createDialog(null, "New Topic");
getTopicDialog.setVisible(true);
if(null == pane.getValue()) {
    System.out.println("User closed dialog");
}
else {
    switch(((Integer)pane.getValue()).intValue()) {
    case JOptionPane.OK_OPTION:
        System.out.println("User selected OK");
        break;
    case JOptionPane.CANCEL_OPTION:
        System.out.println("User selected Cancel");
        break;
    default:
        System.out.println("User selected " + pane.getValue());
    }
}

请这个问题是关于,阅读Oracle教程,在这个论坛上搜索。请这个问题是关于,阅读Oracle教程,在这个论坛上搜索