2012-10-31 35 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"); 
} 
相关问题