모든 클래스에는 Math.java라는 메인 구성 요소가 있습니다. 별도의 클래스 (Calc)에서는 모든 구성 요소를 가져 와서 Calc의 생성자에서 Math의 로컬 구성 요소에 저장합니다. 유일한 방법은 구성 요소 중 하나의 부모 구성 요소를 가져 오려고 할 때 항상 null을 반환하는 것입니다. Math의 JLabel에 직접 액세스하면 작동합니다.스윙 : 다른 클래스에서 구성 요소의 부모 가져 오기
cards []는 JLabel의 배열입니다. 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();
}
}
}
"모든 구성 요소를 가져 와서 생성자의 Math에서 로컬 구성 요소에 저장합니다. Calc. " 이것을 보여주십시오 –
@ rasen58 우리에게 몇 가지 코드를 보여주십시오. 대부분 해당 구성 요소의 다른 인스턴스에 액세스하고 있습니다. –