如何将 html 放入 Java 中的 JLabel 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6635730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I put html in a JLabel in java?
提问by The Java Man
How do I use html tags in a JLabel in java?
如何在 Java 的 JLabel 中使用 html 标签?
采纳答案by Globmont
To put html in a JLabel
, you would make it look something like this
要将 html 放在 a 中JLabel
,您可以使它看起来像这样
JLabel label = new JLabel("<html><yourTagHere><yourOtherTagHere>this is your text</yourOtherTagHere></yourTagHere></html>");
回答by james_bond
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);
回答by Vivek
There are following ways
有以下几种方式
Using SetText method of JLabel Object
JLabel HTMLlabel = new JLabel().setText("<html><tag>blah blah</tag></html>");
Passing String to JLable class Constructor.
JLabel HTMLlabel = new JLabel("<html><tag>blah blah</tag></html>");
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);
使用 JLabel 对象的 SetText 方法
JLabel HTMLlabel = new JLabel().setText("<html><tag>blah blah</tag></html>");
将字符串传递给 JLable 类构造函数。
JLabel HTMLlabel = new JLabel("<html><tag>blah blah</tag></html>");
使用 String 并将其传递给 JLabel 类构造函数,类似于上面的示例,但使用 String。
String HTMLlabelStr = "<html><tag>blah blah</tag></html>";
JLabel HTMLlabel = new JLabel(HTMLlabelStr);
回答by whirish
This should do the trick:
这应该可以解决问题:
JLabel whatever =
new JLabel("<html><something>Put Stuff Here</something></html>");
回答by user2396528
JLabel myHTMLLabel =new JLabel("<html>");
myHTMLLabel.setText("<html><font color='green'>Hello World</font>");
回答by Malith
Also you can use this with all Swing buttons, menu items, labels, text panes, editor panes, tool tips, tabbed panes etc...
您也可以将它与所有 Swing 按钮、菜单项、标签、文本窗格、编辑器窗格、工具提示、选项卡式窗格等一起使用...
JTextPane pane = new JTextPane();
pane.setContentType("text/html");
pane.setText("<html><h1>My First Heading</h1><p>My first paragraph.</p></body></html>");