public class Test extends JFrame {
public Test() {
setLayout(null);

int i = 9;
int x = 10;
int y = 10;
while(i>0) {
C c1 = new C();
c1.setSize(100,100);
c1.setLocation(x, y);
this.add(c1);
System.out.println(x + ":" + y);
x+=60;
i--;
}
this.setSize(300,300);
}

public static void main(String[] args) {
JFrame j = new Test();
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setVisible(true);
}
}class C extends JComponent { @Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.YELLOW);
g2d.fillRect(getX(), getY(), getWidth(), getHeight());
//System.out.println(getWidth()+""+getHeight());
}
}对于上面的程序,绘制第一个矩形能成功,但是绘制到第二个的时候就只能绘制出一半,然后后续的几个矩形都绘制不出
这是一个例子程序,我的工程中遇到了类似的问题,希望大家给我解答一下,迷茫了一天了,唉