请问各位大虾,为什么下列程序画出来的矩形是黑边的?
import java.awt.*;
import javax.swing.*;
class DrawRectangle extends JFrame
{
public static void main(String[] args)
{
DrawRectangle dr=new DrawRectangle();
dr.setSize(200,200);
dr.setDefaultCloseOperation(dr.EXIT_ON_CLOSE);
dr.add(new MyPanel());
dr.setVisible(true);
}
}
class MyPanel extends JPanel
{
public void paint(Graphics g)
{
super.paint(g);
g.drawRect(20,20,40,40);
g.setColor(Color.RED);
}
}
------解决方案--------------------
g.drawRect(20,20,40,40);改成
g.setColor(Color.RED);
g.setColor(Color.RED);就ok了(先设定颜色再画图,要记得呦),lz结贴吧
g.drawRect(20,20,40,40);