简体   繁体   中英

Would it be a bad idea to use a For loop to create multiple JLabels that will be part of the implementation of an ActionListener later on?

I am trying to create multiple copies of the same JLabel. Something like,

    int totalLbls = 6;

        JLabel lbl[] = new JLabel[totalLbls];

        for (int i = 0; i <totalLbls;i++){

             lbl[i] = new JLabel("default");
             subPanel.add(lbl[i]);
                }

The labels I am creating will eventually be used to display data. When a JButton "Send" is clicked, these JLabels will display the relevant data which makes use of methods from an External JAR file.

Each JLabel has to display it's own unique data. So would I have to manually create 6 JLabels and manually "map" the data to each JLabel or can this be done with the for loop I have created.

数组实际上是一个很好的主意,数组使处理相当大的一组相同数据变得容易得多,最大的好处是不必命名单个对象。但是如果您希望每个对象都具有一定的大小,则基于所显示的数据无论如何都将必须在循环外部进行操作,但仍然.....没有名字可担心。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM