내 상황 : JFrame을 확장하는 MainScreen 클래스가 있습니다. JLrame을 확장하는 클래스 GameManager를 추가 한 클래스는 JFrame을 확장하는 MainFrame입니다. 뭔가를 표시하는 데 사용하고 있습니다.getParent()에 null을 반환하는 JLayeredPane
public static void main(String[] args) {
MainScreen ms = new MainScreen();
}
public MainScreen() {
this.initScreen();
this.gm = new GameManager();
this.add(gm, BorderLayout.CENTER);
this.setVisible(true);
}
이제 GameManager 클래스에서 기본 JFrame에 JButton을 추가하려고합니다. 그래서 분명히 작동하지 않는,
JButton button = new JButton("Hello");
this.getParent().add(button, BorderLayout.SOUTH);
를하지만의 getParent은()가 null 반환 : 난 그냥 이렇게 쉽게 될 거라 생각 했어요. 이유는 모르겠지만 (JComponent와 JPanel을 사용하여) 이전과 비슷한 것을 했었습니다. 컨테이너에 추가 될 때마다 모든 JComponent가 컨테이너를 컨테이너로 가질 것이라고 생각했습니다. 내가 놓친 게 무엇입니까?
getParent()를 호출하기 전에 GameManager가 실제로 JFrame에 추가 되었습니까? – Shiro
'this.getParent(). add (button, BorderLayout.SOUTH);'가'GameManager.java'에 있습니까? 생성자 내에서? –
그래, 방금 주목 했어, 그건 바보 같은 실수 야. 하지만 이제 getParent()가 JFrame이 아닌 JPanel을 반환합니다. 어떻게 그렇게됩니까? – Paul