尝试创建多个JLabel,但是仅出现一个 [英] Trying to create multiple JLabels, however only one is appearing

查看:209
本文介绍了尝试创建多个JLabel,但是仅出现一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建相同形式的多个JLabel,然后尝试将它们添加到同一JPanel中.但是,只有一个JLabel出现,我不知道为什么! 这是我编写的代码:

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

推荐答案

您没有正确使用BorderLayout.标签全部添加到布局的中心位置,因此彼此覆盖.尝试使用FlowLayout,或者甚至更好地使用 MigLayout .

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.

这篇关于尝试创建多个JLabel,但是仅出现一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆