![](/img/trans.png)
[英]JFileChooser showOpenDialog method not working with ActionListener
[英]ShowopenDialog not working under actionlistener
因此,我一直试图制作一个打开文本文件的文件选择器,然后将内容粘贴到我已定义为textArea的JtextArea中。 但是我不能让showOpenDialog在有参数(this)时不给出错误,我进行了研究,答案是填写(null),这确实使filechooser起作用,但是当我尝试打印它的内容时也只是返回null。 我正在使用Eclipse程序,因此使用自动填充的代码。 我对Java相当陌生,不知道出了什么问题。 如果这不是在此处发布内容的方式,我真的很抱歉。
JButton btnNewButton = new JButton("Bladeren");
btnNewButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println("Selected file: " + selectedFile.getAbsolutePath());
String content = readFile(selectedFile, StandardCharsets.UTF_8);
System.out.println(content);
textArea.setText(content);
}
}
private String readFile(File selectedFile, Charset utf8) {
// TODO Auto-generated method stub
return null;
}
}
);
您可以查看API ,该参数必须来自Component
类型。 那么在您的示例中this
意味着什么? 您的课程从哪个其他类/接口extends
或implements
?
您没有粘贴readFile
方法的代码,但问为什么它返回null? 这样我们就无法为您提供帮助,因此请发布代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.