java JFolderChooser.showOpenDialog 中的父组件是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4545557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is parent Component for in JFolderChooser.showOpenDialog
提问by Stefanos Kargas
Case 1:
情况1:
JFileChooser myFileChooser;
myFileChooser.showOpenDialog(this); //this = parent Component
Case 2:
案例2:
JFileChooser myFileChooser;
myFileChooser.showOpenDialog(null);
What is the practical difference between the two cases?
这两种情况有什么实际区别?
回答by Favonius
Checkout the Javadoc for JFileChooser
查看 JFileChooser 的 Javadoc
The parent argument determines two things: the frame on which the open dialog depends and the component whose position the look and feel should consider when placing the dialog. If the parent is a Frame object (such as a JFrame) then the dialog depends on the frame and the look and feel positions the dialog relative to the frame (for example, centered over the frame). If the parent is a component, then the dialog depends on the frame containing the component, and is positioned relative to the component (for example, centered over the component). If the parent is null, then the dialog depends on no visible window, and it's placed in a look-and-feel-dependent position such as the center of the screen.
parent 参数决定了两件事:打开的对话框所依赖的框架和放置对话框时观感应该考虑的组件。如果父对象是 Frame 对象(例如 JFrame),则对话框取决于框架,并且外观将对话框相对于框架定位(例如,居中于框架之上)。如果父组件是组件,则对话框取决于包含组件的框架,并相对于组件定位(例如,在组件上方居中)。如果parent 为 null,则对话框不依赖于可见窗口,并且它被放置在外观相关的位置,例如屏幕的中心。
internally it tries to get a window using the parent using this JOptionPane.getWindowForComponent(parent)
. Which in turn checks if parent is null or not...
在内部,它尝试使用 this 使用父级获取窗口JOptionPane.getWindowForComponent(parent)
。依次检查 parent 是否为空...
if (parentComponent == null)
return getRootFrame();
If it is null then Root level frame is returned as parent container.
Using the internal SwingUtilities.getSharedOwnerFrame()
. The javadoc for SwingUtilities.getSharedOwnerFrame()
says...
如果它为空,则根级框架作为父容器返回。使用内部SwingUtilities.getSharedOwnerFrame()
. javadoc forSwingUtilities.getSharedOwnerFrame()
说...
Returns a toolkit-private, shared, invisible Frame to be the owner for JDialogs and JWindows created with null owners.
返回一个工具包私有的、共享的、不可见的框架,作为使用空所有者创建的 JDialogs 和 JWindows 的所有者。
回答by Hal
You can specify the parent to determine which component the dialog is related to. It will determine the position of your dialog (centered, relative to the parent). I also guess that the dialog will be modal, thus blocking the parent window.
您可以指定父级来确定对话框与哪个组件相关。它将确定对话框的位置(居中,相对于父级)。我还猜测对话框将是模态的,从而阻塞了父窗口。
If you specify null, the dialog shown won't belong to any component, and I guess it will be displayed either at the top left of the screen or at the center (the last being more likely to happen, I have not tested).
如果您指定 null,则显示的对话框将不属于任何组件,我猜它会显示在屏幕的左上角或中心(最后一个更有可能发生,我没有测试过)。
Hop this helps !
跳这有帮助!