您可以複製幾乎除了其對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一樣工作。 –