使用“相同”名称删除JLabel

时间:2022-03-05 16:50:53

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)

我正在做一个连续4场比赛,我正在添加红色或绿色作为标签,但总是与面板游戏相同的标签。我的代码是这样的(相关的)

labeljogo = new JLabel(new ImageIcon( bola ));

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;
    }
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

(...)并继续直到所有8 * 8件都在面板中

the image bola changes by changing player.

图像bola通过改变玩家而改变。

My doubt is if there is any way of cleaning all this JLabels that all have the same name. Because when I call the

我怀疑的是,是否有任何方法可以清理所有具有相同名称的JLabel。因为我打电话的时候

 panel.remove(labeljogo);

It only removes the last one.

它只删除最后一个。

Is there any way of removing this JLabels without affecting the other JLabels?

有没有办法在不影响其他JLabel的情况下删除这些JLabel?

1 个解决方案

#1


0  

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 JLabels by height. It worked fine.

使用此方法按高度删除JLabel。它工作正常。

#1


0  

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 JLabels by height. It worked fine.

使用此方法按高度删除JLabel。它工作正常。