我在 EventDispatch 线程中创建一个新框架,并希望稍后向其中添加新面板。但我得到的只是一个空白框,高度为 0。但会显示从内部类内部添加的面板。如何使用 showFirstFrame() 添加? 遇到这个问题后我不得不遵循这样的方法:All the Swing frames get "frozen" when wait() is called in Java
我一直在引用这个教程: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");
}
}
最佳答案
如果您只想在两个之间切换,请不要以这种方式重新创建 GUI JPanels里面JFrame那么你有两个非常简单的选择
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(从未被删除)并从此 FatherPanel
删除 JComponents
>,因为如果您调用 JFrame#removeAll()
,那么您就删除了 RootPane
,并且 JFrame
中仅保留 Borders
与您描述的相同
关于java - 从外部向位于 EventDispath 线程中的 JFrame 添加附加面板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7690856/