简体   繁体   English

如何在Java中正确实现FileChooser

[英]How to properly implement the FileChooser in Java

So I'm having problems implementing the JFileChooser, Im making a very simple program that needs to grab an CSV file. 所以我在实现JFileChooser时遇到问题,我制作了一个非常简单的程序,需要抓取CSV文件。 I was hoping to implement a very simple file chooser that would then check to make sure the extension was a CSV. 我希望实现一个非常简单的文件选择器,然后检查该扩展名以确保扩展名为CSV。

Here is my code for my run class where the user would be choosing the file. 这是我的运行类的代码,用户可以在其中选择文件。

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;


public class Run 
{   
    JFrame frame = new JFrame();
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.showOpenDialog(frame);
    File selFile = fileChooser.getSelectedFile();
    Reader reader = new Reader(selFile);
}

And here is the error I'm getting in eclipse BEFORE I compile. 这是我编译之前在eclipse中遇到的错误。

Syntax error on tokens, misplaced construct.

The error occurs on the period between fileChooser and showOpenDialog. 该错误发生在fileChooser和showOpenDialog之间的时间段。

The second error is on frame that which says. 第二个错误是上面所说的。

Syntax error on token "frame", VariableDecleratorID expected after this token.

As of now I'm not entirely sure what I'm doing wrong. 截至目前,我还不确定自己在做什么错。

Thank you for any help. 感谢您的任何帮助。

All your lines of code within the class are declarations of instance variables and their initialization except 您在该类中的所有代码行都是实例变量的声明及其初始化,除了

fileChooser.showOpenDialog(frame);

This is a invocation of a method on an object, as such it cannot be a member of a class. 这是对对象的方法的调用,因此它不能是类的成员。

Class members (simplified) are attributes and methods (extended version: it can also include static variables, static methods, inner classes, static blocks, ....) 类成员(简化)是属性和方法(扩展版本:它也可以包括静态变量,静态方法,内部类,静态块等)。

The reasoning behind it is simple: in an OO environment nothing happens if you don't trigger a method invocation and nothing should happen. 其背后的原因很简单:在OO环境中,如果您不触发方法调用就不会发生任何事情,并且什么也不会发生。

So a first step for you is to put the code inside a method, this might be the constructor or a plain method. 因此,第一步是将代码放入方法中,这可能是构造函数或简单方法。

Second step is that you should call this method. 第二步是您应该调用此方法。 If it is in the constructor then it is executed when (and every time) a new instance of your class is created. 如果它在构造函数中,则它将在(每次)创建类的新实例时执行。 Otherwise you need to instantie the class and then call the method. 否则,您需要实例化该类,然后调用该方法。

Third step is that all the code needs to be initiated when the program launches, you launch a java program by instructing the JVM to "run" a specific class, this class has then have to have a public static void main(String[] args) method, thats the entry point of every java application. 第三步是程序启动时需要启动所有代码,您通过指示JVM“运行”特定类来启动Java程序,然后该类必须具有公共静态void main(String [] args )方法,那就是每个Java应用程序的入口点。

If you just want to test something, put all the 5 lines of code inside a main method... 如果您只想测试某些东西,请将所有5行代码放入main方法中...

so 所以

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;


public class Run {   
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.showOpenDialog(frame);
        File selFile = fileChooser.getSelectedFile();
        Reader reader = new Reader(selFile);
    }
}

The statement 该声明

fileChooser.showOpenDialog(frame);

is a non-declarative statement so needs to be in a code block such as a method. 是非声明性语句,因此需要位于代码块(例如方法)中。 The application has no main method so that can be simply be used not only as the surrounding code block for the statement but also as the entry point for the application. 该应用程序没有main方法,因此不仅可以简单地用作语句的周围代码块,还可以用作应用程序的入口点。

public class Run {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.showOpenDialog(frame);
                File selFile = fileChooser.getSelectedFile();
                // use selFile...
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM