第五週作業-字型設定
阿新 • • 發佈:2018-12-29
/* * 功能:字型應用 * 作者:林同學 */ import java.awt.*; import java.awt.font.*; import javax.swing.*; public class FontSet { public static void main(String[] args) { FontFrame frame = new FontFrame(); //新建一個字體面板 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); //設定面板視覺化 } } class FontFrame extends JFrame { public FontFrame() { setTitle("設定字型"); setSize(WIDTH,HEIGHT); JTextField tf = new JTextField(4); FontPanel panel = new FontPanel(); //將panel加入到Frame panel.add(tf); tf.setEditable(false); Container contentPane = getContentPane(); contentPane.add(panel); Font f1 = new Font("楷體",Font.BOLD,15); tf.setFont(f1); tf.setText("宋體"); } public static final int WIDTH = 300; public static final int HEIGHT = 140; } class FontPanel extends JPanel { public void paintComponent(Graphics g) //呼叫paintComponent方法設定字型 { super.paintComponent(g); //設定字型 Font f = new Font("宋體",Font.BOLD + Font.ITALIC,20); g.setFont(f); //顯示文字 g.drawString("Java歡迎您!",x,y); } public int x = 60; public int y = 50; }
執行結果: