简体   繁体   English

仅在显示JFrame之后如何在Java中读取文件?

[英]How to read file in Java only after JFrame is displayed?

I'm using Java Swing. 我正在使用Java Swing。 Initially I want to read a file (which is quite big). 最初,我想读取一个文件(很大)。 So the frame gets displayed after the file is completely. 因此,在文件完全显示之后,将显示该框架。 Whereas I want the frame to first load (displayed) and then the file should be read. 而我要先加载(显示)框架,然后再读取文件。

class Passwd {

    JFrame jfrm; 
    // other elements

    Passwd() {
        start();

        // Display frame.
        jfrm.setVisible(true);
    }

    public void start() {

        // Create a new JFrame container.
        jfrm = new JFrame("Password Predictability & Strength Measure");

        // Specify FlowLayout for the layout manager.
        //jfrm.setLayout(new FlowLayout());
        jfrm.setLayout(null);

        // Give the frame an initial size.
        jfrm.setSize(450, 300);

        // align window to center of screen
        jfrm.setLocationRelativeTo(null);  
        // Terminate the program when the user closes the application.
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // some elements

        File file = new File("file.txt");
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = br.readLine()) != null) {
                // operation
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 

    }

    public static void main(String args[]) {

        // Create the frame on the event dispatching thread.
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {

                new Passwd();                   

            }
        });
    }
}

How can I read the file after the frame is displayed? 框架显示后如何读取文件?

The JFrame should display immediately, so that's not the problem. JFrame应该立即显示,所以这不是问题。 The problem is that you're reading in the file on the Swing event thread, and this blocks its ability to display the JFrame. 问题在于您正在读取Swing事件线程上的文件,这阻止了它显示JFrame的功能。 The solution is to not do this, to instead read the file in a background thread, such as via a SwingWorker. 解决方案是不执行此操作,而是在后台线程中读取文件,例如通过SwingWorker。 This way the JFrame can display unimpeded, and the file reading will not interfere with Swing functioning. 这样,JFrame可以不受阻碍地显示,并且文件读取不会干扰Swing的功能。

So if the file reading will not change the state of Swing components, use a simple background thread: 因此,如果读取文件不会更改Swing组件的状态,请使用简单的后台线程:

new Thread(() -> {
    File file = new File("file.txt");
    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
        String line;
        while ((line = br.readLine()) != null) {
            // operation
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}).start();

If the reading in will change the state of the GUI as the reading occurs, again, use a SwingWorker. 如果读入将在发生读入时更改GUI的状态,请再次使用SwingWorker。

Side issue: avoid using null layouts as they'll come back to bite you. 附带问题:避免使用空布局,因为它们会再次咬住您。

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

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