JFileChooser 显示错误的当前目录
JFileChooser showing wrong current directory
我有以下代码。问题是当我 select FileChooser 中的某个目录(例如 C:\Windows\System32
)时,它会打印:C:\Windows
(总是比实际低一级)。
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Wybierz folder z logami");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(true);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
path = chooser.getCurrentDirectory().toString();
jLabel1.setText(path);
getCurrentDirectory()
returns 当前显示目录的路径。如果您选择 当前 目录中的子目录,它被认为是当前选择的文件。
因此使用 getSelectedFile()
,即使您知道它实际上是一个目录。
我有以下代码。问题是当我 select FileChooser 中的某个目录(例如 C:\Windows\System32
)时,它会打印:C:\Windows
(总是比实际低一级)。
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Wybierz folder z logami");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(true);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
path = chooser.getCurrentDirectory().toString();
jLabel1.setText(path);
getCurrentDirectory()
returns 当前显示目录的路径。如果您选择 当前 目录中的子目录,它被认为是当前选择的文件。
因此使用 getSelectedFile()
,即使您知道它实际上是一个目录。