从外部向位于 EventDispath 线程中的 JFrame 添加附加面板?

发布于 2024-12-09 03:10:10 字数 5558 浏览 0 评论 0原文

我在 EventDispatch 线程中创建一个新框架,并希望稍后向其中添加新面板。但我得到的只是一个空白框,高度为 0。但会显示从内部类内部添加的面板。如何使用 showFirstFrame() 添加? 遇到此问题后,我必须遵循这样的方法: 当在 Java 中调用 wait() 时,所有 Swing 框架都会“冻结”

我一直在参考本教程: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");
    }
}

I create a new frame in EventDispatch thread and want to add new Panels to that later on. But all i get is a blank frame, with 0 height. But panels added from inside the inner class are displayed. How to add using showFirstFrame()?
I had to follow such an approach after getting this issue: All the Swing frames get "frozen" when wait() is called in Java

I've been referring to this tutorial: http://leepoint.net/JavaBasics/gui/gui-commentary/guicom-main-thread.html

Thanks in advance.

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

    }
}

EDIT:

...

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

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

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

发布评论

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

评论(2

美胚控场 2024-12-16 03:10:10

如果您只想在两个 JPanel 之间切换,请不要以这种方式重新创建 GUI 里面 JFrame 那么你有两个非常简单的选择

1) JFrame 默认有 BorderLayout,如果你把有 JPanel (add.myPanel;) 然后这个 JPanel 被放置到 CENTER 区域并占据整个JFrame,并且在 BorderLayout 中,只能将一个 JComponent 放置到具体区域,那么您将仅调用(不删除,任何原因) )

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

2) 最重要的是使用 CardLayout,然后你就可以忘记了GUI 的所有问题

3) 更安全的方法是放置(到 JFrame)FatherPanel(从未被删除)并从此 FatherPanel< 中删除 JComponents /code>,因为如果您调用 JFrame#removeAll(),那么您就删除了 RootPane,并且 JFrame 中仅保留 >边框和你描述的一样

don't recreate GUI this way, if you want ot only switch betweens two JPanels inside JFrame then you have two very simple choises

1) JFrame has by defalut BorderLayout, and if you put there JPanel (add.myPanel;) then this JPanel is placed to the CENTER area and occupated whole JFrame, and in BorderLayout only one JComponent could be placed to the concrete area, then you will call only (no remove, any reason for that)

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

2) and best of all would be lay your GUI by using CardLayout, then you can pretty to forgot about all problems with your GUI

3) more safer would be to place (to the JFrame) FatherPanel (that never has been removed) and remove JComponents from this FatherPanel, because if you calling for JFrame#removeAll(), then you removed RootPane, and from your JFrame stays there only Borders same as you decribed

倒数 2024-12-16 03:10:10

还有什么?还带有:

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

What else? Also with:

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