【问题标题】:JDialog dissappears when I switch between two windows当我在两个窗口之间切换时,JDialog 消失
【发布时间】:2015-06-03 18:12:56
【问题描述】:

我正在尝试制作一个简单的 JDialog,它要求用户以 3 个文本字段的形式输入,它显示正确并且它的 PropertyListener 工作得非常好,我还没有为 JDialog 分配父级构造函数,所以我猜测默认情况下父级设置为我的小程序中所有组件的祖先。但是,当我从小程序更改为 Firefox 窗口时,当我点击我的小程序时,JDialog 消失了。我是否需要为 JDialog 设置某个属性以确保即使在我切换窗口时它仍然存在。最重要的是我认为对话框仍然存在,但不可见,因为当第一个对话框消失后出现另一个对话框时,两个对话框同时出现(第一个对话框重新出现)。我的 JDialog 代码如下:

private void addQuestion() {
        questionTextField = new TextField(50);

        Object[] componentsArray = {"Question:", questionTextField, "MQLYes:", mqlYesTextField, "MQLNo:", mqlNoTextField};
        Object[] options = {"Enter", "Cancel"};
        addQuestionDialog = new JDialog(new JFrame(),"Add question");
        addQuestionPane = new JOptionPane(componentsArray, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null, options, options[0]);

        int x = getX() + getWidth()/2, y = getY() + getHeight()/2;

        addQuestionDialog.setContentPane(addQuestionPane);
        addQuestionDialog.setResizable(false);
        addQuestionDialog.setSize(300,210);
        addQuestionDialog.setVisible(true);
        addQuestionDialog.setLocation(x, y);
        addQuestionDialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);

        addQuestionPane.addPropertyChangeListener(this);
    }

public void propertyChange(PropertyChangeEvent e) {
    String prop = e.getPropertyName();

    if (addQuestionDialog.isVisible() && (e.getSource() == addQuestionPane) && (JOptionPane.VALUE_PROPERTY.equals(prop) || JOptionPane.INPUT_VALUE_PROPERTY.equals(prop))) {
        Object value = addQuestionPane.getValue();

        if (value == JOptionPane.UNINITIALIZED_VALUE) {
            //ignore reset
            return;
        }

        //Reset the JOptionPane's value.
        //If you don't do this, then if the user
        //presses the same button next time, no
        //property change event will be fired.
        addQuestionPane.setValue(
            JOptionPane.UNINITIALIZED_VALUE);

        if (value.equals("Enter")) {
            String questionTypedText = questionTextField.getText();
            String mqlYesTypedText = mqlYesTextField.getText();
            String mqlNoTypedText = mqlNoTextField.getText();

            sqlModel.addQuestion(questionTypedText, mqlYesTypedText, mqlNoTypedText);
            questionTextField.setText("");
            mqlYesTextField.setText("");
            mqlNoTextField.setText("");
        } else { //user closed dialog or clicked cancel
            addQuestionDialog.setVisible(false);
        }
    }
}

我已经检查了好几次代码,我没有发现任何问题,而且对话框做了他们应该做的事情,所以我猜有一个特殊的 addQuestion.set...(Object我应该添加的 setValue) 方法。

【问题讨论】:

    标签: java swing applet awt


    【解决方案1】:

    我是否需要为 JDialog 设置某个属性以确保它即使在我切换窗口时也保持不变。

    是的。

    我没有在 JDialog 的构造函数中为其分配父级,

    这就是问题所在。只要对话框的所有者可见,该对话框就会可见,因此您需要指定所有者 JFrame。

    【讨论】:

    • 请问我应该使用哪种方法使对话框保持打开状态,有什么办法可以不为对话框声明父级吗?
    • can I ask which method I should use so the dialog remains open - 使用适当的构造函数。 is there any way around not declaring a parent for the dialog? 然后你得到你刚才描述的行为,这是你不想要的。指定父JFrame有什么问题???
    • 你有什么理由避免设置父母?
    • JFrame 在另一个文件中,而对话框包含在 JPanel 中,而 JPanel 包含在 JFrame 中。如果我可以将面板设置为父级 *sigh
    • @AndrewBrick, The JFrame is in another file, - 那么你如何显示对话框呢?我猜你点击了一个 JButton 什么的?然后可以使用SwingUtilities.windowForComponent(...) 方法获取框架。代码不必在同一个源文件中。
    猜你喜欢
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 2011-03-07
    • 2020-12-02
    • 1970-01-01
    • 2018-04-13
    • 2016-04-14
    • 2021-02-13
    相关资源
    最近更新 更多