简体   繁体   English

从 JLabel 文本中删除下划线

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM