HTML in JLabel not showing

前端 未结 2 1634
执念已碎
执念已碎 2020-12-21 05:40
JLabel label = new JLabel(\"Hello world\");

shows nothing.

If I get rid of the tags, it

相关标签:
2条回答
  • 2020-12-21 06:07

    If you want to mix fonts or colors within the text, or if you want formatting such as multiple lines, you can use HTML. HTML formatting can be used in all Swing buttons, menu items, labels, tool tips, and tabbed panes, as well as in components such as trees and tables that use labels to render text.

    Refer this example HtmlDemo.java


    In your example

     JLabel label = new JLabel("<html><body>Hello world</body></html>"); 
    

    you are not applying any formating like <b>bold</b> or <i>italic</i> etc..

    also <body></body> is not required.

    Hope it helps.

    0 讨论(0)
  • 2020-12-21 06:18

    I have tested your JLabel and JEditorPane code in Eclipse using the latest Windows JDK, using several different look-and-feels, but they all work just fine. I know you're using OpenJDK but I can't get hold of a version for Windows. I'm guessing there is a difference in the two implementations.

    Might I suggest wrapping your text inside a paragraph, such as "<html><body><p>Hello world</p></body></html>"?

    0 讨论(0)
提交回复
热议问题