使用JFileChooser打开文件 [英] Opening files with JFileChooser
问题描述
作为一个小小的项目,我认为制作文本编辑器会很酷。我目前仍然坚持打开文件。这是我打开文件的代码( e
是 ActionEvent
,open是 JMenuItem
):
As a little side project I'd thought it would cool to make a text editor. I'm currently stuck on opening files. This is my code for opening the file(e
is an ActionEvent
, open is a JMenuItem
):
else if (e.getSource() == open) {
JFileChooser choice = new JFileChooser();
int option = choice.showOpenDialog(this);
if (option == JFileChooser.APPROVE_OPTION) {
try{
Scanner scan = new Scanner(new FileReader((open).getSelectedFile().getPath()));
}
}
}
尝试街区给了我麻烦。 Eclipse声明 getSelectedFile()
未定义类型 JMenuItem
。对于 MenuItem
,它似乎也未定义。有没有其他方法可以解决这个问题,或者另一种方法是什么?
The try block is giving me the trouble. Eclipse is saying that getSelectedFile()
is undefined for type JMenuItem
. It also appears to be undefined for MenuItem
s as well. Is there another way to approach this, or another method that works the same?
推荐答案
你需要调用
一旦返回,请将您的代码更改为:上的getSelectedFile()
> JFileChooser
You need to call getSelectedFile()
on the JFileChooser
once it has returned, so change your code to:
choice.getSelectedFile()
这篇关于使用JFileChooser打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!