So I have a series of jButtons named card1 to card20. I want to change the icon based on whether or not a specific condition has been fulfilled, so I'd like to make a loop and refer to each one as ("card" + i) or something similar instead of writing separate if statements for each button. The code I'm using has been added below, but is there a way to loop this if statement so each loop of the code affects a different card?
private void cardreset() {
if (cardmatch[1] == 0) {
card1.setIcon(back);
}
}
This is what I'd like to do, but adding all of the "card" variables to an array beforehand creates an illegal forward reference error.
private void cardreset() {
for(int i=1; i<=20; i++){
if (cardmatch[i] == 0) {
card[i].setIcon(back);
}
}
}