2016-10-12 6 views
1

다른 클래스의 JFrame을 추가하는 데 문제가 있습니다. 런타임시 JFrame을 초기화하고 생성합니다.자바 다른 클래스의 JFrame에 JPanel 추가하기

public static void main(String[] args) throws IOException { 
    Testing exc = new Testing(); 
    exc.MessagingGUI(); 
    Update.Server(); 

    Scanner input = new Scanner(System.in); 
    String some_text = input.nextLine(); 
    exc.AddPanel(); 
} 
public void MessagingGUI() throws IOException { 
    JPanel welcome = new JPanel(); 
    JLabel title = new JLabel("Stuff"); 
    welcome.add(title); 
    tab.add("Home", welcome); 
    pane.add(tab); 

    Messaging.setTitle("Messaging"); 
    Messaging.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    Messaging.setContentPane(pane); 
    Messaging.setVisible(true); 
    Messaging.pack(); 
    System.out.println("Done"); 
} 

그리고 다른 클래스에서 실행됩니다.

public class Update { 
    public static void Server() { 
     System.out.println("Hello"); 
     Testing exc =new Testing(); 
     exc.AddPanel(); 
    } 
} 

는 지금은 업데이트 클래스는 JFrame의에 액세스 할 수 없습니다하지만 난 이유를 모르기 때문에 그것의 것을 알고있다. AddPanel 메서드가 아니기 때문에 텍스트를 입력하면 AddPanel 메서드를 실행할 수 있기 때문에 작동합니다. 그래서 누군가가 어떻게 그리고 왜 Update 클래스가 JFrame을 편집 할 수 없는지 설명 할 수 있습니다. 감사합니다.

+0

Google에서 도와 드릴 수있는 충분한 코드를 게시하지 않았습니다. 아마도'Testing'에있는 AddPanel 메소드를 게시 할 수 있습니까? – ControlAltDel

답변

1

업데이트 클래스에 표시된 GUI에 대한 충분한 참조를 제공하지 않았기 때문입니다. 당신이 업데이트 인스턴스를 만들 때

public class Update { 
    private Testing exc; 

    public Update(Testing exc) { 
     this.exc = exc; 
    } 

    // this shouldn't be static 
    public void server() { 
     System.out.println("Hello"); 
     // Testing exc =new Testing(); // Nope! 
     exc.addPanel(); // rename to addPanel to comply with Java naming rules 
    } 
} 

에서, 에 대한 참조를 전달 테스트 GUI 시각화 : 당신은 당신의 서버 방법은 비 정적하고 업데이트 생성자로 테스트 GUI에 대한 유효한 참조 전달해야 . 그런 다음 더 이상 정적이 아니므로 이 아닌이 아닌 Update 인스턴스에서 서버 메서드를 호출합니다.

+0

서버 메소드를 어떻게 호출할까요? 문제가있는 코드를 보여줄 수 있습니까? 어쨌든 위대한 솔루션을 주셔서 감사합니다 – Nightfortress

+0

지금은 문제를 해결하지 않았습니다. 도움을 주셔서 감사합니다 – Nightfortress

+0

@ Nightfortress : 좋은, 다행 당신의 문제를 해결했습니다. –

관련 문제