![](/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.