2012-10-29 41 views
1

我已經有了一個面板(它是一排按鈕),並且它位於一個框架的底部(SOUTH),但是我想在它下面添加兩行(面板/子面板)(一個文本輸入行和輸出線,如果它很重要)。現在我唯一要做的就是聲明並添加更多的面板,這很好,但是當我指定.SOUTH時,它們會覆蓋上一個面板的頂部。如何將面板分成子面板?

編輯:我用

正如特德·霍普提出的解決方案,我說我原來的面板(現在稱爲subPanel1),以及其打算在原來的頂部的兩個新的面板(subPanel2 & subPanel3 ),到第四個「容器面板」(bottomCotainerPanel)。由於我只有三個子面板,因此這允許我指定containerPanel中每個子面板的位置(使用NORTH,CENTER,SOUTH,如果超過3個,可能需要做一些稍微不同的操作),然後指定位置contianerPanel將進入幀(南)。

subPanel1.setLayout(new GridLayout(1,6)); //Layout of subPanel1 
subPanel1.add(clearButton); 
subPanel1.add(customerNameLabel); 
subPanel1.add(customerNameTextField); 
subPanel1.add(showByNameButton); 
subPanel1.add(openNewSavingsButton); 
subPanel1.add(openNewCheckingButton); 


subPanel2.add(sendChatTextField); 
subPanel2.add(sendButton); 
subPanel2.add(clearButton2); 

subPanel3.add(receiveChatTextField); 
subPanel3.add(nextButton); 
subPanel3.add(previousButton); 

bottomContainerPanel.setLayout(new GridLayout(3,1)); //Layout of Container Panel 
bottomContainerPanel.add(subPanel1, BorderLayout.NORTH); 
bottomContainerPanel.add(subPanel2, BorderLayout.CENTER); 
bottomContainerPanel.add(subPanel3, BorderLayout.SOUTH); 

tellerWindow.getContentPane().add(bottomContainerPanel, BorderLayout.SOUTH); 
+0

發佈您的代碼,以便我們可以更好地建議在哪裏進行修改。 – Lipongo

回答

4

您需要添加一個容器面板作爲框架的南面板。容器本身應該有你想要的底部佈局。

+2

針對嵌套面板的+1,適用於[示例](http://stackoverflow.com/a/5630271/230513)。 – trashgod

2

如果您只是想將面板拆分成南北兩個相同的部分,請使用GridLayout。如果你想要中間的東西,你可以使用BorderLayout

如果您想讓您的用戶能夠使用JSplitPane更改子面板尺寸。

0

我有一個類似的問題,試圖將幾行按鈕放入從ListDemo示例中借用的面板中。好了,要做的第一件事就是閱讀有關BorderLayoutHow to Use BorderLayout,或至少看到顯示有圖像:
enter image description here

你不能在BorderLayout多個底部行。但是你可以使用嵌套佈局。我們需要的是BoxLayout,請參閱How to Use BoxLayout
enter image description here

我們只需用按鈕行代替上圖中顯示的按鈕即可。

public class MyStuff extends JPanel { 
    ... 
    public MyStuff() { 
    super(new BorderLayout()); 
    ... 
    JPanel buttonArea = new JPanel(); 
    buttonArea.setLayout(new BoxLayout(buttonArea, BoxLayout.PAGE_AXIS)); 
    add(buttonArea, BorderLayout.PAGE_END); 
    ... 
    //if you dislike the default center alignment: 
    //panelWithButtons1.setAlignmentX(Component.LEFT_ALIGNMENT); 
    buttonArea.add(...);// add the 1st panel with buttons 
    buttonArea.add(...);// add the 2nd panel with buttons 
    buttonArea.add(...);// add the 3rd panel with buttons