Parent container/panel for a CardLayout

2019-03-06 05:27发布

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条回答
手持菜刀,她持情操
2楼-- · 2019-03-06 05:49

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.

查看更多
登录 后发表回答