使用fillRect()方法.
g.fillRect(left, right, width, height);
drawRect()
Draws the outline of the specified rectangle. The left and right edges of the rectangle are at x and x + width. The top and bottom edges are at y and y + height. The rectangle is drawn using the graphics context’s current color.
fillRect()
Fills the specified rectangle. The left and right edges of the rectangle are at x and x + width – 1. The top and bottom edges are at y and y + height – 1. The resulting rectangle covers an area width pixels wide by height pixels tall. The rectangle is filled using the graphics context’s current
color.
“这仅适用于对象边框”,因为drawRect仅绘制轮廓.
“总要为整个对象设置颜色吗?”您误会了.和setColor()将颜色设置为要绘制的轮廓,则只能看到轮廓,而不是因为setColor()将颜色设置为border.