如何从单独的面板更改CardLayout面板? [英] How to change CardLayout panels from a separate panel?

查看:149
本文介绍了如何从单独的面板更改CardLayout面板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My software layout is kinda wizard-base. So the base panel is divided into two JPanels. One left panel which never changes. And one right panel that works with CardLayout. It has many sub-panels and show each one of them by a method.

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

I can easily go from one inner panel to another one. But I want to have a button in left panel and change panels of the right side.

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

Here is a sample code which you can run it:

基础:

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"));
    }
}

这是一张给您这个想法的图片:

And this is a picture to give you the idea:

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

As I explained, I can travel "from first" page to "second page" by using this method: mainPanel.showPanel("secondPage"); or mainPanel.showPanel("firstPage");.

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

But I also have a JButton in the left bar, which I call the same method to show the second panel of the CardLayout. But it does not work. It doesnt give any error though.

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

Any idea how to change these CardLayout panels from outside of panels?

推荐答案

问题是LeftBar具有mainPanel成员,该成员已初始化为MainPanel的新实例.因此,您有两个MainPanel实例,一个在Base中分配并添加到框架中,另一个在LeftBar中分配.

The problem is that LeftBar has mainPanel member that is initialized to a new instance of MainPanel. So you have two instances of MainPanel, one allocated in Base and added to the frame, the other one allocated in LeftBar.

因此,LeftBarMainPanel的第二个实例上执行mainPanel.showPanel("secondPage");,该实例甚至不是视觉层次结构的一部分.要解决此问题,只需将现有的MainPanel实例传递给LeftBar的构造函数.您已经在FirstPageSecondPage中执行了此操作.

So LeftBar executes mainPanel.showPanel("secondPage"); on a second instance of MainPanel which is not even a part of a visual hierarchy. To fix this just pass an existing instance of MainPanel to the constructor of LeftBar. You already do this in FirstPage and SecondPage.

这篇关于如何从单独的面板更改CardLayout面板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆