Trying to create multiple JLabels, however only one is appearing

ε祈祈猫儿з 提交于 2019-12-20 02:56:16

问题


I am trying to create multiple JLabels of the same form and then trying to add them to the same JPanel. However, only one of the JLabels appears and I can't figure out why! Here is the code that I have written:

    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]);
    }   

Any help would be greatly appreciated! Thank you


回答1:


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.




回答2:


You can not use BorderLayout for that, because that layout has only room for 5 components: BorderLayout.CENTER, BorderLayout.NORTH, BorderLayout.WEST, BorderLayout.SOUTH, BorderLayout.EAST.

Solution with one of the built in layouts:

I would suggest using a FlowLayout or a GridLayout, depending on what you want. You can still use BorderLayout as outer panel, but just introduce an inner panel with one of the above mentioned layouts.

So with a GridLayout, you would wrap your labels in a grid layout and then put this one in your border layout. Your code would look like this:

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);

Solution with MigLayout:

If you don't want to choose between different layouts, you can also use MigLayout, which is a 3rd party layout manager, that basically gives you all the options in one manager. And you will have a lot cleaner code (imho). The disadvantage is of course that you have to use an external jar file as dependency. (By the way: Since I found out about MigLayout, I have never used another layout manager again.)

With 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");
}



回答3:


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.




回答4:


If you use the BorderLayout and add components with the simple add method they are all added in the center. If center has no other container, they are all on top of each other and you can just see the top one. Use BorderLayout right or use another layout.

From the 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);


来源:https://stackoverflow.com/questions/13159216/trying-to-create-multiple-jlabels-however-only-one-is-appearing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!