- AWT 教程
- AWT - 首頁
- AWT - 概述
- AWT - 環境
- AWT - 控制元件
- AWT - 事件處理
- AWT - 事件類
- AWT - 事件監聽器
- AWT - 事件介面卡
- AWT - 佈局
- AWT - 容器
- AWT - 選單
- AWT - 圖形
- AWT 有用資源
- AWT - 快速指南
- AWT - 有用資源
- AWT - 討論
AWT 字型類
介紹
Font 類表示字型,用於以可見方式呈現文字。
類宣告
以下是java.awt.Font類的宣告
public class Font
extends Object
implements Serializable
欄位
以下是java.awt.geom.Arc2D類的欄位
static int BOLD -- 粗體樣式常量。
static int CENTER_BASELINE --在對中文、日文和韓文等表意文字進行文字佈局時使用的基線。
static String DIALOG --邏輯字型“Dialog”的規範族名稱的字串常量。
static String DIALOG_INPUT --邏輯字型“DialogInput”的規範族名稱的字串常量。
static int HANGING_BASELINE --在對梵文和類似文字進行文字佈局時使用的基線。
static int ITALIC -- 斜體樣式常量。
static int LAYOUT_LEFT_TO_RIGHT -- 指示文字根據雙向分析結果從左到右的 layoutGlyphVector 標誌。
static int LAYOUT_NO_LIMIT_CONTEXT -- 指示不應該檢查指示限制之後字元陣列中文字的 layoutGlyphVector 標誌。
static int LAYOUT_NO_START_CONTEXT -- 指示不應該檢查指示起始之前字元陣列中文字的 layoutGlyphVector 標誌。
static int LAYOUT_RIGHT_TO_LEFT -- 指示文字根據雙向分析結果從右到左的 layoutGlyphVector 標誌。
static String MONOSPACED -- 邏輯字型“Monospaced”的規範族名稱的字串常量。
protected String name -- 此 Font 的邏輯名稱,如傳遞給建構函式。
static int PLAIN --普通樣式常量。
protected float pointSize -- 此 Font 的點大小(浮點數)。
static int ROMAN_BASELINE --在對大多數羅馬文字進行文字佈局時使用的基線。
static String SANS_SERIF -- 邏輯字型“SansSerif”的規範族名稱的字串常量。
static String SERIF -- 邏輯字型“Serif”的規範族名稱的字串常量。
protected int size --此 Font 的點大小(四捨五入為整數)。
protected int style -- 此 Font 的樣式,如傳遞給建構函式。
static int TRUETYPE_FONT -- 標識型別為 TRUETYPE 的字型資源。
static int TYPE1_FONT -- 標識型別為 TYPE1 的字型資源。
類建構函式
| 序號 | 建構函式 & 描述 |
|---|---|
| 1 | protected Font() () 根據指定的字型建立一個新的 Font。 |
| 2 | Font(Map<? extends AttributedCharacterIterator.Attribute,?> attributes) 根據指定的字型建立一個新的 Font。 |
| 3 | Font(String name, int style, int size) 根據指定的字型建立一個新的 Font。 |
類方法
| 序號 | 方法 & 描述 |
|---|---|
| 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 是否可以顯示從 start 開始到 limit 結束的 iter 指定的文字。 |
| 5 | int canDisplayUpTo(String str) 指示此 Font 是否可以顯示指定的字串。 |
| 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) 返回使用指定的字串和 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() 返回此 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 中指定字串的邏輯邊界。 |
| 47 | Rectangle2D getStringBounds(String str, int beginIndex, int limit, FontRenderContext frc) 返回在指定的 FontRenderContext 中指定字串的邏輯邊界。 |
| 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 物件是否具有影響其大小的變換(除了大小屬性之外)。 |
| 57 | GlyphVector layoutGlyphVector(FontRenderContext frc, char[] text, int start, int limit, int flags) 返回一個新的 GlyphVector 物件,如果可能,執行文字的完整佈局。 |
| 58 | String toString() 將此 Font 物件轉換為字串表示形式。 |
繼承的方法
此類繼承自以下類的方法
java.lang.Object
Font 示例
使用您選擇的任何編輯器建立以下 Java 程式,例如在D:/ > AWT > com > tutorialspoint > gui >
AWTGraphicsDemo.javapackage com.tutorialspoint.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 TutorialsPoint", 50, 70);
Font italicFont = new Font("Serif", Font.ITALIC, 24);
g2.setFont(italicFont);
g2.drawString("Welcome to TutorialsPoint", 50, 120);
Font boldFont = new Font("Serif", Font.BOLD, 24);
g2.setFont(boldFont);
g2.drawString("Welcome to TutorialsPoint", 50, 170);
Font boldItalicFont = new Font("Serif", Font.BOLD+Font.ITALIC, 24);
g2.setFont(boldItalicFont);
g2.drawString("Welcome to TutorialsPoint", 50, 220);
}
}
使用命令提示符編譯程式。進入D:/ > AWT目錄並輸入以下命令。
D:\AWT>javac com\tutorialspoint\gui\AWTGraphicsDemo.java
如果沒有任何錯誤出現,則表示編譯成功。使用以下命令執行程式。
D:\AWT>java com.tutorialspoint.gui.AWTGraphicsDemo
驗證以下輸出