メイン クラスがあり、すべての GUI コンポーネントが Math.java という名前で作成されています。別のクラス (Calc) で、すべてのコンポーネントを取得し、Calc のコンストラクターで Math からローカル コンポーネントに保存します。動作しない唯一のことは、コンポーネントの 1 つの親コンポーネントを取得しようとすると、常に null が返されることです。Math で JLabels に直接アクセスすると、機能します。
cards[] は JLabels の配列です。panel2a と layer は JPanels です。
public void clear()
{
for(int i =0;i <cards.length; i++)
{
//this works, calling the components directly
if(math.cards[i].getParent().equals(math.panel2a) )
{
math.panel2a.remove(cards[i]);
layer.add(cards[i]);
layer.repaint();
}
//this doesn't work, using the local components
if(cards[i].getParent().equals(panel2a) )
{
panel2a.remove(cards[i]);
layer.add(cards[i]);
layer.repaint();
}
}
}