When I try to use HTML with `JOptionPane`, HTML tags are printed instead of HTML formatting

瘦欲@ 提交于 2019-12-04 05:17:26
mre

The entire string needs to be enclosed within the HTML tags. For example:

button = new JButton("<html><b><u>T</u>wo</b><br>lines</html>");

For more information, see How to Use HTML in Swing Components.

Also worth noting: it seems that having a newline in your string triggers JOptionPane to not render the string as HTML, whereas e.g. JLabel does allow newlines in the HTML.

RanRag

Enclose your entire string within html tags.

The <HTML> and </HTML> tag specify that the input is in HTML. If you want to use HTML in Swing components, they have to either not be HTML, or be entirely in HTML. You can change the background of text by using the <FONT> tag, It might also be neater to enclose your text in <P> tags, but that's more a question of taste.

Try using

String msg = "<HTML><BODY><P>Please fix <FONT style="BACKGROUND-COLOR: #FFCCCC"> this</FONT></P></BODY></HTML>";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!