我试图放置在标签中选择的文件夹的路径,但它不会将文件转换为字符串。
公共(public)无效mousePressed(MouseEvent arg0) { JFileChooser 文件选择器; 字符串选择器标题 = null;
filechooser = new JFileChooser();
filechooser.setCurrentDirectory(new java.io.File("."));
filechooser.setDialogTitle(choosertitle);
filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//
// disable the "All files" option.
//
filechooser.setAcceptAllFileFilterUsed(false);
//
int returnValue = filechooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = filechooser.getSelectedFile();
System.out.println(selectedFile.getName());
System.out.println("getCurrentDirectory(): "
+ filechooser.getCurrentDirectory());
lbl_Arborescence.setText(filechooser.getCurrentDirectory());
System.out.println("getSelectedFile() : "
+ filechooser.getSelectedFile());
}
}
最佳答案
JFileChooser.getCurrentDirectory() 返回一个 File 对象。您想获取该文件的名称。使用类似的东西
lbl_Arborescence.setText(fileChooser.getCurrentDirectory().getPath());
关于java - JLabel 类型中的 setText(String) 方法不适用于参数(File),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32386994/