Home > Java > JavaBase > body text

How to make fonts display underline in java

Release: 2019-12-02 09:27:21
Original
3996 people have browsed it

How to make fonts display underline in java

In .net, for the Font class, you can directly use constants to generate underlined fonts.

However, in Java, it is a little more complicated to generate an underlined font, and you need to use the TextAttribute class to generate it. (Recommended: java video tutorial)

Please refer to the code snippet below for details.

// 生成带有下划线的字体
// 需要借助于TextAttribute类来处理
HashMap<TextAttribute, Object> hm = new HashMap<TextAttribute, Object>();
hm.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); // 定义是否有下划线
hm.put(TextAttribute.SIZE, 12); // 定义字号
hm.put(TextAttribute.FAMILY, "Simsun"); // 定义字体名
Font font = new Font(hm); // 生成字号为12,字体为宋体,字形带有下划线的字体
Copy after login

TextAttribute instances are used as attribute keys to identify attributes in Font, TextLayout, AttributedCharacterIterator, and other classes that handle text attributes. Other constants defined in this class can be used as property values.

UNDERLINE_ON:

public static final Integer UNDERLINE_ON
Copy after login

Standard underline.

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of How to make fonts display underline in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template