当前位置: 首页 > 文档资料 > JAVA AWT 中文文档 >

Font

优质
小牛编辑
114浏览
2023-12-01

介绍 (Introduction)

Font类声明了用于以可见方式呈现文本的字体。

类声明

以下是java.awt.Font类的声明:

public class Font
   extends Object
      implements Serializable

字段 (Field)

以下是java.awt.geom.Arc2D类的字段:

  • static int BOLD - 粗体样式常量。

  • static int CENTER_BASELINE - 布局文本时表意脚本(如中文,日文和韩文)中使用的基线。

  • static String DIALOG - 逻辑字体“Dialog”的规范系列名称的字符串常量。

  • static String DIALOG_INPUT - 逻辑字体“DialogInput”的规范系列名称的字符串常量。

  • static int HANGING_BASELINE - 在布局文本时Devanigiri和类似脚本中使用的基线。

  • static int ITALIC - 斜体样式常量。

  • static int LAYOUT_LEFT_TO_RIGHT - static int LAYOUT_LEFT_TO_RIGHT的标志,指示由Bidi分析确定的文本是从左到右。

  • static int LAYOUT_NO_LIMIT_CONTEXT - layoutGlyphVector的标志,指示不应检查指定限制之后的char数组中的文本。

  • static int LAYOUT_NO_START_CONTEXT - layoutGlyphVector的标志,指示不应检查指示的启动之前的char数组中的文本。

  • static int LAYOUT_RIGHT_TO_LEFT - static int LAYOUT_RIGHT_TO_LEFT的标志,指示由Bidi分析确定的文本是从右到左。

  • static String MONOSPACED - 逻辑字体“Monospaced”的规范系列名称的String常量。

  • protected String name - 传递给构造函数的此Font的逻辑名称。

  • static int PLAIN - 普通样式常量。

  • protected float pointSize - protected float pointSize中此Font的磅值。

  • static int ROMAN_BASELINE - 布局文本时大多数罗马脚本中使用的基线。

  • static String SANS_SERIF - 逻辑字体“SansSerif”的规范系列名称的String常量。

  • static String SERIF - 逻辑字体“Serif”的规范系列名称的String常量。

  • protected int size - 此Font的磅值,舍入为整数。

  • protected int style - 传递给构造函数的Font的样式。

  • static int TRUETYPE_FONT - 标识static int TRUETYPE_FONT类型的字体资源。

  • static int TYPE1_FONT - 标识TYPE1类型的字体资源。

类构造函数

SN构造函数和描述
1

protected Font() ()

从指定的字体创建一个新的Font。

2

Font(Map《? extends AttributedCharacterIterator.Attribute,?》 attributes)

从指定的字体创建一个新的Font。

3

Font(String name, int style, int size)

从指定的字体创建一个新的Font。

类方法

SN方法和描述
1

boolean canDisplay(char c)

检查此Font是否具有指定字符的字形。

2

boolean canDisplay(int codePoint)

检查此Font是否具有指定字符的字形。

3

int canDisplayUpTo(char[] text, int start, int limit)

指示此Font是否可以显示从start开始到limit结束的指定文本中的字符。

4

int canDisplayUpTo(CharacterIterator iter, int start, int limit)

指示此Font是否可以显示iter指定的文本,从start开始到limit。

5

int canDisplayUpTo(String str)

指示此Font是否可以显示指定的String。

6

static Font createFont(int fontFormat, File fontFile)

使用指定的字体类型和指定的字体文件返回一个新的Font。

7

static Font createFont(int fontFormat, InputStream fontStream)

使用指定的字体类型和输入数据返回一个新Font。

8

GlyphVector createGlyphVector(FontRenderContext frc, char[] chars)

通过基于此Font中的Unicode cmap将字符一对一映射到字形来创建GlyphVector。

9

GlyphVector createGlyphVector(FontRenderContext frc, CharacterIterator ci)

通过基于此Font中的Unicode cmap将指定字符一对一地映射到字形来创建GlyphVector。

10

GlyphVector createGlyphVector(FontRenderContext frc, int[] glyphCodes)

通过基于此Font中的Unicode cmap将字符一对一映射到字形来创建GlyphVector。

11

GlyphVector createGlyphVector(FontRenderContext frc, String str)

通过基于此Font中的Unicode cmap将字符一对一映射到字形来创建GlyphVector。

12

static Font decode(String str)

返回str参数描述的Font。

13

Font deriveFont(AffineTransform trans)

通过复制当前Font对象并对其应用新变换来创建新的Font对象。

14

Font deriveFont(float size)

通过复制当前Font对象并对其应用新大小来创建新的Font对象。

15

Font deriveFont(int style)

通过复制当前Font对象并对其应用新样式来创建新的Font对象。

16

Font deriveFont(int style, AffineTransform trans)

通过复制此Font对象并应用新样式和变换来创建新的Font对象。

17

Font deriveFont(int style, float size)

通过复制此Font对象并应用新的样式和大小来创建新的Font对象。

18

Font deriveFont(Map《? extends AttributedCharacterIterator.Attribute,?》 attributes)

通过复制当前Font对象并为其应用一组新的字体属性来创建新的Font对象。

19

boolean equals(Object obj)

将此Font对象与指定的Object进行比较。

20

protected void finalize()

处置本机Font对象。

21

Map《TextAttribute,?》 getAttributes()

返回此Font中可用的字体属性的映射。

22

AttributedCharacterIterator.Attribute[] getAvailableAttributes()

返回此Font支持的所有属性的键。

23

byte getBaselineFor(char c)

返回适合显示此字符的基线。

24

String getFamily()

返回此Font的系列名称。

25

String getFamily(Locale l)

返回此Font的系列名称,已针对指定的语言环境进行了本地化。

26

static Font getFont(Map《? extends AttributedCharacterIterator.Attribute,?》 attributes)

返回适合于属性的Font。

27

static Font getFont(String nm)

返回系统属性列表中的Font对象。

28

static Font getFont(String nm, Font font)

从系统属性列表中获取指定的Font。

29

String getFontName()

返回此Font的字体名称。

30

String getFontName(Locale l)

返回Font的字体外观名称,已针对指定的区域设置进行了本地化。

31

float getItalicAngle()

返回此Font的斜体角度。

32

LineMetrics getLineMetrics(char[] chars, int beginIndex, int limit, FontRenderContext frc)

返回使用指定参数创建的LineMetrics对象。

33

LineMetrics getLineMetrics(CharacterIterator ci, int beginIndex, int limit, FontRenderContext frc)

返回使用指定参数创建的LineMetrics对象。

34

LineMetrics getLineMetrics(String str, FontRenderContext frc)

返回使用指定的String和FontRenderContext创建的LineMetrics对象。

35

LineMetrics getLineMetrics(String str, int beginIndex, int limit, FontRenderContext frc)

返回使用指定参数创建的LineMetrics对象。

36

Rectangle2D getMaxCharBounds(FontRenderContext frc)

返回具有指定FontRenderContext中定义的最大边界的字符的边界。

37

int getMissingGlyphCode()

返回当此Font没有指定unicode代码点的字形时使用的glyphCode。

38

String getName()

返回此Font的逻辑名称。

39

int getNumGlyphs()

返回此Font中的字形数。

40

java.awt.peer.FontPeer getPeer()

已过时。 字体渲染现在与平台无关。

41

String getPSName()

返回此Font的postscript名称。

42

int getSize()

返回此Font的磅值,舍入为整数。

43

float getSize2D()

以float值返回此Font的磅值。

44

Rectangle2D getStringBounds(char[] chars, int beginIndex, int limit, FontRenderContext frc)

返回指定FontRenderContext中指定字符数组的逻辑边界。

45

Rectangle2D getStringBounds(CharacterIterator ci, int beginIndex, int limit, FontRenderContext frc)

返回指定FontRenderContext中指定CharacterIterator中索引的字符的逻辑边界。

46

Rectangle2D getStringBounds(String str, FontRenderContext frc)

返回指定FontRenderContext中指定String的逻辑边界。

47

Rectangle2D getStringBounds(String str, int beginIndex, int limit, FontRenderContext frc)

返回指定FontRenderContext中指定String的逻辑边界。

48

int getStyle()

返回此Font的样式。

49

AffineTransform getTransform()

返回与此Font关联的转换的副本。

50

int hashCode()

返回此Font的哈希码。

51

boolean hasLayoutAttributes()

如果此Font包含需要额外布局处理的属性,则返回true。

52

boolean hasUniformLineMetrics()

检查此Font是否具有统一的行度量标准。

53

boolean isBold()

指示此Font对象的样式是否为BOLD。

54

boolean isItalic()

指示此Font对象的样式是否为ITALIC。

55

boolean isPlain()

指示此Font对象的样式是否为PLAIN。

56

boolean isTransformed()

指示除Font属性外,此Font对象是否具有影响其大小的变换。

57

GlyphVector layoutGlyphVector(FontRenderContext frc, char[] text, int start, int limit, int flags)

返回一个新的GlyphVector对象,如果可能,执行文本的完整布局。

58

String toString()

将此Font对象转换为String表示形式。

方法继承

该类继承以下类中的方法:

  • java.lang.Object

字体示例

使用您选择的任何编辑器创建以下java程序,例如D:/ 》 AWT 》 com 》 iowiki 》 gui 》

AWTGraphicsDemo.java
package com.iowiki.gui;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class AWTGraphicsDemo extends Frame {
   public AWTGraphicsDemo(){
      super("Java AWT Examples");
      prepareGUI();
   }
   public static void main(String[] args){
      AWTGraphicsDemo  awtGraphicsDemo = new AWTGraphicsDemo();  
      awtGraphicsDemo.setVisible(true);
   }
   private void prepareGUI(){
      setSize(400,400);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 
   }    
   @Override
   public void paint(Graphics g) {
      Graphics2D g2 = (Graphics2D)g;        
      Font plainFont = new Font("Serif", Font.PLAIN, 24);        
      g2.setFont(plainFont);
      g2.drawString("Welcome to IoWiki", 50, 70); 
      Font italicFont = new Font("Serif", Font.ITALIC, 24);        
      g2.setFont(italicFont);
      g2.drawString("Welcome to IoWiki", 50, 120); 
      Font boldFont = new Font("Serif", Font.BOLD, 24);        
      g2.setFont(boldFont);
      g2.drawString("Welcome to IoWiki", 50, 170); 
      Font boldItalicFont = new Font("Serif", Font.BOLD+Font.ITALIC, 24);        
      g2.setFont(boldItalicFont);
      g2.drawString("Welcome to IoWiki", 50, 220); 
   }
}

使用命令提示符编译程序。 转到D:/ 》 AWT并键入以下命令。

D:\AWT>javac com\iowiki\gui\AWTGraphicsDemo.java

如果没有错误,那意味着编译成功。 使用以下命令运行程序。

D:\AWT>java com.iowiki.gui.AWTGraphicsDemo

验证以下输出

AWT字体