具有调整大小的对话框最小化或帧取决于父级

时间:2012-06-29 11:06:15

标签: java swing jframe jdialog

我希望有一个窗口,其行为类似于Dialog,在术语中,它与父窗口关闭,但它的行为应该像普通的Frame,特别是它应该有最大化/恢复按钮。如何创建绑定到父窗口的窗口(它们在父关闭时关闭)并继承一些属性,即windowicon?

我能想到的最好的就是编写自己的类,它包装了一个JFrame并接受了一个父级。此类将一个监听器安装到父级并跟踪其所有实例,因此它可以在关闭父级时关闭所有实例。 Exit_on_close不能用于父级,因为应用程序的其余部分应该继续运行。

那么有一种简单的方法,还是我必须自己上课?

1 个答案:

答案 0 :(得分:2)

你可以复制几乎任何JDialog行为,除了它在JFrame之上的定位(对于Win平台有一些原生的解决方案,但是使用它真的很糟糕)。

以下是您可以在几分钟内完成的示例:

<强> ChildFrameTest.java

public class ChildFrameTest
{
    public static void main ( String[] args )
    {
        JFrame application = new JFrame ();
        application.setSize ( 600, 600 );
        application.setLocationRelativeTo ( null );
        application.setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE );

        JChildFrame tool = new JChildFrame ( application );
        tool.setModalExclusionType ( Dialog.ModalExclusionType.APPLICATION_EXCLUDE );
        tool.setSize ( 100, 600 );
        tool.setLocation ( application.getX () + application.getWidth (), application.getY () );

        new WindowFollowListener ( tool, application );

        application.setVisible ( true );
        tool.setVisible ( true );
    }

    public static class JChildFrame extends JFrame
    {
        public JChildFrame ( JFrame parent )
        {
            super ();
            parent.addWindowListener ( new WindowAdapter ()
            {
                public void windowClosing ( WindowEvent e )
                {
                    dispose ();
                }
            } );
        }
    }
}

和WindowFollowListener一起添加一些漂亮的子框架行为:

<强> WindowFollowListener.java

public class WindowFollowListener extends ComponentAdapter
{
    private boolean enabled = true;
    private Window followingWindow;
    private Window parentWindow;
    private Point ll;

    public WindowFollowListener ( Window followingWindow, Window parentWindow )
    {
        super ();

        this.followingWindow = followingWindow;
        this.parentWindow = parentWindow;
        this.ll = parentWindow.getLocation ();

        parentWindow.addComponentListener ( this );
    }

    public boolean isEnabled ()
    {
        return enabled;
    }

    public void setEnabled ( boolean enabled )
    {
        this.enabled = enabled;
    }

    public Window getFollowingWindow ()
    {
        return followingWindow;
    }

    public void setFollowingWindow ( Window followingWindow )
    {
        this.followingWindow = followingWindow;
    }

    public Window getParentWindow ()
    {
        return parentWindow;
    }

    public void setParentWindow ( Window parentWindow )
    {
        this.parentWindow = parentWindow;
    }

    public void componentResized ( ComponentEvent e )
    {
        this.ll = parentWindow.getLocation ();
    }

    public void componentMoved ( ComponentEvent e )
    {
        if ( enabled && followingWindow != null && parentWindow != null )
        {
            Point nl = parentWindow.getLocation ();
            Point fwl = followingWindow.getLocation ();
            followingWindow.setLocation ( fwl.x + nl.x - ll.x, fwl.y + nl.y - ll.y );
            this.ll = nl;
        }
    }
}