文件Java:Bug:JFileChooser只是偶尔出现
我有一个打开JFileChooser的方法,允许用户选择一个文件,以便以后复制它的URL。然而,JFileChooser只在我运行程序时出现(?)
我不明白代码出了什么问题。提前谢谢
More clearly: The UI of the JFileChooser does not show up.
String readThisGlos = null;
JFrame parentFrame = new JFrame();
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Choose");
int userSelection = fileChooser.showOpenDialog(parentFrame);
if (userSelection == JFileChooser.APPROVE_OPTION) {
File fileToSave = fileChooser.getSelectedFile();
readThisGlos = fileToSave.getAbsolutePath();
} else {
// Error
}
System.out.println(readThisGlos);
return readThisGlos;
编辑:我知道该方法正在运行,因为当我放置System.out.println("//something")
;在这个方法的开始,它是有效的
If I put
System.out.println("//something");
betweenfileChooser
andint userSelection
, the UI shows up.
# 1 楼答案
我曾经遇到过类似的问题,我甚至无法描述我当时的沮丧
试试这个:
showOpenDialog(parentFrame)
更改为showOpenDialog(null)
(正如用户所建议的,不必要的事情往往会产生问题)李>