繁体   English   中英

JFileChooser getCurrentDirectory返回错误的当前目录?

[英]JFileChooser getCurrentDirectory returning wrong current directory?

我在应用程序中使用JFileChooser来浏览目录,但是当我选择目录时,它返回我选择的文件夹上方文件夹的路径。 即我选择“C:\\ Test”并返回“C:\\”

这是我正在使用的代码

            JFileChooser c = new JFileChooser();
            c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int rVal = c.showSaveDialog(c);
            if (rVal == JFileChooser.APPROVE_OPTION) {
                txtDirectory.setText("");
                CC_Test.MsgBox(c.getCurrentDirectory().toString());
                txtDirectory.setText(c.getCurrentDirectory().toString());
            }
            if (rVal == JFileChooser.CANCEL_OPTION) {
                txtDirectory.setText("");
            }

你应该用

c.getSelectedFile()

代替

c.getCurrentDirectory()

为了获得所选文件(在这种情况下也称为目录)。 否则它会生成filechooser面板(父级)中显示的目录,而不是选中的目录。

要获取所选文件或目录,请使用:

c.getSelectedFile();

如果你使用

c.getCurrentDirectory();

返回取决于操作系统。

您必须使用JFileChooser.getSelectedFile() File类既适用于目录,也适用于文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM