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内で2つのJPanelを切り替えるだけの場合は、2つの非常に単純な選択肢があります。

1)JFrameはdefalut BorderLayoutを持っており、そこにJPaneladd.myPanel;)を置くと、これJPanelはそのCENTER領域に配置され、全体を占有JFrameします。具体的な領域に配置できるのはBorderLayout1つだけで、呼び出しのみ(削除なし、理由はありません)JComponentそれ)

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

2)そして何よりも、 CardLayoutを使用してGUIを配置することで、GUIに関するすべての問題をかなり忘れることができます。

3)より安全なのは、(削除されたことのないJFrame)FatherPanelを配置し、これから削除することです。JComponentsFatherPanelJFrame#removeAll()RootPaneJFrameBorders

于 2011-10-07T18:17:22.780 に答える
0

ほかに何か?また:

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