作为调用父框架的对话框启动应用程序

Start application as Dialog that calls parent frame

情况如下:

我的应用程序由一个包含 x 个元素的对话框和一个按钮组成。用户在与元素交互后按下按钮,如果他以特定方式与它们交互,则只有对话框所在的父框架才会出现。

为了这个目的,我目前知道这种方法:

public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(false);
                jDialog.setVisible(true);
            }
        });
    }

然后在驻留在 jDialog 中的 Button 上添加此命令:

new NewJFrame().setVisible(true);

这个技巧做得很好很简洁,但是之前使用 new NewJFrame().setVisible(false); 调用的实例仍然是 运行(据我所知)。

无论如何我都不能在按钮(驻留在 jDialog 中)按下时执行此操作,因为使用类似的东西:

NewJFrame.setVisible(true);

(它目前给我错误:Non-static method cannot be referenced from static context

确保对话框是模态的,您可以简单地执行以下操作:

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            NewJFrame newJFrame = new NewJFrame();
            newJFrame.pack();
            // no need to set visible false. It already is

            MyDialog myDialog = new MyDialog(newJFrame); 
            // make sure the super constructor makes the dialog modal

            myDialog.pack();
            myDialog.setVisible(true);

            // here the dialog is no longer visible
            // and we can extract data from it and send it to the JFrame if needed

            newJFrame.setVisible(true); // ****** here
        }
    });
}

否则,如果您绝对必须 fiddle 使用 JDialog 中的 JFrame,只需将 NewJFrame 传递到 JDialog 的构造函数中,这是无论如何都需要做的事情,因为它应该在 JDialog 超级构造函数中使用,使用它来设置 NewJFrame 字段,并在对话框内的实例上调用 setVisible(true)