仅在Event-Dispatch线程中显示最后添加的JPanel到JFrame

时间:2011-10-07 19:22:35

标签: java swing swingworker event-dispatch-thread event-dispatching

我终于只能看到JPanel了。以前的似乎被最后一个取代。为什么是这样?这是我的代码。提前谢谢。

...
class GUIController {



    BaseFrame bf = new BaseFrame(); //is a JFrame




    public void showFrame(final String frameName, final JPanel... panels){

        EventQueue.invokeLater(new Runnable() {
                public void run() {
                     try {
                            Class c;
                            Constructor ctr;
                            c = Class.forName("com.mytunes.client.views.frames." + frameName + "Frame");

                            ctr = c.getConstructor();

                            bf.removeAll();

                            bf = (BaseFrame) ctr.newInstance(); // no issue with getting the JFrame dynamically created!


                            for(JPanel panel:panels){

                                switch(i){
                                    case 1: bf.getContentPane().add(panel,BorderLayout.PAGE_START);

                                        break;

                                         case 2:
                                        bf.getContentPane().add(panel,BorderLayout.CENTER);
                                       break;

                                         case 3:
                                        bf.getContentPane().add(panel,BorderLayout.PAGE_END);
                                        break;

                                }

                            }
                            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);
                        }
           }

       });



    }


    public void showEnterCard(String frame){

        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();
        JPanel p3 = new JPanel();

        showFrame(frame, p1,p2, p3);
    }

}

class Main{
    public static void main(String args[]){
        GUIController c = new GUIController();

        c.showEnterCard("FirstFrame");
    }
}

编辑:(解决方案)

循环更改如下:

for(int i = 1; i <= panels.length; i++){
     switch(i){
        case 1: 
              bf.getContentPane().add(panels[0],BorderLayout.PAGE_START);
        break;
         ...
     }
}

1 个答案:

答案 0 :(得分:2)

您永远不会更改i,它始终为1,因此切换中的case 1:始终为true,并且JPanel始终会添加到同一位置。