when i search internet i have found a way to underline font like this,
Font f=jLabel1.getFont();
Map<TextAttribute,Object> map = new Hashtable<TextAttribute,Object>();
map.put(TextAttribute.UNDERLINE,TextAttribute.UNDERLINE_ON);
f=f.derive开发者_StackOverflow中文版Font(map);
jLabel1.setFont(f);
it works well on jdk6, however it doesnt work on jdk5, and it doesnt warn about anything. first, how can i get same effect on jdk5? second, why is there a TextAttribute.UNDERLINE constant, if it doesnt work?
I'm not aware of any way except the somewhat ugly HTML-approach:
label.setText("<html>some <u>underlined</u> text</html>");
Beware however, that if you provide a custom look-and-feel, the HTML-rendering may no longer work as expected.
Another way would be to add a MatteBorder to the label, but that "underlines" the entire label:
label.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
Many bugs have been reported against the deriveFont
methods; see this search. I could not spot one that exactly matched your problem, but you could have better luck. If you find a relevant bug report, there may be a workaround.
I think this code can work in JDK.5
text.setText("0123456789 ABCDEFGHIJKLM NOPQRSTUVWXYZ");
// make 0123456789 appear underlined
obj.underline=true
may work fine.
StyleRange style1 = new StyleRange();
style1.start = 0;
style1.length = 10;
style1.underline = true;
text.setStyleRange(style1);
0123456789 is underlined.
精彩评论