我是编程新手,我想要一些提示。 我有一个程序使用以下方法从文本文件中读取:
BufferedReader reader = new BufferedReader(new FileReader(file));
.....
这有效,但问题是有些行很长,我看不到GUI中的所有文本。如果长度等于一定数量,我该如何告诉Java转到下一行?
示例文字:
A week after Mozart died, his ghost was discovered trying to erase his music. When asked why, it said "I'm decomposing.
我想把它解析为四行,而不是两行。任何帮助,将不胜感激。
答案 0 :(得分:0)
根据您的评论,您正在使用JLabel来显示数据。
在JLabel中显示多行文字,只需将文字换成<html>
标记,如:
JLabel label = new JLabel("<html>your long text here ....</html>");
示例:
String theReadLineFromTextFile = ....;
theReadLineFromTextFile = "<html>"+theReadLineFromTextFile+"</html>";
JLabel label = new JLabel(theReadLineFromTextFile);
//or
JLabel label = new JLabel();
label.setText(theReadLineFromTextFile);
答案 1 :(得分:0)
如果你不能在gui中这样做,那么这里的一些答案会有所帮助:Wrap the string after a number of characters word-wise in Java