2012-10-31 32 views
2

我試圖創建多個相同形式的JLabel,然後嘗試將它們添加到相同的JPanel。但是,只有一個JLabel出現,我無法弄清楚爲什麼! 這裏是代碼,我已經寫了:試圖創建多個JLabels,但只有一個出現

final JPanel labelPanel = new JPanel(new BorderLayout()); 
    panel.add(labelPanel, BorderLayout.NORTH); 

    JLabel[] dashedLineLabel = new JLabel[wordLength]; 

    for (int i = 0; i < wordLength; i++) 
    { 
     dashedLineLabel[i] = new JLabel("__ "); 
     dashedLineLabel[i].setFont(new Font("Serif", Font.BOLD, 30)); 
     labelPanel.add(dashedLineLabel[i]); 
    } 

任何幫助將不勝感激! 謝謝

回答

3

您沒有正確使用BorderLayout。標籤全部添加在佈局的中心位置,從而相互覆蓋。改爲使用FlowLayout,或者更好的方法是使用MigLayout

+0

+1對於MigLayout! :) –

+0

+1,MigLayout是最好的。 ;) – brimborium

1

如果您使用BorderLayout並添加簡單的add方法的組件,它們全部添加在中心。如果中心沒有其他容器,他們都在彼此頂部,你可以看到最上面的一個。右鍵使用BorderLayout或使用其他佈局。

documentation of BorderLayout

A border layout lays out a container, arranging and resizing its components to fit in five regions: north, south, east, west, and center. Each region may contain no more than one component, and is identified by a corresponding constant: NORTH, SOUTH, EAST, WEST, and CENTER. When adding a component to a container with a border layout, use one of these five constants, for example: 

    Panel p = new Panel(); 
    p.setLayout(new BorderLayout()); 
    p.add(new Button("Okay"), BorderLayout.SOUTH); 


As a convenience, BorderLayout interprets the absence of a string specification the same as the constant CENTER: 

    Panel p2 = new Panel(); 
    p2.setLayout(new BorderLayout()); 
    p2.add(new TextArea()); // Same as p.add(new TextArea(), BorderLayout.CENTER); 
+0

其實,我認爲他們並不是彼此重疊。如果某個單元格中添加了某些內容,則當前位於該單元格中的組件將被刪除。糾正我,如果我錯了。 – brimborium

+0

@brimborium我一直以爲他們互相排斥,但現在我意識到這只是一個假設。找不到實際發生的任何參考。 –

+1

你可以嘗試一下。只需使用BorderLayout在面板中創建一個框架即可。將兩個標籤添加到該佈局中的相同位置。然後再次刪除第二個,你會看到第一個標籤不再顯示。 – brimborium

2

BorderLayout的規範說

邊框設計,勾畫出一個容器,安排並調整其大小 組件符合下列五個區域:北,南,東,西和 中心。每個區域可以包含不超過一個組分,並且由相應的常數:NORTH,SOUTH,EAST,WEST和 CENTER標識。當here

組件添加到一個邊界佈局的容器,使用 這五個常量之一,....

當你使用的是默認添加方法,添加組件到父母的中心,因此在你的情況下,你看到只有一個組件被添加。

您可以使用其他佈局(即流量或其他)來滿足您的需求。

3

不能使用BorderLayout的,因爲該佈局只有5個組件房間: BorderLayout.CENTERBorderLayout.NORTHBorderLayout.WESTBorderLayout.SOUTHBorderLayout.EAST

解決方案與內置的佈局之一:

我會建議使用FlowLayoutGridLayout,這取決於你想要什麼。您仍然可以使用BorderLayout作爲外部面板,但只需使用上述佈局之一引入內部面板即可。

因此,使用GridLayout時,您會將標籤包裹在網格佈局中,然後將其放入邊框佈局中。您的代碼應該是這樣的:

panel.setLayout(new BorderLayout()); 
final JPanel upperPanel = new JPanel(); 
panel.add(upperPanel, BorderLayout.NORTH); // add some stuff in the north 

final JPanel innerPanel = new JPanel(new GridLayout(1,0)); 
JLabel[] dashedLineLabel = new JLabel[wordLength]; 
for (int i = 0; i < wordLength; i++) { 
    dashedLineLabel[i] = new JLabel("__ "); 
    dashedLineLabel[i].setFont(new Font("Serif", Font.BOLD, 30)); 
    innerPanel.add(dashedLineLabel[i]); 
} 

panel.add(innerPanel, BorderLayout.CENTER); 

解決方案與MigLayout:

如果你不想不同的佈局中進行選擇,也可以使用MigLayout,這是一個第三方的佈局管理器,基本上給你一個經理的所有選擇。你會有更多更乾淨的代碼(imho)。當然,缺點是你必須使用外部jar文件作爲依賴。 (順便說一句:既然我發現了MigLayout,我從來沒有再次使用另一個佈局管理器。)

隨着MigLayout

final JPanel labelPanel = new JPanel(new MigLayout("", "", "")); 
panel.add(labelPanel, "north"); 

JLabel[] dashedLineLabel = new JLabel[wordLength]; 
for (int i = 0; i < wordLength; i++) { 
    dashedLineLabel[i] = new JLabel("__ "); 
    dashedLineLabel[i].setFont(new Font("Serif", Font.BOLD, 30)); 
    panel.add(dashedLineLabel[i], "wrap"); 
} 
相關問題