Java JLabel 中的换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1090098/
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
Newline in JLabel
提问by mportiz08
How can I display a newline in JLabel
?
如何在 中显示换行符JLabel
?
For example, if I wanted:
例如,如果我想要:
Hello World!
blahblahblah
你好,世界!
等等等等等等
This is what I have right now:
这就是我现在所拥有的:
JLabel l = new JLabel("Hello World!\nblahblahblah", SwingConstants.CENTER);
This is what is displayed:
这是显示的内容:
Hello World!blahblahblah
你好世界!blahblahblah
Forgive me if this is a dumb question, I'm just learning some Swing basics...
如果这是一个愚蠢的问题,请原谅我,我只是在学习一些 Swing 基础知识......
采纳答案by freitass
Surround the string with <html></html>
and break the lines with <br/>
.
用 环绕字符串<html></html>
并用 断开线条<br/>
。
JLabel l = new JLabel("<html>Hello World!<br/>blahblahblah</html>", SwingConstants.CENTER);
回答by Uri
JLabel is actually capable of displaying some rudimentary HTML, which is why it is not responding to your use of the newline character (unlike, say, System.out).
JLabel 实际上能够显示一些基本的 HTML,这就是为什么它不响应您对换行符的使用(不像 System.out)。
If you put in the corresponding HTML and used <BR>
, you would get your newlines.
如果您输入相应的 HTML 并使用<BR>
,您将获得换行符。
回答by Aakash
You can use the MultilineLabel component in the Jide Open Source Components.
您可以使用捷德开源组件中的 MultilineLabel 组件。
回答by nevster
You can do
你可以做
JLabel l = new JLabel("<html><p>Hello World! blah blah blah</p></html>", SwingConstants.CENTER);
and it will automatically wrap it where appropriate.
它会在适当的地方自动包装它。
回答by jidesoft
Thanks Aakash for recommending JIDE MultilineLabel. JIDE's StyledLabel is also enhanced recently to support multiple line. I would recommend it over the MultilineLabel as it has many other great features. You can check out an article on StyledLabel below. It is still free and open source.
感谢 Aakash 推荐 JIDE MultilineLabel。JIDE 的 StyledLabel 最近也得到了增强,以支持多行。我会通过 MultilineLabel 推荐它,因为它有许多其他很棒的功能。您可以在下面查看有关 StyledLabel 的文章。它仍然是免费和开源的。
回答by TheSola10
You can try and do this:
你可以尝试这样做:
myLabel.setText("<html>" + myString.replaceAll("<","<").replaceAll(">", ">").replaceAll("\n", "<br/>") + "</html>")
The advantages of doing this are:
这样做的好处是:
- It replaces all newlines with
<br/>
, without fail. - It automatically replaces eventual
<
and>
with<
and>
respectively, preventing some render havoc.
- 它用 替换所有换行符
<br/>
,没有失败。 - 它自动替换最终
<
并>
用<
和>
分别,防止一些渲染破坏。
What it does is:
它的作用是:
"<html>" +
adds an openinghtml
tag at the beginning.replaceAll("<", "<").replaceAll(">", ">")
escapes<
and>
for convenience.replaceAll("\n", "<br/>")
replaces all newlines bybr
(HTML line break) tags for what you wanted- ... and
+ "</html>"
closes ourhtml
tag at the end.
"<html>" +
html
在开头添加一个开始标签.replaceAll("<", "<").replaceAll(">", ">")
逃生<
和>
方便.replaceAll("\n", "<br/>")
将所有换行符替换为br
(HTML换行符)标记为您想要的- ...并在最后
+ "</html>"
关闭我们的html
标签。
P.S.: I'm very sorry to wake up such an old post, but whatever, you have a reliable snippet for your Java!
PS:我很抱歉唤醒这样一个旧帖子,但无论如何,你有一个可靠的Java片段!