是否有 MultiLine JLabel 选项?
我想在运行时在 JLabel 中显示一些文本。我只是想知道文本是否可以显示在多行上。例如,我希望我的文本以以下格式显示:
Line 1
Line 2
Line 3
String sText = "Line1 \n Line2 \n Line3";
jLabel1.setText (sText);
我尝试了上面的代码,但它不起作用。我做错了什么或者 JLabel 不支持所述功能吗?
如果我无法实现上述功能,如何在运行时在 JPanel 中添加多个标签(每一行一个)?
I want to display some text in JLabel at runtime. I just want to know that is there anyway through which the text is displayed on multiple lines. For example, I want my text to be displayed in following format:
Line 1
Line 2
Line 3
String sText = "Line1 \n Line2 \n Line3";
jLabel1.setText (sText);
I tried the above code but its not working. Am I doing some thing wrong or does JLabel not support said feature?
If I'm unable to achieve the above functionality, how can I add multiple labels (one for each line) in JPanel at runtime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JLabel
支持 HTML。您可以写:编辑:
我添加了带有 br 标记的反斜杠以使代码正常工作
JLabel
supports HTML. You can write:Edit:
I added back slashes with br tag in order to make code working
使用
而不是使用 \n并添加前缀
像这样
use
<br>
instead of using \nand prefix it by
<html>
like this
在这种情况下,对于 HTML 格式的文本,更好的选择是删除硬换行符(段落末尾除外)并使用 CSS 设置 HTML 的宽度。
如第二个示例 (
LabelRenderTest.java
) 所示 这里。A better option for HTML formatted text in this case, is to drop the hard line breaks (except at the end of paragraphs) and set the width of the HTML using CSS.
As seen in the 2nd example (
LabelRenderTest.java
) shown here.