本来是在drawcomponent这个里边使用setBackground,你想啊drawcomponent是继承JComponent的所以它是一个容器,所以它同样有setBackground这个方法来设置它的背景颜色
但是因为你在设置它本身为一个画布,因为你用了paintComponent(Graphics?g)
这个方法,所以setBackground这个方法即使你用了也看不到很大的效果.但是有一种取代的方法就是在paintComponent(Graphics?g)方法中首先就用Graphics?所含有的方法g.setColor(Color.black);来设置背景颜色再用g.fillRect(0,?0,?this.getWidth(),?this.getHeight());来填满整个容器,这就达到了设置背景目的.然后你再g.setColor(其他颜色);来绘制其它图形.
具体代码:(在你以上的代码上修改了点)
public?void?paintComponent(Graphics?g)
{
g.setColor(Color.black);//这里设置背景颜色
g.fillRect(0,?0,?this.getWidth(),?this.getHeight());//这里填充背景颜色
double?x=100;
double?y=100;
ellipse.setFrame(rect);
double?centerx=rect.getCenterX();
double?centery=rect.getCenterY();
}
测试结果图
**************************************************************
新建一个类ChangeColor.java,代码如下:
import?java.awt.Color;
import?java.awt.event.MouseEvent;
import?java.awt.event.MouseMotionListener;
import?javax.swing.JFrame;
/**
*/
public?class?ChangeColor?extends?JFrame?implements?MouseMotionListener?{
public?ChangeColor()?{
this.setTitle("Change?Color");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.getContentPane().setBackground(Color.GREEN);
this.addMouseMotionListener(this);
public?void?mouseMoved(MouseEvent?e)?{
this.getContentPane().setBackground(Color.RED);
}?else?{
this.getContentPane().setBackground(Color.BLUE);
public?void?mouseDragged(MouseEvent?e)?{
public?static?void?main(String[]?args)?{
new?ChangeColor();
运行结果如下:
①.、我的世界颜色代码大全.
①我的世界颜色代码是amp.
①.0.b=蓝绿、amp.