简体   繁体   English

如何在NetBeans中手动将jlabel添加到jlabel数组?

[英]How to add jlabels to a jlabel array manually in netbeans?

I created a JLabel array: 我创建了一个JLabel数组:

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

How do I add the jlabels to this array in netbeans? 如何在NetBeans中将jlabels添加到此数组?

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. 我从来没有使用过netbeans,所以不知道这是否是您的确切含义(例如某些IDE快捷方式或类似的东西),但是您可以通过将数组中的位置分配给新的JLabel将JLabel添加到Java中的数组。

    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. 最初,您的数组具有null条目。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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