我有一个按钮,单击它我希望弹出 JFileChooser。我已经尝试过这个
JButton browse= new JButton("Browse");
add(browse);
browse.addActionListener(new ClassBrowse());
public class ClassBrowse implements ActionListener {
public void actionPerformed(ActionEvent e) {
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
// return the file path
} catch (Exception ex) {
System.out.println("problem accessing file"+file.getAbsolutePath());
}
}
else {
System.out.println("File access cancelled by user.");
}
}
}
Bhe上面给出了错误The method showOpenDialog(Component) in the type JFileChooser is not applicable for the arguments (ClassName.ClassBrowse)
另外,我希望它返回完整的文件路径。我该怎么做?
-
ActionListener
不是一个Component
,你无法通过this
到文件选择器。
- 你可以看看File#getCanonicalPath http://docs.oracle.com/javase/7/docs/api/java/io/File.html#getCanonicalPath%28%29获取文件的完整路径,但你不能
return
it, as actionPerformed
只返回一个void
(或者没有返回类型)。但是,您可以设置一些其他变量,调用另一个方法,甚至设置一个文本JLabel
or JTextField
... 例如...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)