0
I’m developing a game with JLabel from 1 to 50, I can access them online without having to use a switch.
You can verify that what changes in the code and only the lblBoard1 or lblBoard2 and so on, who will determine the JLabel eh the variable points.
There is no problem in the code below, I just want to use something like (I’ll make it up because this is exactly what I want) lblBoard[points].setIcon. One more detail as I created these JLabel in drawing time I didn’t see how to create an array of them in the netbeans interface. What I really have are Jlabel’s ending with numbers lblBoard1, lblBoard2 and so on.
switch (points){
case 1:{
if (samePlace){
lblBoard1.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay12) ) ));
} else {
if (player == "Player 1"){
lblBoard1.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay1) ) ));
} else {
lblBoard1.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay2) ) ));
}
}
break;
}
case 2:{
if (samePlace){
lblBoard2.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay12) ) ));
} else {
if (player == "Player 1"){
lblBoard2.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay1) ) ));
} else {
lblBoard2.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay2) ) ));
}
}
break;
}
case 3:{
if (samePlace){
lblBoard3.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay12) ) ));
} else {
if (player == "Player 1"){
lblBoard3.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay1) ) ));
} else {
lblBoard3.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay2) ) ));
}
}
break;
}
If you clarify more the question this is the screen of my game. I wanted to move the players house by house. It is run by swicth but the code is not over. 51 Labels.


What problem are you having ?
switch caseno need to open and close{}because there already exists thebreak.– NoobSaibot
Maybe I haven’t been clear, I don’t have code problem. I would like to use something like lblBoard[i]. setIcon to help me not have to make the switch case.
– Adriano Menezes
still thanks for the tip...
– Adriano Menezes
Please provide a [mcve] so that it is possible to test the code.
– user28595