2
我有一个主类,其中我所有的gui组件都被称为Math.java。在一个单独的类(Calc)中,我得到了所有的组件,并将它们保存在Calc的构造函数的Math中的本地组件中。唯一不起作用的是当我尝试获取其中一个组件的父组件时,它总是返回null。当我直接访问Math中的JLabels时,它可以工作。Swing:从另一个类获取组件的父项
cards []是一个JLabels数组。 panel2a和图层是JPanel。
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();
}
}
}
“我得到所有组件并将它们保存在Math的构造函数中的本地组件中计算器“。显示此 –
@ rasen58向我们展示一些代码。很可能,您正在访问这些组件的不同实例。 –