简体   繁体   English

JLabel的换行符

[英]Newline in JLabel

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基础...

<html></html>包围字符串并用<br/>打破行。

JLabel l = new JLabel("<html>Hello World!<br/>blahblahblah</html>", SwingConstants.CENTER);

You can try and do this: 你可以尝试这样做:

myLabel.setText("<html>" + myString.replaceAll("<","&lt;").replaceAll(">", "&gt;").replaceAll("\n", "<br/>") + "</html>")

The advantages of doing this are: 这样做的好处是:

  • It replaces all newlines with <br/> , without fail. 它取代所有换行符<br/> ,没有失败。
  • It automatically replaces eventual < and > with &lt; 它会自动用&lt;替换最终的<> and &gt; &gt; respectively, preventing some render havoc. 分别防止一些渲染破坏。

What it does is: 它的作用是:

  • "<html>" + adds an opening html tag at the beginning "<html>" +在开头添加一个开头的html标签
  • .replaceAll("<", "&lt;").replaceAll(">", "&gt;") escapes < and > for convenience .replaceAll("<", "&lt;").replaceAll(">", "&gt;")为了方便而逃避<>
  • .replaceAll("\\n", "<br/>") replaces all newlines by br (HTML line break) tags for what you wanted .replaceAll("\\n", "<br/>")br (HTML换行符)标签替换所有换行符。
  • ... and + "</html>" closes our html tag at the end. ...和+ "</html>"关闭我们的html标签。

PS: I'm very sorry to wake up such an old post, but whatever, you have a reliable snippet for your Java! PS:我很遗憾醒来这么老的帖子,但无论如何,你有一个可靠的Java代码片段!

You can use the MultilineLabel component in the Jide Open Source Components. 您可以在Jide开源组件中使用MultilineLabel组件。

http://www.jidesoft.com/products/oss.htm http://www.jidesoft.com/products/oss.htm

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. 它会在适当的时候自动包装。

Thanks Aakash for recommending JIDE MultilineLabel. 感谢Aakash推荐JIDE MultilineLabel。 JIDE's StyledLabel is also enhanced recently to support multiple line. JIDE的StyledLabel最近也得到了增强,以支持多线。 I would recommend it over the MultilineLabel as it has many other great features. 我会推荐它通过MultilineLabel,因为它有许多其他很棒的功能。 You can check out an article on StyledLabel below. 您可以在下面查看有关StyledLabel的文章。 It is still free and open source. 它仍然是免费和开源的。

http://www.jidesoft.com/articles/StyledLabel.pdf http://www.jidesoft.com/articles/StyledLabel.pdf

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> ,您将获得新行。

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

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