我延長JFrame的是這樣的:JFrame上畫一個簡單的矩形在java中
public GameFrame() {
this.setBounds(30, 30, 500, 500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
initializeSquares();
}
private void initializeSquares(){
for(int i = 0; i < 5; i++){
this.getContentPane().add(new Square(i*10, i*10, 100, 100));
}
this.setVisible(true);
}
但是,只有一個廣場被在屏幕上繪製,沒有任何人知道爲什麼嗎?
也是我的Square類看起來是這樣的:
private int x;
private int y;
private int width;
private int height;
public Square(int x, int y, int width, int height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(x, y, width, height);
}
好的,這似乎是,有沒有辦法不使用佈局管理器,只使用絕對定位? – wbarksdale 2012-03-15 03:13:23
您可以通過將容器的佈局設置爲「null」,但不推薦。最好做一些像上面的例子。 – 2012-03-15 03:20:54