JAVA字体颜色,背景颜色问题

来源:百度知道 编辑:UC知道 时间:2024/09/28 11:13:55
import java.awt.*;
import java.applet.Applet;
public class word extends Applet
{
Font f1=new Font("黑体",Font.PLAIN,24);
Font f2=new Font("宋体",Font.ITALIC+Font.BOLD,16);
public void paint(Graphics g)
{
setBackground(Color.black);
g.setFont(f1);
g.setColor(new Color(255,0,0));
g.drawString("Name:SongTiantian",20,20);
g.setFont(f2);
g.setColor(new Color(0,255,0));
g.drawString("Number:02045016",20,40);
g.setColor(new Color(255,0,255));
g.drawString("Class:Computer021",20,60);
}
}
为什么setFont,setColor使用的时候要用g.setFont,g.setColor而setBackground使用时候不用g.setBackground?它们不都是java.awt.Graphics类的方法吗
还是想不通啊.setBackground既然是java.awt.Graphics类的方法(书上写的,不是吗),g是Graphics的对象,那为什么不能写成g.setBackground呢?

此处的setBackground()是要设置面板的颜色,调用的是Applet类中的方法,而setColor(),setFont()是要设置写出的字的属性

所属的对象不同、setBackground是form的对象相当于this.getContentPane().setBackground,所以可省略