2012-02-07 156 views
0


我正在試圖做出簡單的遊戲佈局。 我從容器中進行佈局 - 所有容器都與我的示例相似(它表示JPanel,setlayout,添加組件,返回JPanel)。
GridBagLayout麻煩

整個佈局的結構:(這3個在主佈局中)
upper - BorderLayout.PAGE_START; // menu
center - BorderLayout.PAGE_CENTER; // centerContainer()
bottom - BorderLayout.PAGE_END; // statusBar - 只是包含文本的容器


這3個容器的放置工作正常,但問題是放置在centerContainer中。
中心容器結構:3個容器 - aboutServerContainer,aboutGameContainer,gameContainer。

gameContainer有大小450x450

我想在同一高度serverInfo, 下gameContainer和的GameInfo開始serveInfo但它在某種程度上cernter的serverInfo和的GameInfo是烏德,但它也使gameContainer下的自由空間(我不希望有任何的自由空間在這裏。)

enter image description here

private Container centerContainer() { 
     JPanel centerJPanelJP = new JPanel(); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     stredniJPanelJP.setLayout(new GridBagLayout()); 

     //gbc.fill = GridBagConstraints.BOTH; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.gridheight = 1; 
     centerJPanelJP.add(aboutServerContainer(),gbc); 

     gbc.gridheight = 1; 
     gbc.gridy = 1; 
     centerJPanelJP.add(aboutGameContainer(),gbc); 

     gbc.gridheight = 2; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     centerJPanelJP.add(gamePanelContainer(),gbc); 

     return centerJPanelJP; 
    } 
+1

這真的是你的代碼嗎? 'gridx'總是0嗎? – 2012-02-07 15:07:55

+1

請發佈http://sscce.org/展示您的問題 – mKorbel 2012-02-07 15:10:12

+0

@ TomHawtin-tackline是的,但它可能是錯誤的 - 我可能有一些問題,理解使用gridx,gridy – user1097772 2012-02-07 15:21:51

回答

3

它看起來像你試圖實現這樣的佈局:

+-----+-----+ 
| A |  | 
+-----+ C | 
| B |  | 
+-----+-----+ 

爲了實現這一目標,您電網約束應該爲:

| x y width height 
--+--------------------- 
A | 0 0 1  1 
B | 0 1 1  1 
C | 1 0 1  2 

在我看來,最好是堅持使用一個「強大」的佈局管理器,而不是應付特殊情況下,嵌套板和襯砌事物的僵化。

+0

這正是我需要的。謝謝。你是什​​麼「強大的」佈局經理? – user1097772 2012-02-07 15:41:32

0

爲什麼不能是這樣的:

有一個主BroderLayout

  • 添加沒什麼PAGE_START
  • 附加中心集裝箱中心
  • 添加VerticalPanel到LINE_START -in垂直面板添加serverInfo -in垂直面板添加的GameInfo -add狀態bar to PAGE_END
+0

我不想添加任何額外的容器主要容器。我想編輯我的中心容器.. – user1097772 2012-02-07 15:27:44