从 JLabel 文本中删除下划线
Remove the underline from a JLabel text
单击此代码后,我曾经在 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);
答案 attributes.put(TextAttribute.UNDERLINE, -1);
不适合我。
但是:
label.setFont(new Font(font.getName(), font.getStyle(), font.getSize()));
有效!
单击此代码后,我曾经在 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);
答案 attributes.put(TextAttribute.UNDERLINE, -1);
不适合我。
但是:
label.setFont(new Font(font.getName(), font.getStyle(), font.getSize()));
有效!