JFolderChooser.showOpenDialog 中的父组件是什么

发布于 2024-10-09 14:02:15 字数 254 浏览 0 评论 0原文

案例 1:

JFileChooser myFileChooser;
myFileChooser.showOpenDialog(this); //this = parent Component

案例 2:

JFileChooser myFileChooser;
myFileChooser.showOpenDialog(null);

这两种情况有什么实际区别?

Case 1:

JFileChooser myFileChooser;
myFileChooser.showOpenDialog(this); //this = parent Component

Case 2:

JFileChooser myFileChooser;
myFileChooser.showOpenDialog(null);

What is the practical difference between the two cases?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原野 2024-10-16 14:02:15

查看 JFileChooser 的 Javadoc

父参数确定两个
things:打开的框架
对话框取决于其组件
定位外观和感觉应有的位置
放置对话框时请考虑。如果
父级是一个 Frame 对象(例如
JFrame),那么对话框取决于
框架和外观
对话框相对于
框架(例如,居中于
框架)。如果父级是一个组件,
那么对话框取决于框架
包含该组件,并且是
相对于组件定位
(例如,以
成分)。如果父级为空
那么对话框依赖于不可见的
窗口,并将其放置在
依赖于外观和感觉的位置,例如
作为屏幕的中心。

在内部,它尝试使用此 JOptionPane.getWindowForComponent(parent) 来获取使用父级的窗口。依次检查父级是否为空...

if (parentComponent == null)
    return getRootFrame();

如果为空,则根级别框架将作为父容器返回。
使用内部 SwingUtilities.getSharedOwnerFrame()SwingUtilities.getSharedOwnerFrame() 的 javadoc 说...

返回一个工具包-私有、共享、
隐形框架是其所有者
JDialogs 和 JWindows 创建于
空所有者。

Checkout the Javadoc for JFileChooser

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.

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...

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...

Returns a toolkit-private, shared,
invisible Frame to be the owner for
JDialogs and JWindows created with
null owners.

沉鱼一梦 2024-10-16 14:02:15

您可以指定父级来确定对话框与哪个组件相关。它将确定对话框的位置(居中,相对于父对话框)。我还猜测该对话框将是模态的,从而阻塞父窗口。

如果指定 null,则显示的对话框将不属于任何组件,我猜它会显示在屏幕的左上角或中心(最后一种更有可能发生,我没有测试过)。

希望这有帮助!

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).

Hop this helps !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文