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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/325840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:12:57  来源:igfitidea点击:

What is the Constant Value of the Underline font in Java?

javaclassfontsboldunderline

提问by Waseem

What is the Constant Value of the Underline font in Java ?

Java中下划线字体的常量值是多少?

Font.BOLD boldfont

Font.BOLD 加粗字体

Font.ITALIC italicfont

Font.ITALIC 斜体字体

What is the UNDERLINE font Constant ? I try all the available constants but it didn't work .

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

回答by tvanfosson

Underlining is not a property of the font but of the text segment. When rendered the text is rendered in the font specified then a line is drawn under it. Depending on what framework you are using, this may be done for you using properties or you may have to do it yourself.

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

回答by Markus Lausberg

For SWT you can use:

对于 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);

回答by coobird

Looking at the Java API Specification, it appears that the Fontclassdoes not have a constant for underlining.

查看Java API SpecificationFont该类似乎没有用于下划线的常量。

However, using the Font(Map<? extends AttributedCharacterIterator.Attribute,?> attributes)constructor, one can give it a Mapcontaining the TextAttributeand the value to use, in order to specify the font attributes. (Note that the TextAttributeclass is a subclass of AttributedCharacterIterator.Attribute)

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

TextAttribute.UNDERLINEseems like the TextAttributeof interest.

TextAttribute.UNDERLINE似乎很TextAttribute有趣。

Edit:There's an example of using TextAttributein the Using Text Attributes to Style Textsection from The Java Tutorials.

编辑:The Java TutorialsTextAttributeUsing Text Attributes to Style Text部分中有一个使用示例

回答by Blake

Suppose you wanted a underlined and bolded Serif style font, size=12.

假设您想要一个带下划线和粗体的 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);

If you don't want it bolded, use Font.PLAIN instead of Font.BOLD. Don't use the getAttributes() method of the Font class. It will give you a crazy wildcard parameterized type Map<TextAttribute,?>, and you won't be able to invoke the put() method. Sometimes Java can be yucky like that. If you're interested in why, you can check out this site: http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html

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