3

我正在尝试创建多个相同形式的 JLabel,然后尝试将它们添加到同一个 JPanel。但是,只有一个 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]);
    }   

任何帮助将不胜感激!谢谢

4

4 回答 4

4

You aren't using the BorderLayout properly. The labels are all added at the center location of the layout, and thus overwrite each others. Try a FlowLayout instead, or even better, a MigLayout.

于 2012-10-31T13:38:57.563 回答
3

您不能使用BorderLayout它,因为该布局只有 5 个组件的空间: BorderLayout.CENTERBorderLayout.NORTHBorderLayout.WESTBorderLayout.SOUTHBorderLayout.EAST

具有内置布局之一的解决方案:

我建议使用 aFlowLayout或 a GridLayout,这取决于你想要什么。您仍然可以使用 BorderLayout 作为外面板,但只需引入具有上述布局之一的内面板。

因此,使用 a 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,这是一个 3rd 方布局管理器,它基本上为您提供了一个管理器中的所有选项。而且您将拥有更清晰的代码(恕我直言)。缺点当然是您必须使用外部 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");
}
于 2012-10-31T13:57:09.760 回答
2

BorderLayout specification says

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,....

in here

As you are using the default add method, it adds components to the center of the parent and thus in your case you see only one component being added.

You can use other layout (i.e. flow or some other) to satisfy your need.

于 2012-10-31T13:46:02.910 回答
1

如果使用BorderLayout简单的方法使用 and 添加组件,add它们都将添加到中心。如果 center 没有其他容器,它们都在彼此之上,你可以看到最上面的一个。正确使用 BorderLayout 或使用其他布局。

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);
于 2012-10-31T13:41:41.560 回答