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

2 回答 2

2

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

1) JFrame 有默认的BorderLayout,如果你放在那里JPanel( add.myPanel;) 那么这个JPanel被放置到CENTER区域并占据整个区域JFrame,并且BorderLayout只有一个JComponent可以放置到具体区域,那么你只会调用(没有删除,任何理由那)

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

2) 最重要的是使用CardLayout放置你的 GUI ,然后你可以很漂亮地忘记你的 GUI 的所有问题

3)更安全的是放置(到JFrame)FatherPanel(从未被移除)并从中移除JComponentsFatherPanel因为如果你要求JFrame#removeAll(),那么你移除了RootPane,并且从你的JFrame停留那里只Borders和你描述的一样

于 2011-10-07T18:17:22.780 回答
0

还有什么?还有:

SwingUtilities.invokeLater(new Runnable() {
                public void run() { ... }
}
于 2011-10-07T17:43:10.323 回答