2014-05-07 68 views
1

Im將JFrame的內容窗格設置爲我創建的面板。該面板爲其後面板使用Grid Bag Layout。我把這個佈局像這樣:用面板填充內容窗格

private void initPanel() { 

    setLayout(new GridBagLayout()); 
    constraints = new GridBagConstraints(); 
    populateGUI(); 
} 

這將導致「遊戲」喜歡這樣的:

enter image description here

什麼實際上,我試圖做的是擴大這些面板(微小正方形)。 JFrame的尺寸爲800 x 500,因此每個面板應該佔用100 x 100,共40個面板。問題在於面板太小了。

我填充JFrame的內容窗格(通過自己的面板更換 - 紅色標記)使用此代碼:

private void populateGUI() { 

    // Fill the frame with panels 
    for (int rowPos = 0; rowPos < numColumns; rowPos++) { 

     for (int colPos = 0; colPos < numRows; colPos++) { 

      constraints.gridx = rowPos; 
      constraints.gridy = colPos; 

      // Add component 
      add(new GameDummyPanel(gameUtil.generateRandomColour(), 
        panelWidth, panelHeight), constraints); 
     } 
    } 
} 

注 - 這些都是我實現的唯一約束。在GameDummyPanel類中,我使用以下設置大小:

public GameDummyPanel(Color panelColor, int panelWidth, int panelHeight) 
{ 
    dummyPanelWidth = panelWidth/8; 
    dummyPanelHeight = panelHeight/5; 

    setBackground(panelColor); 
    setSize(new Dimension(dummyPanelWidth, dummyPanelHeight)); 
} 

那麼爲什麼這不被執行?它不像我在面板和作爲JFrame的內容窗格的面板上調用pack。

感謝

更新

審查和執行什麼需要做的答案 - 你不應該想下面看 - 是的約束上的fill屬性設置爲BOTH並刪除所有基於面板類的基於尺寸的屬性。這意味着我的約束是這樣的:

constraints = new GridBagConstraints(); 
    constraints.weightx = 1.0; 
    constraints.weighty = 1.0; 
    constraints.fill = GridBagConstraints.BOTH; 

減去各自成立gridxgridy面板的真正獨特的定位。那麼JPanel的實現可能實際上被淘汰,並且讓JPanel在運行中剛剛創建。

+0

這個帖子可能會幫助:HTTP: //stackoverflow.com/questions/1783793/java-difference-between-the-setpreferredsize-and-setsize-methods-in-compone –

回答

2

嘗試使用GridBagConstraints的更多屬性。

constraints.weightx=1.0; 
constraints.weighty=1.0; 
constraints.fill=GridBagConstraints.BOTH; 

OR

使用GridLayout而不是GridBagLayout在這種情況下。

private void initPanel() { 
    setLayout(new GridLayout(numRows, numColumns)); 
    populateGUI(); 
} 

enter image description here

2

設置以下值到面板:除非你至少指定一個非零的weightx或沉重的價值

constraints.weightx = 1.0; 
constraints.weighty = 1.0; 

http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

,所有組件在中心聚集在一起的容器。這是因爲當權重爲0.0(默認值)時,GridBagLayout會在其網格單元格和容器邊緣之間放置任何額外的空間。

而且你不應該手動設置面板的尺寸,讓GridBagLayout中做的工作,只是配置約束,以自己的喜好,對於簡單的佈局,你至少應該考慮以下變量:

gridx, gridy 
fill 
anchor 
weightx, weighty 

他們都在前面的鏈接中解釋過。

+2

With out'使用'constraints.fill = GridBagConstraints.BOTH;'輸出將會不同。 – Braj

+0

@Braj這是正確的,我只是給他的問題的核心罪犯,從那時起,他應該定製他的佈局,以滿足他的需求。因爲據我們所知,一個GridLayout會更適合你,正如你所指出的那樣。此外,他正在強制面板的大小,所以他可能會得到足夠好的東西。 – DSquare

+0

你說的話和設置填充屬性的組合做了訣竅。如果沒有填充屬性,面板將會很小。 – Katana24

1
  • 做在面板上沒有通話setSize()

  • 是否覆蓋面板的getPreferredSize()GridBagLayout尊重優選尺寸。如果你不覆蓋,它會有你的體驗。

    @Override 
    public Dimension getPreferredSize() { 
        return new Dimension(SIZE, SIZE); 
    } 
    
  • 呼叫frame.pack()所以preferredSize受到尊重(不要設置幀大小)

  • 看到Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

+0

感謝您的回答 - 您的建議是否超出了最佳實踐?如果是的話,我可以在哪裏閱讀關於它們 – Katana24

+0

是...................... –

+0

所以沒有資源.............? – Katana24