设置 JFileChooser 的位置

发布于 2024-08-21 05:32:49 字数 109 浏览 7 评论 0原文

我们如何设置JFileChooser窗口的位置,我尝试了setLocation()setBounds()方法,但它不起作用。

How can we set the location of the JFileChooser window, I tried setLocation() and setBounds() methods but it doesn't works.

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

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

发布评论

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

评论(3

狼亦尘 2024-08-28 05:32:49

不幸的是,没有简单的方法可以做到这一点,因为每当您显示选择器时,内部 createDialog 方法都会将位置设置为父级的中心。

一种方法是继承 JFileChooser 并重写 createDialog 方法,如下所示:

   static class MyChooser extends JFileChooser {
        protected JDialog createDialog(Component parent)
                throws HeadlessException {
            JDialog dlg = super.createDialog(parent);
            dlg.setLocation(20, 20);
            return dlg;
        }
    }

现在您可以直接使用 MyChooser 而不是 JFileChooser。在上面的代码中,我已将位置硬编码为 20, 20,但您可以将其设置为您想要的任何值。

Unfortunatley there is no trivial way to do it, because whenever you show the chooser, the internal createDialog method will set the location to center of parent.

One way to do is to subclass JFileChooser and override createDialog method like this:

   static class MyChooser extends JFileChooser {
        protected JDialog createDialog(Component parent)
                throws HeadlessException {
            JDialog dlg = super.createDialog(parent);
            dlg.setLocation(20, 20);
            return dlg;
        }
    }

Now you can directly uise MyChooser instead of JFileChooser. In above code I have hardcoded the location to 20, 20, but you can set it to whatever you want.

鱼窥荷 2024-08-28 05:32:49

来自 JavaDoc for JFileChoosershowDialog,看起来您对对话框的位置没有太多控制放置。

parent 参数决定两件事:打开的对话框所依赖的框架以及放置对话框时外观和感觉应考虑其位置的组件。如果父级是 Frame 对象(例如 JFrame),则对话框取决于框架,并且外观和感觉将对话框相对于框架定位(例如,在框架上居中)。如果父级是组件,则对话框取决于包含该组件的框架,并且相对于该组件定位(例如,在组件上方居中)。如果父级为空,则对话框依赖于不可见的窗口,并且它被放置在依赖于外观和感觉的位置,例如屏幕的中心。

From the JavaDoc for JFileChooser's showDialog, it looks as if you do not have a great amount of control over where the dialog gets placed.

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.

面如桃花 2024-08-28 05:32:49

您可以尝试将 JFileChooser 的父级设置为当前的 JFrame/JPanel。

JFrame parentToBe = new JFrame();
JFileChooser chooser = new JFileChooser();
int pathSelection = chooser.showSaveDialog(parentToBe);

这应该可以解决问题。

您还可以在此处查看参考。

You could try to set the parent of your JFileChooser to your current JFrame/JPanel.

JFrame parentToBe = new JFrame();
JFileChooser chooser = new JFileChooser();
int pathSelection = chooser.showSaveDialog(parentToBe);

This should do the trick.

You can also look here for reference.

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