设置JFileChooser的位置

Ram*_*Ram 6 java swing jfilechooser

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

Sur*_*ran 9

不幸的是,没有简单的方法可以做到这一点,因为无论何时显示选择器,内部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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

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