简体   繁体   English

Java将长行文本解析为多行

[英]Java parsing long lines of text into multiple lines

I am new to programming and I would like some tips. 我是编程新手,我想要一些技巧。 I have a program that reads from a text file using the the following method: 我有一个使用以下方法从文本文件读取的程序:

BufferedReader reader = new BufferedReader(new FileReader(file));
.....

This works but the problem is that some lines are long and I can't see all the text in the GUI. 这行得通,但问题是有些行很长,我无法在GUI中看到所有文本。 How should I tell Java to go to next line if length is equal to a certain amount? 如果长度等于一定量,我应该如何告诉Java下一行?

Example text: 示例文字:

A week after Mozart died, his ghost was discovered trying to erase his music. When asked why, it said "I'm decomposing.

Instead of two lines, I want to parse it into four lines. 我想将其解析为四行,而不是两行。 Any help would be appreciated. 任何帮助,将不胜感激。

as per your comments, you are using JLabel for displaying the data. 根据您的评论,您正在使用JLabel显示数据。

to show multi-line text in JLabel just wrap your text in <html> tag like: 要在JLabel中显示多行文本,只需将文本包装在<html>标记中,例如:

JLabel label = new JLabel("<html>your long text here ....</html>");

example: 例:

String theReadLineFromTextFile = ....;

theReadLineFromTextFile = "<html>"+theReadLineFromTextFile+"</html>";
JLabel label = new JLabel(theReadLineFromTextFile);
//or 
JLabel label = new JLabel();
label.setText(theReadLineFromTextFile);

如果您无法在gui中执行此操作,则此处的一些答案将有所帮助: 在Java中,将多个字符按字面顺序包装在字符串后

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

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