java 是否存在任何 MultiLine JLabel?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7447691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:00:59  来源:igfitidea点击:

Is there any MultiLine JLabel exist?

javaswingjlabelmultilinehtml-formatting

提问by Jame

I want to display some text in JLabel at runtime. I just want to know that is there anyway through which the text is dispalyed on multiple line e.g i want my text to be displayed in following format:

我想在运行时在 JLabel 中显示一些文本。我只是想知道无论如何都可以通过它在多行上显示文本,例如我希望我的文本以以下格式显示:

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 doning some thing wrong or JLabel doesn't support above said feature?

我尝试了上面的代码,但它不起作用。是我做错了什么还是 JLabel 不支持上述功能?

In case if i am unable to achieve the above functionality then please guide me how do i add multiple labels (one for each line) in JPanel at runtime

如果我无法实现上述功能,请指导我如何在运行时在 JPanel 中添加多个标签(每行一个)

回答by Chandra Patni

JLabelsupports HTML. You can write:

JLabel支持 HTML。你可以写:

String  sText  = "<html>Line1 <br/> Line2 <br/> Line3</html>";
jLabel1.setText (sText);

Edit: I added back slashes with br tag in order to make code working

编辑:我用 br 标签添加了反斜杠以使代码工作

回答by confucius

use <br>instead of using \n and prefix it by <html>like this

使用<br>而不是使用 \n 并<html>像这样添加前缀

"<html>Line1 <br> Line2 <br> Line3</html>";

回答by Andrew Thompson

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.

在这种情况下,HTML 格式文本的更好选择是删除硬换行符(段落末尾除外)并使用 CSS 设置 HTML 的宽度。

As seen in the 2nd example (LabelRenderTest.java) shown here.

如所看到的在第二实施例(LabelRenderTest.java)中示出在这里

JLabel with multiline, formatted, text.

带有多行、格式化、文本的 JLabel。