How do I put html in a JLabel in java?

蓝咒 提交于 2019-11-26 17:13:07

问题


How do I use html tags in a JLabel in java?


回答1:


To put html in a JLabel, you would make it look something like this

 JLabel label = new JLabel("<html><yourTagHere><yourOtherTagHere>this is your text</yourOtherTagHere></yourTagHere></html>");



回答2:


This will do the trick:

 String labelText ="<html><FONT COLOR=RED>Red</FONT> and <FONT COLOR=BLUE>Blue</FONT> Text</html>";
 JLabel coloredLabel =new JLabel(labelText);



回答3:


There are following ways

  1. Using SetText method of JLabel Object

    JLabel HTMLlabel = new JLabel().setText("<html><tag>blah blah</tag></html>");

  2. Passing String to JLable class Constructor.

    JLabel HTMLlabel = new JLabel("<html><tag>blah blah</tag></html>");

  3. Using String and passing it to JLabel class Constructor similar to above example but using String.

    String HTMLlabelStr = "<html><tag>blah blah</tag></html>";
    JLabel HTMLlabel = new JLabel(HTMLlabelStr);




回答4:


This should do the trick:

JLabel whatever =
    new JLabel("<html><something>Put Stuff Here</something></html>");



回答5:


Also you can use this with all Swing buttons, menu items, labels, text panes, editor panes, tool tips, tabbed panes etc...

JTextPane pane = new JTextPane();
pane.setContentType("text/html");

pane.setText("<html><h1>My First Heading</h1><p>My first paragraph.</p></body></html>");



回答6:


JLabel myHTMLLabel =new JLabel("<html>");
myHTMLLabel.setText("<html><font color='green'>Hello World</font>");


来源:https://stackoverflow.com/questions/6635730/how-do-i-put-html-in-a-jlabel-in-java

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