java paint和JFrame` 背景色的问题`

来源:百度知道 编辑:UC知道 时间:2024/05/21 13:09:31
import java.awt.*;

import javax.swing.*;

public class Zhulei extends JFrame
{

/**
*
*/
private static final long serialVersionUID = -8513034365211500719L;
int x=50,y=50;

//paint方法框架

public void paint(Graphics a)
{
Color c=a.getColor();
a.setColor(Color.RED);
a.fillOval(x,y,30,30); //图形位置(我的坦克)
a.setColor(c);

y+=5;
}

public void asdf()
{
this.setLocation(400,300);
this.setSize(800,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
this.setBackground(Color.BLACK);
}

public static void main(String[] args)
{

Zhulei zl=new Zhulei();
zl.asdf();

}

}

我这个程序设置的背景应该是黑色` 但是我运行的时候却没有背景色` 而背景却是 `程序启动时 窗口后面我电脑的截图... 谢谢
电脑的问题? 具体是什么地方的问题呢?? 谢谢

1.一般都覆盖paintComponent方法而不是paint方法
2.在覆盖的paintComponent里调用父类的paintComponent方法
super.paintComponent(g)

竟然不听我的...你没有调用super.paintComponent(g)才会没背景色的

你的程序没有问题,应该是电脑有点小问题。你看我运行你程序的截图:

一楼的正解。
AWT组件(如窗口Frame、按钮Button)重写paint(Graphics)
swing组件(如JFrame、JButton)重写paintComponent(Graphics)

儿歌特

public void paint(Graphics a)
{
Color c=a.getColor();

a.setColor(Color.green);
a.