Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to create multiple JLabels, however only one is appearing

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

like image 566
user1788673 Avatar asked Jan 29 '26 18:01

user1788673


1 Answers

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.

like image 151
Emmanuel Bourg Avatar answered Feb 01 '26 07:02

Emmanuel Bourg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!