2013-02-02 13 views
0

可能重複:
Parent container/panel for a CardLayout從卡本身在CardLayout顯示忘詞

如何顯示從卡板本身(其佈局不CardLayout卡面板,但其包含在具有CardLayout的面板中)?我不想在容器中放置單獨的導航欄,因爲我需要訪問卡數據。

+0

就像我提到的4小時前在您的其他問題。爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。不要忘記給問題添加一個問號(?)。 –

+0

請指出這個問題與上一個問題在同一主題上的區別:[CardLayout的父容器/面板](http://stackoverflow.com/questions/14659033/parent-container-panel-for-a-cardlayout) – trashgod

回答

0

您可以使用面板的getParent()方法訪問父組件(此方法可從每個AWT/Swing組件訪問)。如果您的面板不是直接放置在具有卡布局的面板中,則應該重複調用此方法,直到獲得所需的面板。

例子:

Component myComp; // for example your navigation bar 
Component parent = myComp.getParent(); 
while (null != parent) { 
    if (parent.getLayout() instanceof CardLayout) { 
    break; 
    } 
    parent = parent.getParent(); 
} 
if (null != parent) { 
    // now we have parent with the card layout 
}