您可以复制几乎除了其对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;
}
}
}
谢谢你,所以基本上我必须把自己包裹起来,但这很简单。感谢代码示例。 – ted
@ ye yeh,只是包装JFrame并添加一些需要的侦听器,以便它像一个附加的框架。您还可以通过将“setEnabled(false)”设置为主框架来使“子”框架像模态JDialog一样工作。 –