2011-10-07 3 views
0

EventDispatch 스레드에서 새 프레임을 만들고 나중에 새 패널을 추가하려고합니다. 하지만 내가 얻은 것은 높이가 0 인 빈 프레임입니다. 그러나 내부 클래스 내부에서 추가 된 패널이 표시됩니다. showFirstFrame()을 사용하여 추가하는 방법? 나는이 문제를받은 후 이러한 접근 방식을 따르도록했다 :이 자습서를 참조했습니다 All the Swing frames get "frozen" when wait() is called in JavaEventDispath 스레드에있는 JFrame에 외부 패널을 추가하고 있습니까?

: http://leepoint.net/JavaBasics/gui/gui-commentary/guicom-main-thread.html

사전에 감사합니다.

public class GUIController { 
    JFrame bf; 
    JFrame tempFrame; 

    public JFrame showFrame(){ 
     SwingUtilities.invokeLater(new Runnable() { 
        public void run() { 
          try { 
           Class c; 
           Constructor ctr; 
           c = Class.forName("SomeJFrame"); 
           ctr = c.getConstructor(); 
           GUIController.this.bf.removeAll(); 
           GUIController.this.bf = (BaseFrame) ctr.newInstance(); 
           GUIController.this.bf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
           GUIController.this.bf.pack(); 
           GUIController.this.bf.setVisible(true); 

         } catch (InstantiationException ex) { 
          Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex); 
         } catch (IllegalAccessException ex) { 
          Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex); 
         } catch (IllegalArgumentException ex) { 
          Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex); 
         } catch (InvocationTargetException ex) { 
          Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex); 
         } catch (NoSuchMethodException ex) { 
          Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex); 
         } catch (SecurityException ex) { 
          Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex); 
         } catch (ClassNotFoundException ex) { 
          Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex); 
         } 

       } 
      }); 

    return GUIController.this.bf; 
} 


public void showFirstFrame(){ 
     tempFrame = showFrame(); 
     tempFrame .getContentPane().add(headerPanel, BorderLayout.PAGE_START); 
      tempFrame .getContentPane().add(new EnterSomePanel(), BorderLayout.CENTER); 
      tempFrame .getContentPane().add(footerPanel, BorderLayout.PAGE_END); 
      tempFrame .setVisible(true); 

    } 
} 

편집 :

... 

class GUIController { 


    HeaderPanel headerPanel = new HeaderPanel(); // extends JPanel 
    FooterPanel footerPanel = new FooterPanel(); 
    BaseFrame bf = new BaseFrame(); // extends JFrame 

    public BaseFrame showFrame(String frameName){ 


     try { 
         Class c; 
         Constructor ctr; 
         c = Class.forName("some.dynamically.loaded.JFrame" + frameName); 
         ctr = c.getConstructor(); 
         bf = (BaseFrame) ctr.newInstance(); 
         bf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
         bf.pack(); 
         bf.setVisible(true); 

        } catch (InstantiationException ex) { 
         Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex); 
        } catch (IllegalAccessException ex) { 
         Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex); 
        } catch (IllegalArgumentException ex) { 
         Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex); 
        } catch (InvocationTargetException ex) { 
         Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex); 
        } catch (NoSuchMethodException ex) { 
         Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex); 
        } catch (SecurityException ex) { 
         Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex); 
        } catch (ClassNotFoundException ex) { 
         Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex); 
        } 

     return bf; 

    } 


    public void showFirstFrame(final String frame){ //some controller will pass a frame name to this 

     bf = showFrame(frame); 

     SwingUtilities.invokeLater(new Runnable() { 
       public void run() { 
        bf.getContentPane().add(headerPanel, BorderLayout.PAGE_START); 
        bf.invalidate(); 
        bf.validate(); 
        System.out.println("test"); 
       } 
     }); 


    } 
} 

class Main{ 
    public static void main(String args[]){ 
     GUIController c = new GUIController(); 
     c.showFirstFrame("FirstFrame"); 
    } 
} 
+0

는 [sscce] 주시기 바랍니다 설명한 기능과 동일한 HTTP : //sscce.org/)에 문제가 있음을 보여줍니다. 기술하다. – trashgod

+0

@trashgod 안녕하세요, EDITED 코드를 추가했습니다. pls 봐. 이제는 분명해질 수 있기를 바랍니다. – coder9

+0

죄송합니다. 귀하의 예는 완전하지도 않고 편집 할 수도 없습니다. – trashgod

답변

2

만 스위치 오티하려면 다음 두 가지 매우 간단한 choises

이 두 JPanels JFrame 내부 betweens, 이런 식으로 GUI를 다시하지 않는

1) JFrame의는 defalut BorderLayout가 있으며, 경우 당신은

다음이 JPanelCENTER 영역에 배치 JPanel ( add.myPanel;)가 넣어 전체 JFrame을 occupated, 단 하나의 JComponent 콘크리트 영역에 배치 될 수 BorderLayout에, 당신은 단지 (더 제거, 그것에 대한 어떤 이유)를 호출하지 않습니다
myFatherPanel.add(myPanel, BorderLayout.PAGE_START); 
revalidate(); 
repaint(); 

2) 모든의 가장 CardLayout를 사용하여 GUI를 마련 할 것은, 당신은 꽤하려면) 더 안전한는 JFrame로 (배치하는 것입니다 귀하의 GUI

3)에 대한 모든 문제를 잊었 수 있습니다 FatherPanel (제거되지 않은 결코) 및 JFrame#removeAll()을 요구하는 경우, 당신은 RootPane을 제거하기 때문에,이 FatherPanel에서 JComponents를 제거하고 거기 JFrame 숙박 만 Borders (당신이

+0

이것은 그것에 관한 것이 아닙니다. 내 코드는 Event Dispatch를 사용하지 않고도 완벽하게 작동합니다. 이것은 Event-dispatching에 관한 것입니다. – coder9

+0

+1 이들은보다 신뢰할 수있는 접근 방식입니다. 패널을 동적으로로드하려면'removeAll()','add()'및'validate()'를 사용하십시오. 'SwingSet2'와''benojt' (http://sourceforge.net/projects/benojt/)가 대안입니다. – trashgod

0

그 밖의 무엇? 또한과 :

SwingUtilities.invokeLater(new Runnable() { 
       public void run() { ... } 
} 
+0

무엇을 의미합니까? – coder9

관련 문제