简体   繁体   English

尝试使用BufferedReader从文本文件读取以显示到JTextArea中

[英]Trying to read from a text file to display into a JTextArea using a BufferedReader

So I am trying to read a text file to display into a JTextArea I am having no luck getting it to show up. 因此,我试图读取文本文件以显示到JTextArea中,但我没有运气让它显示出来。 I believe the problem lies around the setup of my BufferedReader but I am not certain. 我相信问题出在我的BufferedReader的设置上,但我不确定。 It also may be a problem with my SOP section. 我的SOP部分也可能有问题。 Any help is appreciated. 任何帮助表示赞赏。

import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class WindowInventory extends JFrame {

    private JTextArea ta_inventory;

    public WindowInventory() {
        setSize(200, 500);
        setResizable(false);
        setTitle("Inventory");
        setLocation(200, 200);

        Container contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.setBackground(Color.white);

        ta_inventory = new JTextArea();
        ta_inventory.setBounds(0, 0, 200, 350);
        contentPane.add(ta_inventory);

        try {

            BufferedReader myReader = new BufferedReader(new FileReader("Inventory.txt"));

            while (myReader.ready()) {

                String Name = myReader.readLine();
                System.out.println(Name);
                String Price = myReader.readLine();
                System.out.println(Price);
                String ID = myReader.readLine();
                System.out.println(ID);

            }

            myReader.close();

        } catch (IOException e) {

            System.out.println("Problem reading from a file");

        }
    }
}

Yes, your method of setting up the reader is wrong. 是的,您设置阅读器的方法是错误的。 Take a look at Basic I/O for more deatils. 查看基本I / O ,了解更多详细信息。

However, you could try using something more like... 但是,您可以尝试使用更多类似...

try (BufferedReader myReader = new BufferedReader(new FileReader("Inventory.txt"))) {
    String text = null;
    while ((text = myReader.readLine()) != null) {
        System.out.println(text);
    }
} catch (IOException exp) {
    exp.printStackTrace();
}

Instead. 代替。

Now having said that. 现在这么说。 It would be easier to simple use JTextArea#read 简单使用JTextArea#read会更容易

Something like.... 就像是....

JTextArea textArea = new JTextArea(10, 20);
try (Reader myReader = new BufferedReader(new FileReader("Inventory.txt"))) {
    textArea.read(myReader, "Inventory");
} catch (IOException exp) {
    exp.printStackTrace();
}

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. 避免使用null布局,像素完美布局是现代ui设计中的一种幻觉。 There are too many factors which affect the individual size of components, none of which you can control. 有太多因素会影响组件的单个大小,您无法控制。 Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify Swing旨在与布局经理为核心一起工作,舍弃这些问题不会导致问题和问题的终结,您将花费越来越多的时间来尝试纠正

Text components like JTextArea really should be maintained within a JScrollPane . JTextArea这样的文本组件实际上应该在JScrollPane维护。 See How to Use Scroll Panes and How to Use Text Areas for more details 有关更多详细信息,请参见如何使用滚动窗格如何使用文本区域

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

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