2012-03-15 40 views
6

我延長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); 
} 

回答

12

Jframe的contentPane中使用BorderLayout的默認。當你爲它添加一個Square時,它會默認添加BorderLayout.CENTER並覆蓋之前添加的Squares。您將需要閱讀Swing GUI提供的所有佈局管理器。

對於例如,從這裏開始:Laying Out Components within a Container

但話雖如此,我會做不同的事情。我會創建一個JPanel並使其能夠繪製多個方塊。例如,如下所示:

import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Rectangle; 
import java.util.ArrayList; 
import java.util.List; 

import javax.swing.*; 

public class GameFrame extends JFrame { 
    public GameFrame() { 
     super("Game Frame"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     Squares squares = new Squares(); 
     getContentPane().add(squares); 
     for (int i = 0; i < 15; i++) { 
     squares.addSquare(i * 10, i * 10, 100, 100); 
     } 

     pack(); 
     setLocationRelativeTo(null); 
     setVisible(true); 

    } 

    public static void main(String[] args) { 
     new GameFrame(); 
    } 

} 

class Squares extends JPanel { 
    private static final int PREF_W = 500; 
    private static final int PREF_H = PREF_W; 
    private List<Rectangle> squares = new ArrayList<Rectangle>(); 

    public void addSquare(int x, int y, int width, int height) { 
     Rectangle rect = new Rectangle(x, y, width, height); 
     squares.add(rect); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(PREF_W, PREF_H); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2 = (Graphics2D) g; 
     for (Rectangle rect : squares) { 
     g2.draw(rect); 
     } 
    } 

} 
+0

好的,這似乎是,有沒有辦法不使用佈局管理器,只使用絕對定位? – wbarksdale 2012-03-15 03:13:23

+0

您可以通過將容器的佈局設置爲「null」,但不推薦。最好做一些像上面的例子。 – 2012-03-15 03:20:54

1

Fo絕對定位,調用setLayout(null)。然後,圖標將被繪製在由getLocation()方法返回的位置,因此您可能首先要調用setLocation()。

+0

您需要做的不止於此,因爲您將全權負責設置組件的位置*和*其大小。但是,這不是建議。 – 2012-03-15 03:22:26

0

即使我在使用絕對佈局(即佈局設置爲null)在jframe上顯示多個矩形時遇到了問題。

JFrame frame = new JFrame("hello"); 
frame.setLayout(null); 
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); 
frame.setSize(600, 600); 
frame.getContentPane().setBackground(Color.red); 
frame.getContentPane().add(new Square(10,10,100,100));//My rectangle class is similar to the Square mentioned in the question and it extends JComponent. 

不過我得到的問題爲矩形沒有displayed.Unless明確設置上坊(JComponent中),它不會work.Even邊界儘管廣場是在構造函數傳遞的位置,的setBounds只解決了問題。所以下面的問題就解決了,這個問題是針對絕對佈局的jframe。

Square square = new Square(10,10,100,100); 
square.setBounds(10,10,100,100); 
frame.getContentPane().add(square);