如何从文本文件读取到 JLabel 或 JTextArea?

How Read from Text File to JLabel or JTextArea?

我正在尝试创建一个弹出窗口 window,内容如下:

玩过的游戏数:2
总分:10
平均分:5

我将数字 2、10 和 5 存储在一个文本文件中。我只是希望能够将文本文件中的数字读入(这就是我感到困惑的地方)JLabel 或 JTextArea?我还希望能够清除分数并将它们全部重置为 0。我认为这应该不会太难,但我可能是错的。我应该在读入数字时将它们存储到 ArrayList 中吗?

这是我目前的代码:

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.Scanner;

public class HistoryPopUp {

    JFrame history;
    JPanel panel;
    JLabel numGames, totalScore, avgScore;
    JTextArea games,score,aScore;
    JButton clearHistory;

    HistoryPopUp(){
        history = new JFrame();
        panel = new JPanel(new GridLayout(3, 1));
        numGames = new JLabel("Number of Games Played: ");
        totalScore = new JLabel("Total Score: ");
        avgScore = new JLabel("Average Score: ");
        games = new JTextArea();
        score = new JTextArea();
        aScore = new JTextArea();


        clearHistory = new JButton();

        try {
            String textLine;
            FileReader fr = new FileReader("history.txt");
            BufferedReader reader = new BufferedReader(fr);
            while((textLine=reader.readLine()) != null){
                textLine = reader.readLine();
                games.read(reader,"Something");
                score.read(reader, "seomthing");
                aScore.read(reader,"balh");
            }

            reader.close();

        }catch(IOException ex){
                System.out.println("ABORT! YOU KILLED IT!!");
            }

        history.pack();
        history.setVisible(true);

        panel.add(games);
        panel.add(score);
        panel.add(aScore);

        JOptionPane.showMessageDialog(null, panel, "History of Games Played", JOptionPane.PLAIN_MESSAGE);

    }
}

编辑:格式化

您遇到的问题是您有 4 段代码都试图从同一个数据池中读取,scoreaScore 不太可能在 reader 阅读一次 games 完成

如果您只想使用 JLabels,您可以这样做...

String[] headers = {"Number of Games Played:", "Total Score:", "Average Score:"};
JLabel[] labels = new JLabel[3];
for (int index = 0; index < labels.length; index++) {
    labels[index] = new JLabel();
    // Add label to screen
}

try (BufferedReader br = new BufferedReader(new FileReader(new File("history.txt")))) {
    String text = null;
    int lineCount = 0;
    while ((text = br.readLine()) != null && lineCount < 3) {
        System.out.println(text);
        labels[lineCount].setText(headers[lineCount] + " " + text);
        lineCount++;
    }
} catch (IOException ex) {
    ex.printStackTrace();
}

如果你想使用 JTextArea,你可以这样做...

String[] headers = {"Number of Games Played:", "Total Score:", "Average Score:"};
JTextArea textArea = new JTextArea(3, 20);
// Add text area to container

try (BufferedReader br = new BufferedReader(new FileReader(new File("history.txt")))) {
    String text = null;
    int lineCount = 0;
    while ((text = br.readLine()) != null && lineCount < 3) {
        System.out.println(text);
        textArea.append(headers[lineCount] + " " + text + "\n");
        lineCount++;
    }
} catch (IOException ex) {
    ex.printStackTrace();
}

您也可以以类似的方式使用 JTextField 的数组

不要那样做。

不要将 "persistence"(信息保存在文件中)与 "presentation" 混用。

相反;阅读有关 Swing 文档模型的信息;或使用 "model view controller" 方法。

但是,从您编写的代码来看……您似乎仍然对 Swing 的非常基本的元素有问题 UI。重点是:有javadoc;以及来自 Oracle 的优秀教程。不要期望其他人会向您详细解释如何使用它们。我的意思是:首先学习如何使用各种UI元素;逐个;了解如何将数据输入其中;以及这些信息会是什么样子。

然后,当你理解了UI个元素;想一个合理的方法来取消持久化你的数据;并将其放入您的 UI 元素中(如前所述:在同一段代码中执行所有这些操作是糟糕的设计;应该避免这种情况)。