单击此代码后,我曾经在 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));
所以现在我需要在单击另一个 JLabel 后将它恢复到它的第一个状态(没有下划线)我应该更改什么?
先谢谢你!
最佳答案
事实证明 TextAttribute.UNDERLINE_OFF
不是一个真正的常量。所以我咨询了TextAttribute#UNDERLINE
documentation :
public static final TextAttribute UNDERLINE
Attribute key for underline. Values are instances of
Integer
. The default value is-1
, which means no underline.The constant value
UNDERLINE_ON
is provided.The underline affects both the visual bounds and the outline of the text.
事实证明默认值是-1
。因此,要将文本恢复为不带下划线的状态,只需使用:
attributes.put(TextAttribute.UNDERLINE, -1);
关于java - 从 JLabel 文本中删除下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38784076/