开发者

Problem regarding loops. I need to access 10 labels through a loop in java?

I开发者_如何学Go have a problem regarding loops. I need to access 10 labels which have names like label1, label2, label3 .... etc. I need to know whether I can access those labels by going through a loop in java?


How about using List or an array

List<JLabel> labels = new ArrayList<JLabel>();
labels.get(index);


Change those labels to be an array, and access it using an index.

For example:

JLabel[] labels = new JLabel[10];
for (int i = 0; i < labels.length; ++i) {
    labels[i] = new JLabel("Label " + i);
}
for (int i = 0; i < labels.length; ++i) {
    // access each label.
}


Put your labels in to LinkList or array Then you can access those array or linkList on a loop


If you cannot change the labels names / put them into an array you can make an array of references to the labels and fill it at the beginning of your program with the list of your labels.


'Access to labels' is kinda vague. Are you referring to different instances of java.awt.label? If so you can simply loop over them when they're in a list with a for-each statement.


If you are talking about Java labels, you can use a switch statement instead. If you are talking about objects such as a JLabel, use an array or ArrayList.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜