简体   繁体   中英

Use for-loop index inside variable

I have the following for loop:

for (int i=0; i<5; i++){
    if (someVariable==anotherVariable)
        jPanel(i).remove(anElement);
}

I would like to use the for loop to iterate through the JPanels I have, and remove a certain elements from all of them, my jPanels are named jPanel0, jPanel1,jPanel2....jPanel5

I know the above code doesn't work and I have no idea how to do it. Any help would be appreciated

Instead of creating multiple variables named jPanel0 , jPanel1 ... you should create an array of JPanel[] panels; . Then you can access a specific panel with panels[i] in your loop.

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