简体   繁体   English

JLabel没有显示长段

[英]JLabel doesn't show a long paragraph

字符串文字

窗口

How can I enable the JLabel to show the whole paragraph? 如何让JLabel显示整个段落?

JLabel label = new JLabel("<html>"+test+"</html>");

The JLabel component is usually used for short pieces of text. JLabel组件通常用于短文本。 If you need to throw in large amounts of texts, you should use the JTextArea . 如果需要输入大量文本,则应使用JTextArea Once you use that, you can then wrap the text using the setLineWrap : 一旦你使用它,你可以使用setLineWrap包装文本:

Sets the line-wrapping policy of the text area. 设置文本区域的换行策略。 If set to true the lines will be wrapped if they are too long to fit within the allocated width. 如果设置为true,则如果行太长而无法放入分配的宽度内,则会对其进行换行。

If you still want to go ahead with the usage of a JLabel (not recommended), setting the size of the label should do the trick for you. 如果您仍想继续使用JLabel(不推荐),设置标签的大小应该适合您。

Put the text in html tags and use br-tags to break the line. 将文本放在html标签中并使用br-tags打破这一行。 Nearly every Swing component does support html text. 几乎每个Swing组件都支持html文本。

EG: 例如:

"<html>" + "a line" + "<br />" + 
"second line" + "</html>";

Try to surround the whole string with an HTML p tag as follows: 尝试使用HTML p标记包围整个字符串,如下所示:

String text = "<html><p>This program........ BIG LINE_HERE....called person.txt</p></html>";
JLabel info = new JLabel(text);

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

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