我想使用DirectoryChooser从目录中获取路径。我的代码如下所示:

DirectoryChooser dc = new DirectoryChooser();
dc.showDialog(stage);
File f = dc.getInitialDirectory();
String s = f.getAbsolutePath();
System.out.println(s);


但这不起作用,有人可以帮我吗?方法getInitialDirectory()始终为null

最佳答案

它应该看起来像这样:

DirectoryChooser directoryChooser = new DirectoryChooser();
File selectedDirectory = directoryChooser.showDialog(stage);

if(selectedDirectory == null){
     //No Directory selected
}else{
     System.out.println(selectedDirectory.getAbsolutePath());
}


更多信息:example of DirectoryChooser

10-05 19:34