Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

java - HTML in JLabel not showing

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...