仅显示事件调度线程中最后添加到 JFrame 的 JPanel

发布于 2024-12-08 20:48:05 字数 3456 浏览 0 评论 0原文

我只能看到最后添加的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;
         ...
     }
}

I can only see the JPanel added at last. Previous ones seem to be replaced by the last one. Why is this? Here's my code. Thanks in advance.

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

EDIT: (solution)

Loop changed as follows:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘴硬脾气大 2024-12-15 20:48:05

您永远不会更改 i,它始终为 1,因此 switch 中的 case 1: 始终为 true,并且 JPanel 始终添加到同一个地方。

you never change i, it is always one, so case 1: in the switch is always true, and the JPanel is always added to the same place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文