仅显示事件调度线程中最后添加到 JFrame 的 JPanel
我只能看到最后添加的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 can only see the JPanel added at last. Previous ones seem to be replaced by the last one. Why is this? Here's my code. Thanks in advance.
...
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");
}
}
EDIT: (solution)
Loop changed as follows:
for(int i = 1; i <= panels.length; i++){
switch(i){
case 1:
bf.getContentPane().add(panels[0],BorderLayout.PAGE_START);
break;
...
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您永远不会更改
i
,它始终为 1,因此 switch 中的case 1:
始终为 true,并且JPanel
始终添加到同一个地方。you never change
i
, it is always one, socase 1:
in the switch is always true, and theJPanel
is always added to the same place.