版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013899461/article/details/22896893
/*
* 功能:字体应用
* 作者:林同学
*/
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;
}
运行结果: