簡體   English   中英

將數組的字符串輸出到Java中的JTextArea中

[英]Output strings of an array into a JTextArea in java

嗨,我制作了一個程序,該程序讀取包含單詞的文本文件並將其添加到數組中。 現在,我需要在我創建的JTextArea中顯示這些單詞,但我不確定如何顯示。 文本文件每行包含一個單詞,這就是我希望JTextArea也顯示它們的方式。

這是迄今為止的代碼。 我擁有的JTextArea稱為textArea(通過另一種方法創建)

    public static void file() {

    List<String> wordList = new ArrayList<String>();

    BufferedReader br = null;
    try {

        br = new BufferedReader(new FileReader("data/WordFile.txt"));
        String word;

        while ((word = br.readLine()) != null) {
            wordList.add(word);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    String[] words = new String[wordList.size()];
    wordList.toArray(words);
}

創建一個JTextArea對象。

因此,@ Andrew建議正確的功能是JTextArea.append(String)

JTextArea textArea = new JTextArea();

for(String W: Words)
  textArea.append(W);

JTextArea教程Java Swing

看一下教程,看看如何使用TextAreas。 基本上,您想要做的是遍歷數組並通過Event Dispatcher Thread(負責GUI的線程)打印其內容。 這通常是通過使用SwingUtils.invokeLater()完成的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM