JavaAwtSwing笔记之 字体 java.awt.Font

前言

java.awt.Font 可以设置字体的种类, 样式((普通, 粗体, 斜体)的组合), 大小
java.awt.Font 简单方式不能设置颜色
设置字体颜色可以用 component.setForeground(Color color),或者用TextAttribute类来构建Font

类声明

public class Font implements java.io.Serializable {

构造函数

public Font(String name, int style, int size) {

    public Font(String name, int style, int size) {
        this.name = (name != null) ? name : "Default";
        this.style = (style & ~0x03) == 0 ? style : 0;
        this.size = size;
        this.pointSize = size;
    }

public Font(Map<? extends Attribute, ?> attributes) {

    public Font(Map<? extends Attribute, ?> attributes) {
        initFromValues(AttributeValues.fromMap(attributes, RECOGNIZED_MASK));
    }

实例化方法

Font font = new (字体种类名称 , 普粗斜的组合 , 大小 );

例如 new Font(“宋体” , Font.ITALIC , 25);

参数1 : String 字体种类名称

查看支持的本地字体列表
import java.awt.*;
public class 查看支持的本地字体Font {
	public static void main(String...arguments) {
		GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
		Font fonts[] = gEnv.getAllFonts();
		for(Font font : fonts)System.out.println(font);
	}
}
java.awt.Font[family=Arial,name=Arial,style=plain,size=1]
java.awt.Font[family=Arial Black,name=Arial Black,style=plain,size=1]
java.awt.Font[family=Arial,name=Arial Bold,style=plain,size=1]
java.awt.Font[family=Arial,name=Arial Bold Italic,style=plain,size=1]
java.awt.Font[family=Arial,name=Arial Italic,style=plain,size=1]
java.awt.Font[family=Bahnschrift,name=Bahnschrift,style=plain,size=1]
java.awt.Font[family=Dialog,name=Bahnschrift Bold,style=plain,size=1]
java.awt.Font[family=Dialog,name=Bahnschrift Bold Condensed,style=plain,size=1]
java.awt.Font[family=Dialog,name=Bahnschrift Bold SemiCondensed,style=plain,size=1]
java.awt.Font[family=Dialog,name=Bahnschrift Condensed,style=plain,size=1]
java.awt.Font[family=Dialog,name=Bahnschrift Light,style=plain,size=1]
java.awt.Font[family=Dialog,name=Bahnschrift Light Condensed,style=plain,size=1]
java.awt.Font[family=Dialog,name=Bahnschrift Light SemiCondensed,style=plain,size=1]
java.awt.Font[family=Dialog,name=Bahnschrift Regular,style=plain,size=1]
java.awt.Font[family=Dialog,name=Bahnschrift SemiBold,style=plain,size=1]
java.awt.Font[family=Dialog,name=Bahnschrift SemiBold Condensed,style=plain,size=1]
java.awt.Font[family=Dialog,name=Bahnschrift SemiBold SemiCondensed,style=plain,size=1]
java.awt.Font[family=Dialog,name=Bahnschrift SemiCondensed,style=plain,size=1]
java.awt.Font[family=Dialog,name=Bahnschrift SemiLight,style=plain,size=1]
java.awt.Font[family=Dialog,name=Bahnschrift SemiLight Condensed,style=plain,size=1]
java.awt.Font[family=Dialog,name=Bahnschrift SemiLight SemiCondensed,style=plain,size=1]
java.awt.Font[family=Calibri,name=Calibri,style=plain,size=1]
java.awt.Font[family=Calibri,name=Calibri Bold,style=plain,size=1]
java.awt.Font[family=Calibri,name=Calibri Bold Italic,style=plain,size=1]
java.awt.Font[family=Calibri,name=Calibri Italic,style=plain,size=1]
java.awt.Font[family=Calibri Light,name=Calibri Light,style=plain,size=1]
java.awt.Font[family=Calibri Light,name=Calibri Light Italic,style=plain,size=1]
java.awt.Font[family=Cambria,name=Cambria,style=plain,size=1]
java.awt.Font[family=Cambria,name=Cambria Bold,style=plain,size=1]
java.awt.Font[family=Cambria,name=Cambria Bold Italic,style=plain,size=1]
java.awt.Font[family=Cambria,name=Cambria Italic,style=plain,size=1]
java.awt.Font[family=Cambria Math,name=Cambria Math,style=plain,size=1]
java.awt.Font[family=Candara,name=Candara,style=plain,size=1]
java.awt.Font[family=Candara,name=Candara Bold,style=plain,size=1]
java.awt.Font[family=Candara,name=Candara Bold Italic,style=plain,size=1]
java.awt.Font[family=Candara,name=Candara Italic,style=plain,size=1]
java.awt.Font[family=Candara Light,name=Candara Light,style=plain,size=1]
java.awt.Font[family=Candara Light,name=Candara Light Italic,style=plain,size=1]
java.awt.Font[family=Comic Sans MS,name=Comic Sans MS,style=plain,size=1]
java.awt.Font[family=Comic Sans MS,name=Comic Sans MS Bold,style=plain,size=1]
java.awt.Font[family=Comic Sans MS,name=Comic Sans MS Bold Italic,style=plain,size=1]
java.awt.Font[family=Comic Sans MS,name=Comic Sans MS Italic,style=plain,size=1]
java.awt.Font[family=Consolas,name=Consolas,style=plain,size=1]
java.awt.Font[family=Consolas,name=Consolas Bold,style=plain,size=1]
java.awt.Font[family=Consolas,name=Consolas Bold Italic,style=plain,size=1]
java.awt.Font[family=Consolas,name=Consolas Italic,style=plain,size=1]
java.awt.Font[family=Constantia,name=Constantia,style=plain,size=1]
java.awt.Font[family=Constantia,name=Constantia Bold,style=plain,size=1]
java.awt.Font[family=Constantia,name=Constantia Bold Italic,style=plain,size=1]
java.awt.Font[family=Constantia,name=Constantia Italic,style=plain,size=1]
java.awt.Font[family=Corbel,name=Corbel,style=plain,size=1]
java.awt.Font[family=Corbel,name=Corbel Bold,style=plain,size=1]
java.awt.Font[family=Corbel,name=Corbel Bold Italic,style=plain,size=1]
java.awt.Font[family=Corbel,name=Corbel Italic,style=plain,size=1]
java.awt.Font[family=Corbel Light,name=Corbel Light,style=plain,size=1]
java.awt.Font[family=Corbel Light,name=Corbel Light Italic,style=plain,size=1]
java.awt.Font[family=Courier New,name=Courier New,style=plain,size=1]
java.awt.Font[family=Courier New,name=Courier New Bold,style=plain,size=1]
java.awt.Font[family=Courier New,name=Courier New Bold Italic,style=plain,size=1]
java.awt.Font[family=Courier New,name=Courier New Italic,style=plain,size=1]
java.awt.Font[family=Dialog,name=Dialog.bold,style=plain,size=1]
java.awt.Font[family=Dialog,name=Dialog.bolditalic,style=plain,size=1]
java.awt.Font[family=Dialog,name=Dialog.italic,style=plain,size=1]
java.awt.Font[family=Dialog,name=Dialog.plain,style=plain,size=1]
java.awt.Font[family=DialogInput,name=DialogInput.bold,style=plain,size=1]
java.awt.Font[family=DialogInput,name=DialogInput.bolditalic,style=plain,size=1]
java.awt.Font[family=DialogInput,name=DialogInput.italic,style=plain,size=1]
java.awt.Font[family=DialogInput,name=DialogInput.plain,style=plain,size=1]
java.awt.Font[family=Ebrima,name=Ebrima,style=plain,size=1]
java.awt.Font[family=Ebrima,name=Ebrima Bold,style=plain,size=1]
java.awt.Font[family=Franklin Gothic Medium,name=Franklin Gothic Medium,style=plain,size=1]
java.awt.Font[family=Franklin Gothic Medium,name=Franklin Gothic Medium Italic,style=plain,size=1]
java.awt.Font[family=Gabriola,name=Gabriola,style=plain,size=1]
java.awt.Font[family=Gadugi,name=Gadugi,style=plain,size=1]
java.awt.Font[family=Gadugi,name=Gadugi Bold,style=plain,size=1]
java.awt.Font[family=Georgia,name=Georgia,style=plain,size=1]
java.awt.Font[family=Georgia,name=Georgia Bold,style=plain,size=1]
java.awt.Font[family=Georgia,name=Georgia Bold Italic,style=plain,size=1]
java.awt.Font[family=Georgia,name=Georgia Italic,style=plain,size=1]
java.awt.Font[family=HoloLens MDL2 Assets,name=HoloLens MDL2 Assets,style=plain,size=1]
java.awt.Font[family=Impact,name=Impact,style=plain,size=1]
java.awt.Font[family=Ink Free,name=Ink Free,style=plain,size=1]
java.awt.Font[family=Javanese Text,name=Javanese Text,style=plain,size=1]
java.awt.Font[family=Leelawadee UI,name=Leelawadee UI,style=plain,size=1]
java.awt.Font[family=Leelawadee UI,name=Leelawadee UI Bold,style=plain,size=1]
java.awt.Font[family=Leelawadee UI Semilight,name=Leelawadee UI Semilight,style=plain,size=1]
java.awt.Font[family=Lucida Console,name=Lucida Console,style=plain,size=1]
java.awt.Font[family=Lucida Sans Unicode,name=Lucida Sans Unicode,style=plain,size=1]
java.awt.Font[family=MS Gothic,name=MS Gothic,style=plain,size=1]
java.awt.Font[family=MS PGothic,name=MS PGothic,style=plain,size=1]
java.awt.Font[family=MS UI Gothic,name=MS UI Gothic,style=plain,size=1]
java.awt.Font[family=MT Extra,name=MT Extra,style=plain,size=1]
java.awt.Font[family=MV Boli,name=MV Boli,style=plain,size=1]
java.awt.Font[family=Malgun Gothic,name=Malgun Gothic,style=plain,size=1]
java.awt.Font[family=Malgun Gothic,name=Malgun Gothic Bold,style=plain,size=1]
java.awt.Font[family=Malgun Gothic Semilight,name=Malgun Gothic Semilight,style=plain,size=1]
java.awt.Font[family=Marlett,name=Marlett,style=plain,size=1]
java.awt.Font[family=Microsoft Himalaya,name=Microsoft Himalaya,style=plain,size=1]
java.awt.Font[family=微軟正黑體,name=Microsoft JhengHei,style=plain,size=1]
java.awt.Font[family=微軟正黑體,name=Microsoft JhengHei Bold,style=plain,size=1]
java.awt.Font[family=微軟正黑體 Light,name=Microsoft JhengHei Light,style=plain,size=1]
java.awt.Font[family=Microsoft JhengHei UI,name=Microsoft JhengHei UI,style=plain,size=1]
java.awt.Font[family=Microsoft JhengHei UI,name=Microsoft JhengHei UI Bold,style=plain,size=1]
java.awt.Font[family=Microsoft JhengHei UI Light,name=Microsoft JhengHei UI Light,style=plain,size=1]
java.awt.Font[family=Microsoft New Tai Lue,name=Microsoft New Tai Lue,style=plain,size=1]
java.awt.Font[family=Microsoft New Tai Lue,name=Microsoft New Tai Lue Bold,style=plain,size=1]
java.awt.Font[family=Microsoft PhagsPa,name=Microsoft PhagsPa,style=plain,size=1]
java.awt.Font[family=Microsoft PhagsPa,name=Microsoft PhagsPa Bold,style=plain,size=1]
java.awt.Font[family=Microsoft Sans Serif,name=Microsoft Sans Serif,style=plain,size=1]
java.awt.Font[family=Microsoft Tai Le,name=Microsoft Tai Le,style=plain,size=1]
java.awt.Font[family=Microsoft Tai Le,name=Microsoft Tai Le Bold,style=plain,size=1]
java.awt.Font[family=Microsoft YaHei UI Light,name=Microsoft YaHei UI Light,style=plain,size=1]
java.awt.Font[family=Microsoft YaHei UI,name=Microsoft Yahei UI,style=plain,size=1]
java.awt.Font[family=Microsoft YaHei UI,name=Microsoft Yahei UI Bold,style=plain,size=1]
java.awt.Font[family=Microsoft Yi Baiti,name=Microsoft Yi Baiti,style=plain,size=1]
java.awt.Font[family=細明體-ExtB,name=MingLiU-ExtB,style=plain,size=1]
java.awt.Font[family=細明體_HKSCS-ExtB,name=MingLiU_HKSCS-ExtB,style=plain,size=1]
java.awt.Font[family=Mongolian Baiti,name=Mongolian Baiti,style=plain,size=1]
java.awt.Font[family=Monospaced,name=Monospaced.bold,style=plain,size=1]
java.awt.Font[family=Monospaced,name=Monospaced.bolditalic,style=plain,size=1]
java.awt.Font[family=Monospaced,name=Monospaced.italic,style=plain,size=1]
java.awt.Font[family=Monospaced,name=Monospaced.plain,style=plain,size=1]
java.awt.Font[family=Myanmar Text,name=Myanmar Text,style=plain,size=1]
java.awt.Font[family=Myanmar Text,name=Myanmar Text Bold,style=plain,size=1]
java.awt.Font[family=新宋体,name=NSimSun,style=plain,size=1]
java.awt.Font[family=Nirmala UI,name=Nirmala UI,style=plain,size=1]
java.awt.Font[family=Nirmala UI,name=Nirmala UI Bold,style=plain,size=1]
java.awt.Font[family=Nirmala UI Semilight,name=Nirmala UI Semilight,style=plain,size=1]
java.awt.Font[family=新細明體-ExtB,name=PMingLiU-ExtB,style=plain,size=1]
java.awt.Font[family=Palatino Linotype,name=Palatino Linotype,style=plain,size=1]
java.awt.Font[family=Palatino Linotype,name=Palatino Linotype Bold,style=plain,size=1]
java.awt.Font[family=Palatino Linotype,name=Palatino Linotype Bold Italic,style=plain,size=1]
java.awt.Font[family=Palatino Linotype,name=Palatino Linotype Italic,style=plain,size=1]
java.awt.Font[family=SansSerif,name=SansSerif.bold,style=plain,size=1]
java.awt.Font[family=SansSerif,name=SansSerif.bolditalic,style=plain,size=1]
java.awt.Font[family=SansSerif,name=SansSerif.italic,style=plain,size=1]
java.awt.Font[family=SansSerif,name=SansSerif.plain,style=plain,size=1]
java.awt.Font[family=Segoe MDL2 Assets,name=Segoe MDL2 Assets,style=plain,size=1]
java.awt.Font[family=Segoe Print,name=Segoe Print,style=plain,size=1]
java.awt.Font[family=Segoe Print,name=Segoe Print Bold,style=plain,size=1]
java.awt.Font[family=Segoe Script,name=Segoe Script,style=plain,size=1]
java.awt.Font[family=Segoe Script,name=Segoe Script Bold,style=plain,size=1]
java.awt.Font[family=Segoe UI,name=Segoe UI,style=plain,size=1]
java.awt.Font[family=Segoe UI Black,name=Segoe UI Black,style=plain,size=1]
java.awt.Font[family=Segoe UI Black,name=Segoe UI Black Italic,style=plain,size=1]
java.awt.Font[family=Segoe UI,name=Segoe UI Bold,style=plain,size=1]
java.awt.Font[family=Segoe UI,name=Segoe UI Bold Italic,style=plain,size=1]
java.awt.Font[family=Segoe UI Emoji,name=Segoe UI Emoji,style=plain,size=1]
java.awt.Font[family=Segoe UI Historic,name=Segoe UI Historic,style=plain,size=1]
java.awt.Font[family=Segoe UI,name=Segoe UI Italic,style=plain,size=1]
java.awt.Font[family=Segoe UI Light,name=Segoe UI Light,style=plain,size=1]
java.awt.Font[family=Segoe UI Light,name=Segoe UI Light Italic,style=plain,size=1]
java.awt.Font[family=Segoe UI Semibold,name=Segoe UI Semibold,style=plain,size=1]
java.awt.Font[family=Segoe UI Semibold,name=Segoe UI Semibold Italic,style=plain,size=1]
java.awt.Font[family=Segoe UI Semilight,name=Segoe UI Semilight,style=plain,size=1]
java.awt.Font[family=Segoe UI Semilight,name=Segoe UI Semilight Italic,style=plain,size=1]
java.awt.Font[family=Segoe UI Symbol,name=Segoe UI Symbol,style=plain,size=1]
java.awt.Font[family=Serif,name=Serif.bold,style=plain,size=1]
java.awt.Font[family=Serif,name=Serif.bolditalic,style=plain,size=1]
java.awt.Font[family=Serif,name=Serif.italic,style=plain,size=1]
java.awt.Font[family=Serif,name=Serif.plain,style=plain,size=1]
java.awt.Font[family=宋体,name=SimSun,style=plain,size=1]
java.awt.Font[family=SimSun-ExtB,name=SimSun-ExtB,style=plain,size=1]
java.awt.Font[family=Sitka Banner,name=Sitka Banner,style=plain,size=1]
java.awt.Font[family=Sitka Banner,name=Sitka Banner Bold,style=plain,size=1]
java.awt.Font[family=Sitka Banner,name=Sitka Banner Bold Italic,style=plain,size=1]
java.awt.Font[family=Sitka Banner,name=Sitka Banner Italic,style=plain,size=1]
java.awt.Font[family=Sitka Display,name=Sitka Display,style=plain,size=1]
java.awt.Font[family=Sitka Display,name=Sitka Display Bold,style=plain,size=1]
java.awt.Font[family=Sitka Display,name=Sitka Display Bold Italic,style=plain,size=1]
java.awt.Font[family=Sitka Display,name=Sitka Display Italic,style=plain,size=1]
java.awt.Font[family=Sitka Heading,name=Sitka Heading,style=plain,size=1]
java.awt.Font[family=Sitka Heading,name=Sitka Heading Bold,style=plain,size=1]
java.awt.Font[family=Sitka Heading,name=Sitka Heading Bold Italic,style=plain,size=1]
java.awt.Font[family=Sitka Heading,name=Sitka Heading Italic,style=plain,size=1]
java.awt.Font[family=Sitka Small,name=Sitka Small,style=plain,size=1]
java.awt.Font[family=Sitka Small,name=Sitka Small Bold,style=plain,size=1]
java.awt.Font[family=Sitka Small,name=Sitka Small Bold Italic,style=plain,size=1]
java.awt.Font[family=Sitka Small,name=Sitka Small Italic,style=plain,size=1]
java.awt.Font[family=Sitka Subheading,name=Sitka Subheading,style=plain,size=1]
java.awt.Font[family=Sitka Subheading,name=Sitka Subheading Bold,style=plain,size=1]
java.awt.Font[family=Sitka Subheading,name=Sitka Subheading Bold Italic,style=plain,size=1]
java.awt.Font[family=Sitka Subheading,name=Sitka Subheading Italic,style=plain,size=1]
java.awt.Font[family=Sitka Text,name=Sitka Text,style=plain,size=1]
java.awt.Font[family=Sitka Text,name=Sitka Text Bold,style=plain,size=1]
java.awt.Font[family=Sitka Text,name=Sitka Text Bold Italic,style=plain,size=1]
java.awt.Font[family=Sitka Text,name=Sitka Text Italic,style=plain,size=1]
java.awt.Font[family=Sylfaen,name=Sylfaen,style=plain,size=1]
java.awt.Font[family=Symbol,name=Symbol,style=plain,size=1]
java.awt.Font[family=Tahoma,name=Tahoma,style=plain,size=1]
java.awt.Font[family=Tahoma,name=Tahoma Bold,style=plain,size=1]
java.awt.Font[family=Times New Roman,name=Times New Roman,style=plain,size=1]
java.awt.Font[family=Times New Roman,name=Times New Roman Bold,style=plain,size=1]
java.awt.Font[family=Times New Roman,name=Times New Roman Bold Italic,style=plain,size=1]
java.awt.Font[family=Times New Roman,name=Times New Roman Italic,style=plain,size=1]
java.awt.Font[family=Trebuchet MS,name=Trebuchet MS,style=plain,size=1]
java.awt.Font[family=Trebuchet MS,name=Trebuchet MS Bold,style=plain,size=1]
java.awt.Font[family=Trebuchet MS,name=Trebuchet MS Bold Italic,style=plain,size=1]
java.awt.Font[family=Trebuchet MS,name=Trebuchet MS Italic,style=plain,size=1]
java.awt.Font[family=Verdana,name=Verdana,style=plain,size=1]
java.awt.Font[family=Verdana,name=Verdana Bold,style=plain,size=1]
java.awt.Font[family=Verdana,name=Verdana Bold Italic,style=plain,size=1]
java.awt.Font[family=Verdana,name=Verdana Italic,style=plain,size=1]
java.awt.Font[family=Webdings,name=Webdings,style=plain,size=1]
java.awt.Font[family=Wingdings,name=Wingdings,style=plain,size=1]
java.awt.Font[family=Yu Gothic,name=Yu Gothic Bold,style=plain,size=1]
java.awt.Font[family=Yu Gothic Light,name=Yu Gothic Light,style=plain,size=1]
java.awt.Font[family=Yu Gothic Medium,name=Yu Gothic Medium,style=plain,size=1]
java.awt.Font[family=Yu Gothic,name=Yu Gothic Regular,style=plain,size=1]
java.awt.Font[family=Yu Gothic UI,name=Yu Gothic UI Bold,style=plain,size=1]
java.awt.Font[family=Yu Gothic UI Light,name=Yu Gothic UI Light,style=plain,size=1]
java.awt.Font[family=Yu Gothic UI,name=Yu Gothic UI Regular,style=plain,size=1]
java.awt.Font[family=Yu Gothic UI Semibold,name=Yu Gothic UI Semibold,style=plain,size=1]
java.awt.Font[family=Yu Gothic UI Semilight,name=Yu Gothic UI Semilight,style=plain,size=1]
java.awt.Font[family=仿宋,name=仿宋,style=plain,size=1]
java.awt.Font[family=宋体,name=宋体,style=plain,size=1]
java.awt.Font[family=微软雅黑,name=微软雅黑,style=plain,size=1]
java.awt.Font[family=微软雅黑,name=微软雅黑 Bold,style=plain,size=1]
java.awt.Font[family=微软雅黑 Light,name=微软雅黑 Light,style=plain,size=1]
java.awt.Font[family=新宋体,name=新宋体,style=plain,size=1]
java.awt.Font[family=楷体,name=楷体,style=plain,size=1]
java.awt.Font[family=等线,name=等线,style=plain,size=1]
java.awt.Font[family=等线,name=等线 Bold,style=plain,size=1]
java.awt.Font[family=等线 Light,name=等线 Light,style=plain,size=1]
java.awt.Font[family=黑体,name=黑体,style=plain,size=1]
查看支持的本体字体名称
import java.awt.GraphicsEnvironment;
import java.util.stream.Stream;
public class 查看支持的本地字体名称 {
	public static void main(String...arguments) {
		GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
		final String AvailableFontFamilyNames[] = gEnv.getAvailableFontFamilyNames();
		Stream.of(AvailableFontFamilyNames).forEach(System.out::println);
	}
}
Arial
Arial Black
Bahnschrift
Calibri
Calibri Light
Cambria
Cambria Math
Candara
Candara Light
Comic Sans MS
Consolas
Constantia
Corbel
Corbel Light
Courier New
Dialog
DialogInput
Ebrima
Franklin Gothic Medium
Gabriola
Gadugi
Georgia
HoloLens MDL2 Assets
Impact
Ink Free
Javanese Text
Leelawadee UI
Leelawadee UI Semilight
Lucida Console
Lucida Sans Unicode
Malgun Gothic
Malgun Gothic Semilight
Marlett
Microsoft Himalaya
Microsoft JhengHei
Microsoft JhengHei Light
Microsoft JhengHei UI
Microsoft JhengHei UI Light
Microsoft New Tai Lue
Microsoft PhagsPa
Microsoft Sans Serif
Microsoft Tai Le
Microsoft YaHei UI
Microsoft YaHei UI Light
Microsoft Yi Baiti
MingLiU-ExtB
MingLiU_HKSCS-ExtB
Mongolian Baiti
Monospaced
MS Gothic
MS PGothic
MS UI Gothic
MT Extra
MV Boli
Myanmar Text
Nirmala UI
Nirmala UI Semilight
Palatino Linotype
PMingLiU-ExtB
SansSerif
Segoe MDL2 Assets
Segoe Print
Segoe Script
Segoe UI
Segoe UI Black
Segoe UI Emoji
Segoe UI Historic
Segoe UI Light
Segoe UI Semibold
Segoe UI Semilight
Segoe UI Symbol
Serif
SimSun-ExtB
Sitka Banner
Sitka Display
Sitka Heading
Sitka Small
Sitka Subheading
Sitka Text
Sylfaen
Symbol
Tahoma
Times New Roman
Trebuchet MS
Verdana
Webdings
Wingdings
Yu Gothic
Yu Gothic Light
Yu Gothic Medium
Yu Gothic UI
Yu Gothic UI Light
Yu Gothic UI Semibold
Yu Gothic UI Semilight
仿宋
宋体
微软雅黑
微软雅黑 Light
新宋体
楷体
等线
等线 Light
黑体
查看本地所支持的字体效果
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class 查看本地所支持的字体效果 {
	static Frame frame = new Frame(Thread.currentThread().getStackTrace()[1].getClassName());
	static {
		frame.addWindowListener(new WindowAdapter() {@Override public void windowClosing(WindowEvent e) {frame.dispose();System.exit(0);}});
		frame.setLayout(new GridLayout(0,5,10,10)); frame.setBounds(100,50,1600,900);
		
		GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
		final String AvailableFontFamilyNames[] = gEnv.getAvailableFontFamilyNames();
		for(String fontFamilyName : AvailableFontFamilyNames) {
			Font font = new Font(fontFamilyName, 1, 30);
			JLabel jLabel = new JLabel(font.getFamily()); jLabel.setFont(font); jLabel.setBackground(new Color(168*256+255)); jLabel.setOpaque(true);
			frame.add(jLabel);
		}
		frame.setVisible(true);
	}
	public  static  void main(String...arguments) {}
}

在这里插入图片描述

查看Font静态属性自带的字体效果 , java1.6增加

Font的静态属性带了5中字体family的选择

  • Font.DIALOG
  • Font.DIALOG_INPUT
  • Font.MONOSPACED
  • Font.SANS_SERIF
  • Font.SERIF

源码👇

/*
     * Constants to be used for logical font family names.
     */

    /**
     * A String constant for the canonical family name of the
     * logical font "Dialog". It is useful in Font construction
     * to provide compile-time verification of the name.
     * @since 1.6
     */
    public static final String DIALOG = "Dialog";

    /**
     * A String constant for the canonical family name of the
     * logical font "DialogInput". It is useful in Font construction
     * to provide compile-time verification of the name.
     * @since 1.6
     */
    public static final String DIALOG_INPUT = "DialogInput";

    /**
     * A String constant for the canonical family name of the
     * logical font "SansSerif". It is useful in Font construction
     * to provide compile-time verification of the name.
     * @since 1.6
     */
    public static final String SANS_SERIF = "SansSerif";

    /**
     * A String constant for the canonical family name of the
     * logical font "Serif". It is useful in Font construction
     * to provide compile-time verification of the name.
     * @since 1.6
     */
    public static final String SERIF = "Serif";

    /**
     * A String constant for the canonical family name of the
     * logical font "Monospaced". It is useful in Font construction
     * to provide compile-time verification of the name.
     * @since 1.6
     */
    public static final String MONOSPACED = "Monospaced";

测试代码👇

package awt;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class Font静态属性所带的字体测试2205301901 {
	
	static Frame frame = new Frame(Thread.currentThread().getStackTrace()[1].getClassName());
	static {
		frame.addWindowListener(new WindowAdapter() {@Override public void windowClosing(WindowEvent ev) {frame.dispose();System.exit(0);}});
		frame.setBounds(new Rectangle(100, 50, 1600, 900));
		frame.setLayout(new GridLayout(0, 1, 10, 10));
		
	}
	
	static void demoJlabelByFont(Font font, String str) {
		JLabel jLabel = new JLabel(str+"    汉字, English"); jLabel.setFont(font);
		jLabel.setBorder(new LineBorder(Color.BLUE, 3, true));
		jLabel.setHorizontalAlignment(JLabel.CENTER);
		frame.add(jLabel);
	}
	
	public static  void main(String...arguments) {
		Font font = null;
		font = new Font(Font.DIALOG, Font.PLAIN, 30); demoJlabelByFont(font, "font = new Font(Font.DIALOG, Font.PLAIN, 30);");
		font = new Font(Font.DIALOG_INPUT, Font.PLAIN, 30); demoJlabelByFont(font, "font = new Font(Font.DIALOG_INPUT, Font.PLAIN, 30);");
		font = new Font(Font.MONOSPACED, Font.PLAIN, 30); demoJlabelByFont(font, "font = new Font(Font.MONOSPACED, Font.PLAIN, 30);");
		font = new Font(Font.SANS_SERIF, Font.PLAIN, 30); demoJlabelByFont(font, "font = new Font(Font.SANS_SERIF, Font.PLAIN, 30);");
		font = new Font(Font.SERIF, Font.PLAIN, 30); demoJlabelByFont(font, "font = new Font(Font.SERIF, Font.PLAIN, 30);");
		
		frame.setVisible(true);
	}

}

在这里插入图片描述

参数2 : int 字体Style , 只有三种选项 , 普粗斜 , 可以组合使用

普0粗1斜2
    /**
     * The plain style constant.
     */
    public static final int PLAIN       = 0;

    /**
     * The bold style constant.  This can be combined with the other style
     * constants (except PLAIN) for mixed styles.
     */
    public static final int BOLD        = 1;

    /**
     * The italicized style constant.  This can be combined with the other
     * style constants (except PLAIN) for mixed styles.
     */
    public static final int ITALIC      = 2;
示例
Font font1 = new Font("宋体", Font.PLAIN, 30);
Font font2 = new Font("宋体", Font.BOLD, 30);
Font font3 = new Font("宋体", Font.ITALIC, 30);
Font font4 = new Font(null, Font.BOLD | Font.ITALIC, 30);
Font font5 = new Font(null, 1|2, 30);
Font font6 = new Font(null, Font.ITALIC | Font.PLAIN, 30);
Font font7 = new Font(null, 2|0, 30);

参数3 : int 字体大小 , 一个整数

示例

import java.awt.*;
import java.awt.event.*;
public class FontStyle普粗斜012 {
	static Frame frame  = new Frame("FontStyle普粗斜012");
	static {
		frame.addWindowListener(new WindowAdapter() {@Override public void windowClosing(WindowEvent ev) {frame.dispose();System.exit(0);}});
		frame.setBounds(100,50,1600,900); frame.setLayout(new GridLayout(0, 1, 10, 10));
	}
	static Font font;
	static Label label;
	public static void main(String...arguments) {
		font=new Font(null, Font.PLAIN, 30); label=new Label("new Font(null, Font.PLAIN, 30)"); label.setFont(font); frame.add(label);
		font=new Font(null, Font.BOLD, 30); label=new Label("new Font(null, Font.BOLD, 30)"); label.setFont(font); frame.add(label);
		font=new Font(null, Font.ITALIC, 30); label=new Label("new Font(null, Font.ITALIC, 30)"); label.setFont(font); frame.add(label);
		font=new Font(null, 0, 30); label=new Label("new Font(null, 0, 30)"); label.setFont(font); frame.add(label);
		font=new Font(null, 1, 30); label=new Label("new Font(null, 1, 30)"); label.setFont(font); frame.add(label);
		font=new Font(null, 2, 30); label=new Label("new Font(null, 2, 30)"); label.setFont(font); frame.add(label);
		
		font=new Font(null, Font.PLAIN | Font.BOLD, 30); label=new Label("new Font(null, Font.PLAIN | Font.BOLD, 30)"); label.setFont(font); frame.add(label);
		font=new Font(null, Font.BOLD | Font.ITALIC, 30); label=new Label("new Font(null, Font.BOLD | Font.ITALIC, 30)"); label.setFont(font); frame.add(label);
		font=new Font(null, Font.ITALIC | Font.PLAIN, 30); label=new Label("new Font(null, Font.ITALIC | Font.PLAIN, 30)"); label.setFont(font); frame.add(label);
		font=new Font(null, Font.PLAIN | Font.BOLD | Font.ITALIC, 30); label=new Label("new Font(null, Font.PLAIN | Font.BOLD | Font.ITALIC, 30)"); label.setFont(font); frame.add(label);
		
		for(int i=0; i<frame.getComponentCount(); i++) 	frame.getComponent(i).setBackground(new Color(Integer.valueOf("0099ff", 16)));
		frame.setVisible(true);
	}
}

在这里插入图片描述



Font.getFont(Map<? extends Attribute, ?> attributes) 或 new Font(Map<? extends Attribute, ?> attributes)

这两个方法是通过 TextAttribute来配置Font, 先将多个TextAttribute属性放到Map中

TextAttribute注释中的Summary of attributes
Key, value type, principal constants, and default value behavior of all TextAttributes
Key Value Type Principal Constants Default Value
TextAttribute.FAMILY String See Font java.awt.Font.DIALOG DIALOG, java.awt.Font.DIALOG_INPUT DIALOG_INPUT,
java.awt.Font.SERIF SERIF, java.awt.Font.SANS_SERIF SANS_SERIF, and java.awt.Font.MONOSPACED MONOSPACED.
"Default" (use platform default)
TextAttribute.WEIGHT Number WEIGHT_REGULAR, WEIGHT_BOLD WEIGHT_REGULAR
TextAttribute.WIDTH Number WIDTH_CONDENSED, WIDTH_REGULAR,
WIDTH_EXTENDED
WIDTH_REGULAR
TextAttribute.POSTURE Number POSTURE_REGULAR, POSTURE_OBLIQUE POSTURE_REGULAR
TextAttribute.SIZE Number none 12.0
TextAttribute.TRANSFORM TransformAttribute See TransformAttribute TransformAttribute.IDENTITY IDENTITY TransformAttribute.IDENTITY
TextAttribute.SUPERSCRIPT Integer SUPERSCRIPT_SUPER, SUPERSCRIPT_SUB 0 (use the standard glyphs and metrics)
TextAttribute.FONT java.awt.Font none null (do not override font resolution)
TextAttribute.CHAR_REPLACEMENT GraphicAttribute none null (draw text using font glyphs)
TextAttribute.FOREGROUND java.awt.Paint none null (use current graphics paint)
TextAttribute.BACKGROUND java.awt.Paint none null (do not render background)
TextAttribute.UNDERLINE Integer UNDERLINE_ON -1 (do not render underline)
TextAttribute.STRIKETHROUGH Boolean STRIKETHROUGH_ON false (do not render strikethrough)
TextAttribute.RUN_DIRECTION Boolean RUN_DIRECTION_LTR
RUN_DIRECTION_RTL
null (use java.text.Bidi standard default)
TextAttribute.BIDI_EMBEDDING Integer none 0 (use base line direction)
TextAttribute.JUSTIFICATION Number JUSTIFICATION_FULL JUSTIFICATION_FULL
TextAttribute.INPUT_METHOD_HIGHLIGHT java.awt.im.InputMethodHighlight,
java.text.Annotation
(see class) null (do not apply input highlighting)
TextAttribute.INPUT_METHOD_UNDERLINE Integer UNDERLINE_LOW_ONE_PIXEL,
UNDERLINE_LOW_TWO_PIXEL
-1 (do not render underline)
TextAttribute.SWAP_COLORS Boolean SWAP_COLORS_ON false (do not swap colors)
TextAttribute.NUMERIC_SHAPING java.awt.font.NumericShaper none null (do not shape digits)
TextAttribute.KERNING Integer KERNING_ON 0 (do not request kerning)
TextAttribute.LIGATURES Integer LIGATURES_ON 0 (do not form optional ligatures)
TextAttribute.TRACKING Number TRACKING_LOOSE, TRACKING_TIGHT 0 (do not add tracking)

这两个方法并不完全相等, 静态那个是做了 些什么后又调用构造那个

new Font(Map<? extends Attribute, ?> attributes)

源码👇

    /**
     * Creates a new {@code Font} with the specified attributes.
     * Only keys defined in {@link java.awt.font.TextAttribute TextAttribute}
     * are recognized.  In addition the FONT attribute is
     *  not recognized by this constructor
     * (see {@link #getAvailableAttributes}). Only attributes that have
     * values of valid types will affect the new {@code Font}.
     * <p>
     * If {@code attributes} is {@code null}, a new
     * {@code Font} is initialized with default values.
     * @see java.awt.font.TextAttribute
     * @param attributes the attributes to assign to the new
     *          {@code Font}, or {@code null}
     */
    public Font(Map<? extends Attribute, ?> attributes) {
        initFromValues(AttributeValues.fromMap(attributes, RECOGNIZED_MASK));
    }

    /**
     * Initialize the standard Font fields from the values object.
     */
    private void initFromValues(AttributeValues values) {
        this.values = values;
        values.defineAll(PRIMARY_MASK); // for 1.5 streaming compatibility

        this.name = values.getFamily();
        this.pointSize = values.getSize();
        this.size = (int)(values.getSize() + 0.5);
        if (values.getWeight() >= 2f) this.style |= BOLD; // not == 2f
        if (values.getPosture() >= .2f) this.style |= ITALIC; // not  == .2f

        this.nonIdentityTx = values.anyNonDefault(EXTRA_MASK);
        this.hasLayoutAttributes =  values.anyNonDefault(LAYOUT_MASK);
    }
Font.getFont(Map<? extends Attribute, ?> attributes)

源码👇

    /**
     * Returns a {@code Font} appropriate to the attributes.
     * If {@code attributes} contains a {@code FONT} attribute
     * with a valid {@code Font} as its value, it will be
     * merged with any remaining attributes.  See
     * {@link java.awt.font.TextAttribute#FONT} for more
     * information.
     *
     * @param attributes the attributes to assign to the new
     *          {@code Font}
     * @return a new {@code Font} created with the specified
     *          attributes
     * @throws NullPointerException if {@code attributes} is null.
     * @since 1.2
     * @see java.awt.font.TextAttribute
     */
    public static Font getFont(Map<? extends Attribute, ?> attributes) {
        // optimize for two cases:
        // 1) FONT attribute, and nothing else
        // 2) attributes, but no FONT

        // avoid turning the attributemap into a regular map for no reason
        if (attributes instanceof AttributeMap &&
            ((AttributeMap)attributes).getValues() != null) {
            AttributeValues values = ((AttributeMap)attributes).getValues();
            if (values.isNonDefault(EFONT)) {
                Font font = values.getFont();
                if (!values.anyDefined(SECONDARY_MASK)) {
                    return font;
                }
                // merge
                values = font.getAttributeValues().clone();
                values.merge(attributes, SECONDARY_MASK);
                return new Font(values, font.name, font.style,
                                font.createdFont, font.font2DHandle);
            }
            return new Font(attributes);
        }

测试代码:👇

package awt;

import java.awt.*;
import java.awt.event.*;
import java.awt.font.GraphicAttribute;
import java.awt.font.TextAttribute;
import java.awt.font.TransformAttribute;
import java.awt.im.InputMethodHighlight;
import java.util.HashMap;

import javax.swing.*;

public class FontTextAttribute2205272329 {
	static JFrame frame = new JFrame("FontTextAttribute2205272329");
	static {
		frame.addWindowListener(new WindowAdapter() {
			@Override public void windowClosing(WindowEvent ev) {frame.dispose(); System.exit(0);}
		});
		frame.setBounds(100, 50, 1600, 900);
		frame.setLayout(new GridLayout(2, 1, 10, 10));
	}
	
	
	static Font font1 , font2;
	public static void main(String...arguments) {
		HashMap<java.awt.font.TextAttribute, Object> atts = new HashMap<>();
		atts.put(TextAttribute.FAMILY, Font.DIALOG);  // TextAttribute.FAMILY 字体家族
		atts.put(TextAttribute.SIZE, 30); // 字体大小
		atts.put(TextAttribute.WIDTH, TextAttribute.WIDTH_CONDENSED);
		atts.put(TextAttribute.BACKGROUND, Color.GREEN);  //文本区的背景色
		atts.put(TextAttribute.FOREGROUND, Color.RED);  //字体颜色, 会覆盖showText1的ForeGround
		atts.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);  //字体权重, 可设置粗体等
		atts.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE); //姿势 , 默认:POSTURE_REGULAR:正体 , POSTURE_OBLIQUE:斜体
		atts.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_DOTTED); //下滑线样式
		atts.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON); //删除线样式
		atts.put(TextAttribute.TRANSFORM, TransformAttribute.IDENTITY);  //待研究
		atts.put(TextAttribute.CHAR_REPLACEMENT, GraphicAttribute.CENTER_BASELINE);
		atts.put(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_LTR);
		atts.put(TextAttribute.BIDI_EMBEDDING	, Integer.valueOf(30));
		atts.put(TextAttribute.JUSTIFICATION	, TextAttribute.JUSTIFICATION_NONE);  // 两端对齐?  默认是:JUSTIFICATION_FULL:1.0f
		atts.put(TextAttribute.KERNING	, Integer.valueOf(50));  // 字间距?
		atts.put(TextAttribute.LIGATURES	, TextAttribute.LIGATURES_ON);  // 连字?
		atts.put(TextAttribute.TRACKING	, TextAttribute.TRACKING_TIGHT);  // 跟踪?
		atts.put(TextAttribute.INPUT_METHOD_HIGHLIGHT	, InputMethodHighlight.SELECTED_RAW_TEXT_HIGHLIGHT);  // ?
		atts.put(TextAttribute.INPUT_METHOD_UNDERLINE	, TextAttribute.UNDERLINE_LOW_TWO_PIXEL);  // ?
		
		
		font1=new Font(atts);
		font2=Font.getFont(atts);
		
		JTextArea showText1 = new JTextArea(); showText1.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 3, true));
		showText1.setBackground(Color.DARK_GRAY); showText1.setForeground(Color.LIGHT_GRAY);
		showText1.setText("""
		HashMap<java.awt.font.TextAttribute, Object> atts = new HashMap<>();
		atts.put(TextAttribute.FAMILY, Font.DIALOG);  // TextAttribute.FAMILY 字体家族
		atts.put(TextAttribute.SIZE, 16); // 字体大小
		atts.put(TextAttribute.WIDTH, TextAttribute.WIDTH_CONDENSED);
		atts.put(TextAttribute.BACKGROUND, Color.GREEN);  //文本区的背景色
		atts.put(TextAttribute.FOREGROUND, Color.RED);  //字体颜色, 会覆盖容器的ForeGround
		atts.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE); //姿势 , 默认:POSTURE_REGULAR:正体 , POSTURE_OBLIQUE:斜体
		atts.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_DOTTED); //下滑线样式
		atts.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON); //删除线样式
		atts.put(TextAttribute.TRANSFORM, TransformAttribute.IDENTITY);  //待研究
		atts.put(TextAttribute.CHAR_REPLACEMENT, GraphicAttribute.CENTER_BASELINE);
		atts.put(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_LTR);
		atts.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
		atts.put(TextAttribute.BIDI_EMBEDDING	, Integer.valueOf(30));
		atts.put(TextAttribute.JUSTIFICATION	, TextAttribute.JUSTIFICATION_NONE);  // 两端对齐?  默认是:JUSTIFICATION_FULL:1.0f
		atts.put(TextAttribute.KERNING	, Integer.valueOf(50));  // 字间距?
		atts.put(TextAttribute.LIGATURES	, TextAttribute.LIGATURES_ON);  // 连字?
		atts.put(TextAttribute.TRACKING	, TextAttribute.TRACKING_TIGHT);  // 跟踪?
		atts.put(TextAttribute.INPUT_METHOD_HIGHLIGHT	, InputMethodHighlight.SELECTED_RAW_TEXT_HIGHLIGHT);  // ?
		atts.put(TextAttribute.INPUT_METHOD_UNDERLINE	, TextAttribute.UNDERLINE_LOW_TWO_PIXEL);  // ?
				""");
		
		JTextArea showText2 = new JTextArea(); showText2.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 3, true));
		showText2.setBackground(Color.DARK_GRAY); showText2.setForeground(Color.LIGHT_GRAY);
		showText2.setText(showText1.getText());
		
		
		showText1.setFont(font1);
		showText2.setFont(font2);
		frame.add(showText1);
		frame.add(showText2);
		frame.setVisible(true);
	}
}


在这里插入图片描述

TextAttribute

Font支持的TextAttribute属性 可通过 getAvailableAttributes()方法获得

源码👇

   /**
     * Returns the keys of all the attributes supported by this
     * {@code Font}.  These attributes can be used to derive other
     * fonts.
     * @return an array containing the keys of all the attributes
     *          supported by this {@code Font}.
     * @since 1.2
     */
    public Attribute[] getAvailableAttributes() {
        // FONT is not supported by Font

        Attribute[] attributes = {
            TextAttribute.FAMILY,  // 字体家族
            TextAttribute.WEIGHT,  //可设为粗体等
            TextAttribute.WIDTH,  //整体宽度
            TextAttribute.POSTURE, //姿势, 可设斜体
            TextAttribute.SIZE,  //单字大小
            TextAttribute.TRANSFORM,
            TextAttribute.SUPERSCRIPT,  //上标或下标
            TextAttribute.CHAR_REPLACEMENT,
            TextAttribute.FOREGROUND, // 字体色
            TextAttribute.BACKGROUND,  //字体背景色
            TextAttribute.UNDERLINE,  // 下划线样式
            TextAttribute.STRIKETHROUGH, //删除线样式
            TextAttribute.RUN_DIRECTION,
            TextAttribute.BIDI_EMBEDDING,
            TextAttribute.JUSTIFICATION, // 两端对齐方式
            TextAttribute.INPUT_METHOD_HIGHLIGHT,
            TextAttribute.INPUT_METHOD_UNDERLINE,
            TextAttribute.SWAP_COLORS,
            TextAttribute.NUMERIC_SHAPING,
            TextAttribute.KERNING,   // 字间距?
            TextAttribute.LIGATURES,
            TextAttribute.TRACKING,
        };

        return attributes;
    }

Key, value type, principal constants, and default value behavior of all TextAttributes
Key Value Type Principal Constants Default Value 说明
FAMILY String See Font java.awt.Font.DIALOG DIALOG, java.awt.Font.DIALOG_INPUT DIALOG_INPUT,
java.awt.Font.SERIF SERIF, java.awt.Font.SANS_SERIF SANS_SERIF, and java.awt.Font.MONOSPACED MONOSPACED.
"Default" (use platform default) 字体家族
WEIGHT Number WEIGHT_REGULAR, WEIGHT_BOLD WEIGHT_REGULAR WEIGHT_REGULAR正体, WEIGHT_BOLD粗体
WEIGHT_EXTRA_LIGHT 0.5f 最细
WEIGHT_LIGHT 0.75f 细
WEIGHT_DEMILIGHT 0.875f 稍微细
WEIGHT_REGULAR 1.0f 正体,标准
WEIGHT_SEMIBOLD 1.25f 稍粗, 比 WEIGHT_REGULAR 稍重的 weight。
WEIGHT_MEDIUM 1.5f WEIGHT_REGULAR 和 WEIGHT_BOLD 之间的中间 weight。
WEIGHT_DEMIBOLD 1.75f 比 WEIGHT_BOLD 稍轻的 weight。
WEIGHT_BOLD 2.0f 粗体
WEIGHT_HEAVY 2.25f 比 WEIGHT_BOLD 稍重的 weight。
WEIGHT_EXTRABOLD 2.5f 特别粗
WEIGHT_ULTRABOLD 2.75f 最重的预定义 weight。
WIDTH Number WIDTH_CONDENSED, WIDTH_REGULAR,
WIDTH_EXTENDED
WIDTH_REGULAR 整体宽度:
WIDTH_CONDENSED 紧缩,
WIDTH_REGULAR 常规,
WIDTH_EXTENDED 延伸
POSTURE Number POSTURE_REGULAR, POSTURE_OBLIQUE POSTURE_REGULAR 姿势 : TextAttribute.POSTURE_REGULAR 正体,
TextAttribute.POSTURE_OBLIQUE 斜体
SIZE Number none 12.0 单字大小
TRANSFORM TransformAttribute See TransformAttribute TransformAttribute.IDENTITY IDENTITY TransformAttribute.IDENTITY 字体转换的属性键。这些值是 TransformAttribute 的实例。默认值为 TransformAttribute.IDENTITY。 TransformAttribute 类定义了常量 IDENTITY。 这对应于传递给 Font.deriveFont(AffineTransform) 的转换。由于该转换是可变的,并且 TextAttribute 值不可以为 null,所以使用的是 TransformAttribute 包装器类。 该值主要用于支持缩放和倾斜,但可能还有其他作用。 某些转换会导致基线旋转和/或移位。将文本和基线一起转换,从而文本将遵循新的基线。例如,对于水平基线上的文本,新的基线将遵循通过转换得到的 x 向量单元的方向。文本规格是针对此新基线进行测量的。因此,如果其他属性相同,使用旋转的 TRANSFORM 和非旋转的 TRANSFORM 呈现的文本都将视为具有相同上行高度 (ascent)、下行高度 (descent) 和步进 (advance)。 在样式化文本中,每个这样延伸的基线都将一个接一个地排列,从而可能为整个文本延伸创建一条非线性的基线。有关更多信息,请参阅 TextLayout.getLayoutPath()。
SUPERSCRIPT Integer SUPERSCRIPT_SUPER, SUPERSCRIPT_SUB 0 (use the standard glyphs and metrics) SUPERSCRIPT_SUPER = 1 上标
默认值 0 表示正常
TextAttribute.SUPERSCRIPT_SUB = -1 下标
FONT java.awt.Font none null (do not override font resolution) 用于提供呈现文本字体的属性键。这些值是 Font 的实例。默认值为 null,指示应该根据属性执行 Font 的正常分辨率。 TextLayout 和 AttributedCharacterIterator 根据 TextAttribute 的 Map 进行工作。通常,所有属性都被检查,并被用于选择和配置 Font 实例。然而,如果存在 FONT 属性,将使用其关联 Font。这为用户提供了一种将字体的分辨率属性重写到 Font,或强制使用特定 Font 实例的方法。这还允许用户在可以子类化 Font 的情况下指定 Font 的子类。 FONT 适用于特殊情形,在这些情形下,客户端已经拥有 Font 实例但是仍然需要使用基于 Map 的 API。通常,Map 中除了 FONT 属性以外没有其他属性。对于基于 Map 的 API,常见情形是单独指定所有属性,因此无需 FONT。 但是,如果 FONT 和其他属性都存在于 Map 中,那么呈现系统会将 Font 中定义的属性与附加属性合并。此合并过程将 TextAttributes 分为两组。一组(“主要”组)被认为是字体的选择和规格行为的基础。这些属性有 FAMILY、WEIGHT、WIDTH、POSTURE、SIZE、TRANSFORM、SUPERSCRIPT 和 TRACKING。另一组(“次要”组)由除了 FONT 自身以外的所有其他已定义属性组成。 要生成新的 Map,首先需要从 FONT 属性获得 Font,并将它的所有 属性提取为一个新的 Map。然后,只将取自原始 Map 的次要 属性添加到新 Map 的属性中。因此,主要属性的值仅取自 Font,而次要属性的值源自 Font,但可以用 Map 中的其他值重写它们。 注:Font 的基于 Map 的构造方法和 deriveFont 方法不处理 FONT 属性,因为这些属性用于创建新的 Font 对象。相反,Font.getFont(Map) 应该用于处理 FONT 属性。
CHAR_REPLACEMENT GraphicAttribute none null (draw text using font glyphs) 用于取代字符的字体标准字型显示的用户定义字形的属性键。这些值是 GraphicAttribute 的实例。默认值为 null,指示应该使用字体提供的标准字形。 此属性用于为图形或文本行中嵌入的其他组件保留空间。在执行双向重排序(请参阅 Bidi)时,它对于正确排列“内联”组件在某一行中的位置是必需的。每个字符(Unicode 代码点)都将使用提供的 GraphicAttribute 呈现。通常,应用此属性的字符应是 \uFFFC。 GraphicAttribute 确定文本的逻辑边界和可视边界;实际 Font 值被忽略。
FOREGROUND java.awt.Paint none null (use current graphics paint) 前景, 字体颜色
BACKGROUND java.awt.Paint none null (do not render background) 字体背景 ,不包括容器背景
UNDERLINE Integer UNDERLINE_ON -1 (do not render underline) 下划线 :
UNDERLINE_ON 标准下划线
UNDERLINE_LOW_ONE_PIXEL 单像素实心低下划线 实线
UNDERLINE_LOW_TWO_PIXEL 双像素实心低下划线 双实线
UNDERLINE_LOW_DOTTED 单像素点线式低下划线 点状线
UNDERLINE_LOW_GRAY 双像素灰色低下划线
UNDERLINE_LOW_DASHED 单像素虚线下划线。 虚线
STRIKETHROUGH Boolean STRIKETHROUGH_ON false (do not render strikethrough) 删除线, 只有两选项,有或没有 STRIKETHROUGH_ON为Boolean类的TRUE值
RUN_DIRECTION Boolean RUN_DIRECTION_LTR
RUN_DIRECTION_RTL
null (use java.text.Bidi standard default) 线条延伸方向的属性键。这些值是 Boolean 的实例。默认值为 null,指示用来确定延伸方向的标准 Bidi 算法应该与 Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT 值一起使用。 提供的常量是 RUN_DIRECTION_RTL 和 RUN_DIRECTION_LTR。 这确定了传递给 Bidi 构造方法的值,以选择段落中文本的主方向。 注:对于段落中的所有文本,此属性都应该具有相同的值,否则,行为将是不确定的。
BIDI_EMBEDDING Integer none 0 (use base line direction) 文本嵌入级别的属性键。这些值是 Integer 的实例。默认值为 null,指示在没有显式嵌入的情况下应该使用双向算法。 1 到 61 的正值是嵌入 级别,-1 到 -61 的负值是重写 级别。0 值表示使用基线方向。这些级别被传入 Bidi 构造方法的嵌入级别数组。 注:如果此属性出现在段落中的任何位置,则该段落中的所有 Unicode bidi 控制字符(RLO、LRO、RLE、LRE 和 PDF)都将被忽略,没有此属性的情况下,文本延伸时假定此属性存在,并且属性值为 0。
JUSTIFICATION Number JUSTIFICATION_FULL , JUSTIFICATION_NONE JUSTIFICATION_FULL 段落调整的属性键。这些值是 Number 的实例。默认值为 1,指示调整时应该使用所提供的完整宽度。这些值被限制在 [0..1] 范围内。 提供的常量是 JUSTIFICATION_FULL 和 JUSTIFICATION_NONE。 当请求在 TextLayout 上进行调整时,指定要使用的额外空间部分。例如,如果该线条宽为 50 磅,要求调整到 70 磅,则值 0.75 会使用多余空间的四分之三(或 15 磅)来填补,这样得到的线条长度为 65 磅。 注:对于段落中的所有文本,此属性都应该具有相同的值,否则,行为将是不确定的。
JUSTIFICATION_FULL 将线条调整到完整的请求宽度。这是 JUSTIFICATION 的默认值。
JUSTIFICATION_NONE 不允许调整线条。
INPUT_METHOD_HIGHLIGHT java.awt.im.InputMethodHighlight,
java.text.Annotation
(see class) null (do not apply input highlighting) 用于输入法高亮显示样式的属性键。 这些值是 InputMethodHighlight 或 Annotation 的实例。默认值为 null,指示在呈现前不应该应用输入法样式。 如果需要单独呈现具有相同 InputMethodHighlight 的文本邻近延伸,则应在 Annotation 实例中包装 InputMethodHighlight。 在使用输入法撰写文本时,将使用输入法高亮显示。即使是通常只处理无样式文本的文本编辑组件,也应保留输入法高亮显示,并使它们可用于绘制例程。
INPUT_METHOD_UNDERLINE Integer UNDERLINE_LOW_ONE_PIXEL,
UNDERLINE_LOW_TWO_PIXEL
-1 (do not render underline) 输入法下划线的属性键。这些值是 Integer 的实例。默认值为 -1,表示没有下划线。 这里提供了一些常量值,请参阅 UNDERLINE_LOW_ONE_PIXEL、UNDERLINE_LOW_TWO_PIXEL、UNDERLINE_LOW_DOTTED、UNDERLINE_LOW_GRAY 和 UNDERLINE_LOW_DASHED。 如果需要,此值可以与 UNDERLINE 一起使用。该值主要用于输入法。在其他地方使用这些简单装饰下划线可能会使用户迷惑。 输入法下划线可影响可视边界和文本轮廓。
SWAP_COLORS Boolean SWAP_COLORS_ON false (do not swap colors) 交换前景和背景 Paint 的属性键。这些值是 Boolean 的实例。默认值为 false,表示不交换颜色。
NUMERIC_SHAPING java.awt.font.NumericShaper none null (do not shape digits) 将 ASCII 十进制数字转换为其他十进制范围数字的属性键。这些值是 NumericShaper 的实例。默认值为 null,表示不执行数字成形。 当定义了一个数字成形器时,在执行其他任何文本分析之前,首先用成形器处理该文本。 注:对于段落中的所有文本,此属性都应该具有相同的值,否则行为将是不确定的。
KERNING Integer KERNING_ON 0 (do not request kerning) 请求进行字距调整的属性键。这些值是 Integer 的实例。默认值为 0,表示不请求进行字距调整。 提供的常量值是 KERNING_ON。 单个字符的默认 advance 不适合某些字符序列,例如,“To”或“AWAY”。若没有进行字距调整,那么相邻字符看起来好像使用了太多的空格来分隔。字距调整使选定的字符序列采用不同的空格来分隔,以获得更舒适的可视外观。
LIGATURES Integer LIGATURES_ON 0 (do not form optional ligatures) 启用可选连字的属性键。这些值是 Integer 的实例。默认值为 0,表示不使用可选的连字。 定义的常量值是 LIGATURES_ON。 始终启用书写系统所需的连字。
TRACKING Number TRACKING_LOOSE, TRACKING_TIGHT 0 (do not add tracking) 控制跟踪的属性键。这些值是 Number 的实例。默认值为 0,表示没有附加的跟踪。 提供的常量值是 TRACKING_TIGHT 和 TRACKING_LOOSE。 跟踪值与字体磅值大小相乘,并通过字体转换传递,以确定要添加到每种字形群的 advance 的附加量。正跟踪值将禁止可选连字格式。跟踪值通常在 -0.1 与 0.3 之间,超出此范围的值通常是不需要的。
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kfepiza

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值