日期:2014-05-20  浏览次数:20839 次

请教小程序出了什么问题
源程序如下:
import   java.awt.*;
import   javax.swing.*;

public   class   Chessboard   extends   JApplet{
int   baseXPosition,   baseYPosition;
int   currentXPosition,   currentYPosition;

public   void   init(){
baseXPosition   =   40;
baseYPosition   =   40;

setBackground(Color.black);
}

public   void   paint(Graphics   g){
currentXPosition=baseXPosition;
currentYPosition=baseYPosition;

    for(int   row=0;row <8;row++){
   
            currentXPosition=   baseXPosition+row*40;
            for(int   column=0;column <8;column++){
            if((column+row)%2==0)
                    g.setColor(Color.white);
           
            else  
                    g.setColor(Color.gray);
            currentYPosition=baseXPosition+column*40;
            g.fillRect(currentXPosition,currentYPosition,40,40);
           
           
            }
           
            }
    }
}

运行结果却没有显示背景为黑色,请教是何缘故?

------解决方案--------------------
改了一下,看看是否能符合你的要求:

import java.awt.*;
import javax.swing.*;

public class Chessboard extends JApplet{
int baseXPosition, baseYPosition;
int currentXPosition, currentYPosition;

public void init(){
baseXPosition = 40;
baseYPosition = 40;
}

public void paint(Graphics g){
currentXPosition=baseXPosition;
currentYPosition=baseYPosition;

g.setColor(Color.black);//这两行是后添加上的
g.fillRect(0, 0, 400, 400);

for(int row=0;row <8;row++){

currentXPosition= baseXPosition+row*40;
for(int column=0;column <8;column++){
if((column+row)%2==0)
g.setColor(Color.white);

else
g.setColor(Color.gray);
currentYPosition=baseXPosition+column*40;
g.fillRect(currentXPosition,currentYPosition,40,40);


}

}
}
}

------解决方案--------------------
this.setForeground(Color.black);
设置背景色不行 你看看前景色行不