2012-06-29 55 views
0

我想有一个窗口的行为就像一个对话框,它关闭与父窗口,但它应该像一个正常的框架,特别是它应该有最大化/恢复按钮。我如何创建绑定到父窗口的窗口(它们在父窗口关闭时关闭)并继承一些属性,即windowicon?带调整大小的对话框最小化或框架取决于父

我能想到的最好的方法是编写我自己的课程,包装一个JFrame并带上父母。这个类将一个Listener安装到父级并跟踪其所有实例,以便在父级关闭时关闭所有实例。 Exit_on_close不能用于父级,因为应用程序的其余部分应该继续运行。

那么,有没有一种简单的方法,还是我必须推出自己的班级?

回答

2

您可以复制几乎除了其对JFrame的顶部定位任何的JDialog行为(有该案例适用于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; 
     } 
    } 
} 
+0

谢谢你,所以基本上我必须把自己包裹起来,但这很简单。感谢代码示例。 – ted

+1

@ ye yeh,只是包装JFrame并添加一些需要的侦听器,以便它像一个附加的框架。您还可以通过将“setEnabled(false)”设置为主框架来使“子”框架像模态JDialog一样工作。 –