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

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

Font.BOLD 粗体字体

Font.ITALIC 斜体字体

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

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

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

TextAttribute.UNDERLINE看起来像感兴趣的TextAttribute

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

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

 Map fontAttributes = new HashMap(); fontAttributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); Font boldUnderline = new Font("Serif",Font.BOLD, 12).deriveFont(fontAttributes); 

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

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

对于SWT,您可以使用:

 StyledText text = new StyledText(shell, SWT.BORDER); text.setText("0123456789 ABCDEFGHIJKLM NOPQRSTUVWXYZ"); // make 0123456789 appear underlined StyleRange style1 = new StyleRange(); style1.start = 0; style1.length = 10; style1.underline = true; text.setStyleRange(style1);