修飾子
public static final String SERIF
舉例如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | import java.awt.*; import java.awt.event.*; public class FontDemo1 { public static void main(String[] args) { Frame frame = new Frame( "AWTDemo" ); frame.addWindowListener( new AdapterDemo()); frame.setLayout( new GridLayout( 5 , 1 )); Label word1 = new Label( "There is no spoon." ); Font font1 = new Font(Font.DIALOG, Font.BOLD, 35 ); word1.setFont(font1); frame.add(word1); Label word2 = new Label( "There is no spoon." ); Font font2 = new Font(Font.DIALOG_INPUT, Font.ITALIC, 35 ); word2.setFont(font2); frame.add(word2); Label word3 = new Label( "There is no spoon." ); Font font3 = new Font(Font.MONOSPACED, Font.PLAIN, 35 ); word3.setFont(font3); frame.add(word3); Label word4 = new Label( "There is no spoon." ); Font font4 = new Font(Font.SANS_SERIF, Font.PLAIN, 35 ); word4.setFont(font4); frame.add(word4); Label word5 = new Label( "There is no spoon." ); Font font5 = new Font(Font.SERIF, Font.PLAIN, 35 ); word5.setFont(font5); frame.add(word5); frame.pack(); frame.setVisible( true ); System.out.println( "PLAIN: " + Font.PLAIN); System.out.println( "BOLD: " + Font.BOLD); System.out.println( "ITALIC: " + Font.ITALIC); System.out.println( "DIALOG: " + Font.DIALOG); System.out.println( "DIALOG_INPUT: " + Font.DIALOG_INPUT); System.out.println( "MONOSPACED: " + Font.MONOSPACED); System.out.println( "SANS_SERIF: " + Font.SANS_SERIF); System.out.println( "SERIF: " + Font.SERIF); } } class AdapterDemo extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit( 0 ); } } /* 《程式語言教學誌》的範例程式 檔名:FontDemo1.java 功能:示範 Java 程式 作者:張凱慶 時間:西元 2012 年 3 月 */ |
此例建立五個 Label 物件。第一個字型種類設定為 Font.DIALOG ,字型樣式設定為粗體 Font.BOLD ,字型大小為 35
10 11 12 13 | Label word1 = new Label( "There is no spoon." ); Font font1 = new Font(Font.DIALOG, Font.BOLD, 35 ); word1.setFont(font1); frame.add(word1); |
第二個字型種類設定為 Font.DIALOG_INPUT ,字型樣式設定為斜體 Font.ITALIC ,字型大小為 35
15 16 17 18 | Label word2 = new Label( "There is no spoon." ); Font font2 = new Font(Font.DIALOG_INPUT, Font.ITALIC, 35 ); word2.setFont(font2); frame.add(word2); |
第一個字型種類設定為 Font.MONOSPACED ,字型樣式設定為粗體 Font.PLAIN ,字型大小為 35
20 21 22 23 | Label word3 = new Label( "There is no spoon." ); Font font3 = new Font(Font.MONOSPACED, Font.PLAIN, 35 ); word3.setFont(font3); frame.add(word3); |
第四個字型種類設定為 Font.SANS_SERIF ,字型樣式設定為粗體 Font.PLAIN ,字型大小為 35
25 26 27 28 | Label word4 = new Label( "There is no spoon." ); Font font4 = new Font(Font.SANS_SERIF, Font.PLAIN, 35 ); word4.setFont(font4); frame.add(word4); |
第五個字型種類設定為 Font.SERIF ,字型樣式設定為粗體 Font.PLAIN ,字型大小為 35
30 31 32 33 | Label word5 = new Label( "There is no spoon." ); Font font5 = new Font(Font.SERIF, Font.PLAIN, 35 ); word5.setFont(font5); frame.add(word5); |
最後在命令列印出 PLAIN 、 BOLD 、 ITALIC 、 DIALOG 、 DIALOG_INPUT 、 MONOSPACED 、 SANS_SERIF 、 SERIF 的常數值
38 39 40 41 42 43 44 45 | System.out.println( "PLAIN: " + Font.PLAIN); System.out.println( "BOLD: " + Font.BOLD); System.out.println( "ITALIC: " + Font.ITALIC); System.out.println( "DIALOG: " + Font.DIALOG); System.out.println( "DIALOG_INPUT: " + Font.DIALOG_INPUT); System.out.println( "MONOSPACED: " + Font.MONOSPACED); System.out.println( "SANS_SERIF: " + Font.SANS_SERIF); System.out.println( "SERIF: " + Font.SERIF); |
編譯後執行,結果如下

中英文術語對照 | |
---|---|
類別 | class |
屬性 | field |
參數 | parameter |
您可以繼續參考
字型與顏色
相關目錄
Java API 分類導覽
Java 教材
首頁
參考資料
http://docs.oracle.com/javase/6/docs/api/java/awt/Font.html
沒有留言:
張貼留言