Parent container/panel for a CardLayout

孤人 提交于 2019-12-20 05:49:07

问题


How I can get the 'container' panel for any panel in CardLayout.

That is, if a panel is a card in another 'container' panel, then how to get reference to this 'container' panel, from the card?

Here's what i am doing:-

public class LogInPanel extends javax.swing.JPanel implements ActionListener{

    /**
     * Creates new form Panel2
     */
   private JPanel parentPanel;
   private CardLayout c1=null;
    public LogInPanel() {
        initComponents();
        //c1=new CardLayout();
        parentPanel=(JPanel)(SwingUtilities.getAncestorOfClass(this.getClass(), this));

        c1=(CardLayout)(parentPanel.getLayout());
        submitLogin.addActionListener(this);
    }
...

回答1:


You can use the method getParent that is provided by the Component superclass. For your panel that is a card, simply call panel.getParent() and it will give you the containing (parent) panel.



来源:https://stackoverflow.com/questions/14659033/parent-container-panel-for-a-cardlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!