需要帮助从ArrayList中删除JLabel

时间:2016-07-20 16:54:10

标签: java swing arraylist jlabel

我有一个程序,它有一个名为handNumberList的JLabel的ArrayList。方法getHand()用于在创建面板时在程序开头显示JLabel列表。

2
10
15

之后我按下一个按钮,该按钮应该删除ArrayList手(由Integers组成)和handNumberList中的两个组件。当代码运行时,它会从手中移除组件,但是handNumberList没有任何反应,并且面板上的所有内容都保持不变。

public JPanel getHand()
{        
    if (firstUpdate == 0)
    {
        for (int index = 0; index < 9; index++)
        {
            hand.add(index + 1);
        }

        for (int index = 0; index < hand.size(); index++)
        { 
            handNumberList.add(getLabel(Integer.toString(hand.get(index))));
            handNumberList.get(index).setFont(new Font("Serif", Font.PLAIN, 20));

            addComp(handPanel, handNumberList.get(index), 2 * index, 1, 1, 1, 0.5, 0.2,
                  GridBagConstraints.BOTH, GridBagConstraints.NORTHWEST);

        }
    }

    firstUpdate = 1;


    return handPanel;
}

除此之外,似乎一切正常。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

  

面板上的一切都保持不变。

将组件移除(或添加)到可见GUI时,基本代码为:

panel.remove(...);
panel.revalidate();
panel.repaint();
相关问题