Removing JLabels with the "same" name
I'm doing a 4 in a row game, and i'm adding the red or green piece as a label, but always the same label, to the panel game. My code is like this (the relevant one)
labeljogo = new JLabel(new ImageIcon( bola ));
if(f.getSource()==setaL1开发者_如何学C && c1==6 && modoDeJogo==2 || setaPc=="setaL1" && c1==6 && modoDeJogo==3 && jogadorActual==2 || f.getSource()==setaL1 && c1==6 && modoDeJogo==3 && jogadorActual==1 || modoDeJogo==4 && setaPc=="setaL1" && c1==6)
{
labeljogo.setBounds(150, 100, 60, 60);
c1++;
matrix[1][0]=jogadorActual;
}
if(f.getSource()==setaL1 && c1==6 && modoDeJogo==2 || setaPc=="setaL1" && c1==6 && modoDeJogo==3 && jogadorActual==2 || f.getSource()==setaL1 && c1==6 && modoDeJogo==3 && jogadorActual==1 || modoDeJogo==4 && setaPc=="setaL1" && c1==6)
{
labeljogo.setBounds(150, 100, 60, 60);
c1++;
matrix[1][0]=jogadorActual;
}
(...) and it continues until all the 8*8 pieces are in the panel
the image bola changes by changing player.
My doubt is if there is any way of cleaning all this JLabels that all have the same name. Because when I call the
panel.remove(labeljogo);
It only removes the last one.
Is there any way of removing this JLabels without affecting the other JLabels?
for(int i=0; i<Main.panel.getComponentCount (); i++)
if(Main.panel.getComponent(i).getBounds ( ).height==60)
Main.panel.getComponent(i).setVisible(false);
used this method to remove the JLabel
s by height. It worked fine.
精彩评论