HTML in JLabel not showing

雨燕双飞 提交于 2019-11-28 08:22:22

问题


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

shows nothing.

If I get rid of the tags, it shows plain text (as expected), so the JLabel is definitely being added and shown on the window.

Same for:

JEditorPane jep = new JEditorPane("text/html", "<html><body>Hello world</body></html>");

Any ideas?

I'm using java-6-openjdk with Eclipse. More details:

matt@matt-laptop:~$ java -version
java version "1.6.0_20"
OpenJDK Runtime Environment (IcedTea6 1.9.5) (6b20-1.9.5-0ubuntu1~10.04.1)
OpenJDK Server VM (build 19.0-b09, mixed mode)

matt@matt-laptop:~$ javac -version
javac 1.6.0_24

回答1:


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.




回答2:


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>"?



来源:https://stackoverflow.com/questions/5119157/html-in-jlabel-not-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!