Append Text to JTextArea on Multiple Lines?

与世无争的帅哥 提交于 2019-12-24 02:44:36

问题


I have a string with multiple numbers, when printed out it looks something like this:

2
4
5
10
20
25
50

However, when I append the string to a JTextArea it looks like this:

24510202550

How can I make the JTextArea look like the ouput with the numbers on seperate lines? Thanks!


回答1:


You're probably using System.out.println() to print to console. The System.out.println() will add '\n' character to the end of each line for you.

But to output strings to JTextArea in same way use jTextArea.append('\n');.




回答2:


put new line character which is \n at the end of the string




回答3:


Your JTextArea extends JTextComponent and thus has its own read(...) method that allows it to read in text files (among other things), understand them in an OS-dependent manner, and then display them, complete with new-lines. For example, please see this code which is essentially,

     BufferedReader br = null;
     try {
        br = new BufferedReader(new FileReader(file));
        textArea.read(br, null); // here we read in the text file
     } catch (FileNotFoundException e) {
        e.printStackTrace();
     } catch (IOException e) {
        e.printStackTrace();
     } finally {
        if (br != null) {
           try {
              br.close();
           } catch (IOException e) {}
        }
     }


来源:https://stackoverflow.com/questions/28307990/append-text-to-jtextarea-on-multiple-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!