简体   繁体   中英

How to add jlabels to a jlabel array manually in netbeans?

I created a JLabel array:

private JLabel label[]=new JLabel[10];

How do I add the jlabels to this array in netbeans?

Try this:-

 JLabel[] labels = new JLabel[10];

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

I've never used netbeans so I'm not sure if this is what you mean exactly (like some IDE shortcut or something), but you can add JLabels to an array in java by assigning the location in the array to a new JLabel.

    JLabel[] label = new JLabel[10];

assigning them manually...

    label[0] = new JLabel("text");

or all at once...

     for(int i = 0; i < label.length; i++) {
         label[i] = new JLabel("text");
     }

You don't add elements to array, it is not dynamic, you can just update the entries. Initially your array has null entries.

JLabel label1 = new JLabel("Okocha");
label[0] = label1;

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