简体   繁体   English

追加到JTextArea不会在'\\ n'上创建换行符

[英]Appending to JTextArea is not creating a line break on '\n'

I'm loading string resources from a text file (so as to not have to rebuild if I need to change them) which when appended to the JTextArea displays as "Some sentence,\\n on the same line." 我正在从文本文件加载字符串资源(以便在需要更改时不必重建),该字符串资源在附加到JTextArea时显示为“某些句子,\\ n在同一行上”。

When I hard code the exact same String, it appends fine. 当我对完全相同的String进行硬编码时,它会很好地附加。
Where could this be going wrong? 这可能在哪里出错?

What does your text file look like? 您的文本文件是什么样的? If "\\n" is in the text file it's probably copied literally, ie it's not treated as an escape sequence. 如果文本文件中有“ \\ n”,则可能会原样复制,即不会被视为转义序列。

EDIT: You could try reading the text file as a property file and automatically have eg \\n parsed a newline. 编辑:您可以尝试将文本文件读取为属性文件,并自动将\\ n解析为换行符。

Properties p = new Properties();
InputStream fileStream = new FileInputStream("myfile.txt");
p.load(fileStream);    
String value = p.getProperty(key);

In the text file do this... 在文本文件中执行此操作...

"1st_Half_of_String" +"\\n"+ "2nd_Half_of_String" “ 1st_Half_of_String” +"\\n"+ “ 2nd_Half_of_String”

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

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