【发布时间】:2013-06-01 10:58:56
【问题描述】:
我正在研究大型项目。如您所见,我有一个主要的 JFrame 和大约 20 个菜单项。每个菜单项必须弹出一个新窗口。一开始我创建了一个 JLayeredPanel,然后我将每个菜单项分配给 JFrame 内的一个 JPanel。然后我在 JLayeredPanel 中放置了 25 个面板...默认所有面板都设置为不可见,例如:
panel1.setVisible(false);
panel2.setVisible(false);
等等
当用户单击一个菜单项时,它的 JPanel 将可见,而其余的则不可见。它看起来很乱,我有 5000 行代码。我使用了 InternalFrame 和 TabbedPane,但我对它们不满意。我想将我的代码拆分为不同的 JPanel 类并将它们分配给主 JFrame。我的意思是当用户单击每个菜单项时,它将调用外部 JPanel 并将其呈现在主 JFrame 上的 JPanel 上。我在 netbeans 中使用设计模式,它为我做了一切,但简单的结构是这样的,它不起作用:
public class NewJPanel extends JPanel{
//I have added buttons and etc on this panel
......
}
public class frame extends JFrame(){
JPanel panel = new JPanel();
.....
Public frame(){
frame.add(panel);
}
......
//When use click on the any button on the panel
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//this is not working
NewJPanel fi = new NewJPanel ();
panel1.add(fi);
//or I tested this way separately but it did not work
panel1.remove();
panel1 = new NewJPanel();
add(panel);
invalidate();
}
}
请给我任何建议,我可以如何以专业的方式在分班中控制这个程序。
【问题讨论】:
-
是
Public有效还是extends JFrame()? -
@yannishristofakis 是的,它是公开的。
-
这是您关于此主题的第三个问题。停止创建新问题并遵循其他帖子的建议,即使用
CardLayout。绝对没有理由使用 JLayeredPane。
标签: java swing user-interface jframe jpanel