gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:09:19 25 4
gpt4 key购买 nike

我只能看到最后添加的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,它始终是1,因此开关中的case 1:始终为true,并且JPanel始终为true添加到同一位置。

关于java - 仅显示事件调度线程中最后添加到 JFrame 的 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7691942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com