我正在编写一个应用程序,我使用CardLayout在两个彼此重叠的面板之间进行交换。
有一个名为Top的JPanel,它的布局是CardLayout。在这个JPanel里面是一个叫做CMatch的JPanel。每当用户点击CMatch面板中的提交按钮时,我想要一个新的JPanel添加到Top,它是根据用户键入的内容自定义构建的,它将显示而不是原始的CMatch面板。所有这些都是使用CardLayout完成的。
然而,这些是不同文件中的所有不同类(面板Top with CardLayout,面板CMatch位于Top面板内,以及自定义构建面板)。 所以我尝试使用以下内容将新面板添加到Top面板,然后显示:
(此代码发生在CMatch类中):
private void submitButtionActionPerformed(ActionEvent e) {
CardLayout cl = (CardLayout)(this.getParent().getLayout());
cl.addLayoutComponent(new CChoice(), "college_choices");
cl.show(this.getParent(), "college_choices");
}
然而,这不起作用。所以我想知道,我做错了什么? 非常感谢任何建议,谢谢!
答案 0 :(得分:1)
不知道它是否有所作为,但我总是直接向Container添加组件:
String cardName = "college_choices";
Container parent = this.getParent();
parent.add(new CChoice(), cardName);
CardLayout cl = (CardLayout)parent.getLayout();
cl.show(parent, cardName);
答案 1 :(得分:0)
The following works for me:
this.requiredPanelName.setvisible(true) // for required panel
this.otherPanelName.setvisible(false) // for not required
this.otherPanelName.setvisible(false) // for not required