Java API 分類導覽 - java.awt.Graphics setFont()

Graphics 類別 (class) 的 setFont() 方法 (method) 設定繪圖字型。



修飾子與參數
public abstract void setFont(Font font)


setFont() 沒有回傳值 (return value) ,需要 Font 型態 (type) 的物件 (object) 當參數 (paramenter) 。


舉例如下
import java.awt.*;
import java.awt.event.*;

public class GraphicsDemo24 extends Canvas {
    public static void main(String[] args) {
        Frame frame = new Frame("AWTDemo");
        frame.addWindowListener(new AdapterDemo());
        frame.setSize(200, 220);
        
        GraphicsDemo24 canvas = new GraphicsDemo24();
        frame.add(canvas, BorderLayout.CENTER);
        
        frame.setVisible(true);
    }
    
    public void paint(Graphics g) {
        String s = "There is no spoon.";
        g.setFont(new Font(s, Font.BOLD, 20));
        g.drawString(s, 3, 30);
        
        System.out.println("getFont: " + g.getFont());
    }    
}

class AdapterDemo extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
}

/* 《程式語言教學誌》的範例程式
    http://pydoing.blogspot.com/
    檔名:GraphicsDemo24.java
    功能:示範 Java 程式 
    作者:張凱慶
    時間:西元 2012 年 3 月 */


此例在 paint() 方法中畫圖,先建立一個字串,然後呼叫 Graphics 的 setFont() 設定字型, drawString() 畫出字串,最後在命令列呼叫 getFont() 印出該字串物件
public void paint(Graphics g) {
    String s = "There is no spoon.";
    g.setFont(new Font(s, Font.BOLD, 20));
    g.drawString(s, 3, 30);
        
    System.out.println("getFont: " + g.getFont());
}


編譯後執行,結果如下



中英文術語對照
類別class
方法method
回傳值return value
型態type
物件object
參數parameter


您可以繼續參考
繪圖


相關目錄
Java API 分類導覽
Java 教材
首頁


參考資料
http://docs.oracle.com/javase/6/docs/api/java/awt/Graphics.html

沒有留言: