繁体   English   中英

如何从单独的面板更改CardLayout面板?

[英]How to change CardLayout panels from a separate panel?

我的软件布局有点像向导基础。 因此,基础面板分为两个JPanel 左侧面板永不改变。 还有一个与CardLayout一起使用的右面板。 它具有许多子面板,并通过一种方法显示每个子面板。

我可以轻松地从一个内面板转到另一个内面板。 但是我想在左侧面板中有一个按钮,并在右侧面板中进行更改。

这是一个示例代码,您可以运行它:

基础:

public class Base {
        JFrame frame = new JFrame("Panel");
        BorderLayout bl = new BorderLayout();

    public Base(){
        frame.setLayout(bl);
        frame.setSize(800, 600);
        frame.add(new LeftBar(), BorderLayout.WEST);
        frame.add(new MainPanel(), BorderLayout.CENTER);

        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        new Base();
    }
}

左边

public class LeftBar extends JPanel{
    JButton button;
    MainPanel mainPanel = new MainPanel();

    public LeftBar(){
        setPreferredSize(new Dimension(200, 40));
        setLayout(new BorderLayout());
        setBackground(Color.black);

        button = new JButton("Show Second Page");
        button.addActionListener(new ActionListener(){
           @Override
           public void actionPerformed(ActionEvent ae) {
               mainPanel.showPanel("secondPage");
           }

       });

       add(button, BorderLayout.NORTH);
    }
}

右边

public class MainPanel extends JPanel {
    private CardLayout cl = new CardLayout();
    private JPanel panelHolder = new JPanel(cl);

    public MainPanel(){
        FirstPage firstPage = new FirstPage(this);
        SecondPage secondPage = new SecondPage(this);

        setLayout(new GridLayout(0,1));

        panelHolder.add(firstPage, "firstPage");
        panelHolder.add(secondPage, "secondPage");

        cl.show(panelHolder, "firstPage");
        add(panelHolder);

    }
    public void showPanel(String panelIdentifier){
        cl.show(panelHolder, panelIdentifier);
    }
}

右侧内面板:

public class FirstPage extends JPanel {
    MainPanel mainPanel;
    JButton button;

    public FirstPage(MainPanel mainPanel) {
       this.mainPanel = mainPanel;
       setBackground(Color.GRAY);

       button = new JButton("Show page");
       button.addActionListener(new ActionListener(){
           @Override
           public void actionPerformed(ActionEvent ae) {
               mainPanel.showPanel("secondPage");
           }

       });

       add(button);
    }
}



public class SecondPage extends JPanel{
    MainPanel mainPanel;
    JButton button;
    public SecondPage(MainPanel mainPanel){
       this.mainPanel = mainPanel;
        setBackground(Color.white);
       add(new JLabel("This is second page"));
    }
}

这是给您这个想法的图片: 在此处输入图片说明

正如我所解释的,可以使用以下方法将“从第一移动到“第二页”mainPanel.showPanel("secondPage"); mainPanel.showPanel("firstPage");

但是我在左侧栏中还有一个JButton ,我调用了相同的方法来显示CardLayout的第二个面板。 但这行不通。 它没有给出任何错误。

知道如何从面板外部更改这些CardLayout面板吗?

问题在于LeftBar具有mainPanel成员,该成员已初始化为MainPanel的新实例。 所以,你有两个实例MainPanel ,在一个分配的Base ,并添加到帧,另一个在分配LeftBar

因此, LeftBar执行mainPanel.showPanel("secondPage"); LeftBar MainPanel的第二个实例上,该实例甚至不属于可视层次结构。 要解决此问题,只需将MainPanel的现有实例MainPanelMainPanel的构造LeftBar 您已经在FirstPageSecondPage执行了此操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM