开发者

java jlabel array

开发者 https://www.devze.com 2023-02-05 02:50 出处:网络
what\'s wrong with this? the labels[] causes an error JLabel labels[] = new JLabel(); for (int i =开发者_如何学Go0; i < 4; i++) {

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

JLabel labels[] = new JLabel();
    for (int i =  开发者_如何学Go0; i < 4; i++) {
       labels[i] = new JLabel("Label" + i);
       panel.add(labels[i]);
 }


JLabel label = new JLabel(); 

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

JLabel labels[] = new JLabel[4];


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();
}


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]);
}


there is a problem with declaring JLabel array,

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

精彩评论

暂无评论...
验证码 换一张
取 消