当父JFrame出现时,为什么这个无模式的子JDialog最小化?

时间:2012-01-26 03:52:53

标签: java swing jdialog minimize

我的理解是,当父JFrame被最小化时,其子节点也被最小化,但是在下面的简单示例中它不会发生(即,当jframe被最小化时,子对话保持可见)。我错过了什么吗?

public class Test
{
    private JFrame f1;
    private JDialog d2;

    private void createAndShowGUI()
    {
        f1 = new JFrame("(parent frame)");
        f1.addWindowListener( new WindowAdapter()
        {
            public void windowClosing( WindowEvent e )
            {
                e.getWindow().dispose();
            }
        } );
        f1.setBounds(32, 32, 300, 200);
        d2 = new JDialog(f1, "child dialog" );
        d2.setBounds(100, 100, 300, 200);
        d2.setVisible( true );
        f1.setVisible( true );
    }

    public static void main( String[] args )
    {
        // Schedule a job for the event-dispatching thread:
        // creating and showing this application's GUI.
        SwingUtilities.invokeLater( new Runnable()
        {
            public void run()
            {
                Test test = new Test();
                test.createAndShowGUI();
            }
        } );
    }
}

谢谢!

1 个答案:

答案 0 :(得分:0)

尝试获取Window []并将它们全部关闭......也许这适用于每个操作系统。

private void createAndShowGUI() {
    f1 = new JFrame("(parent frame)");
    f1.addWindowListener( new WindowAdapter() {
        public void windowClosing( WindowEvent e ) {
            Window[] windows = e.getWindow().getOwnedWindows();
            for (Window window : windows) {
                window.dispose();
            }
        }
    } );