有没有办法更改添加到CardLayout面板的面板名称?我想使用show
方法,而不必使用我将面板添加到主面板时给出的面板名称。
JPanel panel = new JPanel();
add(panel, "0");
show(this, "0");
... // I change name of the panel to "1"
show(this, "1"); // now I can do this to show panel
答案 0 :(得分:1)
// original cardPanel1 name is "card1"
CardLayout lay = (CardLayout)parentPanel.getLayout();
lay.removeLayoutComponent(cardPanel1);
lay.addLayoutComponent(cardPanel1, "card4");
// cardPanel1 can now be shown using "card4" name