Java中的下划线字体的常量值是什么?

Was*_*eem 11 java fonts class underline bold

Java中的下划线字体的常量值是什么?

Font.BOLD 粗体字体

Font.ITALIC 斜体字体

什么是UNDERLINE字体常量?我尝试了所有可用的常量,但它没有用.

小智 14

假设您需要带下划线和粗体的Serif样式字体,size = 12.

Map<TextAttribute, Integer> fontAttributes = new HashMap<TextAttribute, Integer>();
fontAttributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
Font boldUnderline = new Font("Serif",Font.BOLD, 12).deriveFont(fontAttributes);
Run Code Online (Sandbox Code Playgroud)

如果您不想加粗,请使用Font.PLAIN而不是Font.BOLD.不要使用Font类的getAttributes()方法.它会给你一个疯狂的通配符参数化类型Map<TextAttribute,?>,你将无法调用put()方法.有时Java会像那样令人讨厌.如果您对原因感兴趣,可以访问以下网站:http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html

  • 有趣的是,如果你试图关闭下划线,你将找不到"UNDERLINE_OFF"常量.但是你可以使用值`-1`来实现.EG:`fontAttributes.put(TextAttribute.UNDERLINE,-1);` (2认同)

coo*_*ird 13

查看Java API规范,看起来Font该类没有用于下划线的常量.

但是,使用Font(Map<? extends AttributedCharacterIterator.Attribute,?> attributes)构造函数,可以为其指定Map包含TextAttribute和要使用的值,以指定字体属性.(注意TextAttribute该类是其子类AttributedCharacterIterator.Attribute)

TextAttribute.UNDERLINE似乎TextAttribute是有趣的.

编辑:有一个使用Java教程TextAttribute中的使用文本属性到样式文本部分的示例.


tva*_*son 3

下划线不是字体的属性,而是文本段的属性。渲染时,文本将以指定的字体渲染,然后在其下方绘制一条线。根据您使用的框架,这可能会使用属性为您完成,或者您可能必须自己完成。