Java 尝试使用JFileChooser选择文件夹

Java 尝试使用JFileChooser选择文件夹,java,Java,我正在尝试使用JFileChooser选择一个文件夹。它返回父文件夹,而不是当前文件夹。 示例我选择文件夹文档,然后备份。JFileChooser返回备份的文档 String getFilePath() { JFileChooser fc = new JFileChooser(); // fc.setCurrentDirectory(new File(System.getProperty("user.home"))); fc.setFileSelectionMode

我正在尝试使用JFileChooser选择一个文件夹。它返回父文件夹,而不是当前文件夹。 示例我选择文件夹文档,然后备份。JFileChooser返回备份的文档

String getFilePath()
{

    JFileChooser fc = new JFileChooser();

//  fc.setCurrentDirectory(new File(System.getProperty("user.home")));  
      fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
      fc.setAcceptAllFileFilterUsed(false);

    int returnVal = fc.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File rt=fc.getCurrentDirectory();   
            String t=rt.getName();
            return t;
        }

    return null;
}

问题是您正在使用而不是。

使用getSelectedFile