java jlabel 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4704278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:36:46  来源:igfitidea点击:

java jlabel array

java

提问by jjj

what's wrong with this? the labels[] causes an error

这有什么问题?标签 [] 导致错误

JLabel labels[] = new JLabel();
    for (int i =  0; i < 4; i++) {
       labels[i] = new JLabel("Label" + i);
       panel.add(labels[i]);
 }

回答by

JLabel label = new JLabel(); 

initialize a single Jlabel if you want to initialize array you should do like that

如果要初始化数组,请初始化单个 Jlabel,您应该这样做

JLabel labels[] = new JLabel[4];

回答by lweller

JLabel labels[] = new JLabel[4];

and then you have to create new instances for each array entry (otherwise array contains only nulls)

然后你必须为每个数组条目创建新实例(否则数组只包含空值)

for(JLabel label : labels) {
  label = new JLabel();
}

回答by Nipuna

First of all you must define the array. Then you can play with the methods in it.

首先,您必须定义数组。然后你可以使用其中的方法。

Labels = new JLabel[]{ label1, label2, label3 };

for(int i=0; i<Labels.length; i++){
         add(Labels[i]);
}

回答by Yassine Klilich

there is a problem with declaring JLabel array,

声明 JLabel 数组有问题,

JLabel labels[] = new JLabel(); //Incorrect code
JLabel[] labels = new JLabel[enter the size]; //Correct One