CardLayout - 如何更改组件名称

时间:2015-08-07 18:25:25

标签: java cardlayout

有没有办法更改添加到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

1 个答案:

答案 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