[英]Remove the underline from a JLabel text
I used to underline a JLabel text once clicked this code:单击此代码后,我曾经在 JLabel 文本下划线:
JLabel label = new JLabel("Underlined Label");
Font font = label.getFont();
Map attributes = font.getAttributes();
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
label.setFont(font.deriveFont(attributes));
So now I need to restore it to its first state (without underline) once another JLabel is clicked what shall I change?所以现在我需要将它恢复到它的第一个状态(没有下划线),一旦另一个 JLabel 被点击,我应该改变什么?
Thank you in advanced!先谢谢了!
It turns out that TextAttribute.UNDERLINE_OFF
is not a real constant.事实证明
TextAttribute.UNDERLINE_OFF
不是一个真正的常量。 So I consulted the TextAttribute#UNDERLINE
documentation :所以我查阅了
TextAttribute#UNDERLINE
文档:
public static final TextAttribute UNDERLINE
Attribute key for underline.
下划线的属性键。 Values are instances of
Integer
.值是
Integer
实例。 The default value is-1
, which means no underline.默认值为
-1
,这意味着没有下划线。The constant value
UNDERLINE_ON
is provided.提供了常量值
UNDERLINE_ON
。The underline affects both the visual bounds and the outline of the text.
下划线影响文本的视觉边界和轮廓。
And it turns out the default value is -1
.事实证明,默认值是
-1
。 So to revert the text back to not being underlined just use:因此,要将文本恢复为没有下划线,只需使用:
attributes.put(TextAttribute.UNDERLINE, -1);
The answer attributes.put(TextAttribute.UNDERLINE, -1);
答案
attributes.put(TextAttribute.UNDERLINE, -1);
doesn't work for me.对我不起作用。
But:但是:
label.setFont(new Font(font.getName(), font.getStyle(), font.getSize()));
works!作品!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.