第5周作业-字体设置
2014-04-03 20:03
330 查看
第九章-例9-3
import java.awt.*; import java.awt.font.*; import javax.swing.*; public class FontSet { public 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); FontPanel panel = new FontPanel(); TextField tf = new TextField (3); tf.setText(((Component) panel.f).getName()); panel.add(tf); Container contentPane = getContentPane(); contentPane.add(panel); } public static final int WIDTH = 300; public static final int HEIIGHT=140; } class FontPanel extends JPanel { public Object f; public void paintComponent(Graphics g) { super .paintComponent(g); //设置字体 Font f = new Font("宋体",Font.BOLD+Font.ITALIC,20); g.setFont(f); //显示文本 g.drawString("JAVA欢迎你!", x, y); } public int x = 55; public int y = 50; }
运行结果: