使用JFileChooser打开文件 [英] Opening files with JFileChooser

查看:126
本文介绍了使用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 MenuItems 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆