2011-10-07 44 views
0

我在EventDispatch線程中創建一個新框架,並希望稍後添加新面板。但我得到的是一個空白的框架,0高度。但會顯示從內部類中添加的面板。如何添加使用showFirstFrame()? 我只好跟着這樣的做法讓這個問題後:All the Swing frames get "frozen" when wait() is called in Java將外部面板添加到位於EventDispath線程中的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 hi添加了編輯代碼。請看看。希望現在會清楚。 – coder9

+0

對不起,您的示例既不完整也不可編譯。 – trashgod

回答

2

不重新GUI這樣,如果你想只加時賽開關中間人2 JPanelsJFrame那麼你有兩個非常簡單,選項

1)的JFrame具有由defalut BorderLayout,如果你放在那裏JPaneladd.myPanel;),那麼這JPanel被放置到CENTER區域和occupated整個JFrame,並在BorderLayout只有一個JComponent可以放置到具體區域,那麼你將調用只(不刪除,任何理由爲)

myFatherPanel.add(myPanel, BorderLayout.PAGE_START); 
revalidate(); 
repaint(); 

2),最重要的是通過使用CardLayout躺在你的圖形用戶界面,那麼你幾乎可以到忘記你的GUI

3)有關的所有問題,更安全。將放置(到JFrame) FatherPanel(從未已被刪除),並從該FatherPanel刪除JComponents,因爲如果你要求JFrame#removeAll(),然後你刪除RootPane,並從JFrame住宿有隻Borders爲你描述下

+0

這不是關於那個。我的代碼在沒有使用Event Dispatch的情況下完美工作。這是關於事件調度。 – coder9

+0

+1這些是更可靠的方法。如果你想動態加載面板,使用'removeAll()','add()'和'validate()'。 'SwingSet2'和['benojt'](http://sourceforge.net/projects/benojt/)是替代品。 – trashgod

0

什麼?也有:

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

你是什麼意思? – coder9