Trying to create multiple JLabels, however only one is appearing

后端 未结 4 1211
时光说笑
时光说笑 2021-01-20 02:23

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!

相关标签:
4条回答
  • 2021-01-20 02:29

    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);
    
    0 讨论(0)
  • 2021-01-20 02:40

    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.

    0 讨论(0)
  • 2021-01-20 02:47

    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");
    }
    
    0 讨论(0)
  • 2021-01-20 02:49

    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.

    0 讨论(0)
提交回复
热议问题