Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从单独的面板更改CardLayout面板?_Java_Swing_Oop_Layout Manager_Cardlayout - Fatal编程技术网

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

Java 如何从单独的面板更改CardLayout面板?,java,swing,oop,layout-manager,cardlayout,Java,Swing,Oop,Layout Manager,Cardlayout,我的软件布局有点像向导。因此,基础面板分为两个JPanels。一个永远不会改变的左面板。和一个右面板,用于卡片布局。它有许多子面板,并通过一种方法显示每个子面板 我可以很容易地从一个内板到另一个。但我想在左边的面板上有一个按钮,然后换右边的面板 以下是您可以运行的示例代码: 基础: public class Base { JFrame frame = new JFrame("Panel"); BorderLayout bl = new BorderLayout()

我的软件布局有点像向导。因此,基础面板分为两个
JPanel
s。一个永远不会改变的左面板。和一个右面板,用于
卡片布局
。它有许多子面板,并通过一种方法显示每个子面板

我可以很容易地从一个内板到另一个。但我想在左边的面板上有一个按钮,然后换右边的面板

以下是您可以运行的示例代码:

基础:

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

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 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 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 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(“第二页”)
主面板.showPanel(“首页”)

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


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

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


所以
LeftBar
执行
mainPanel.showPanel(“第二页”)
位于
主面板的第二个实例上,该实例甚至不是可视层次结构的一部分。要解决此问题,只需将
MainPanel
的现有实例传递给
LeftBar
的构造函数。您已经在
FirstPage
SecondPage
中执行了此操作

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


所以
LeftBar
执行
mainPanel.showPanel(“第二页”)
位于
主面板的第二个实例上,该实例甚至不是可视层次结构的一部分。要解决此问题,只需将
MainPanel
的现有实例传递给
LeftBar
的构造函数。您已经在
FirstPage
SecondPage
中执行了此操作

我还有一个类似的问题。但我提出了一个单独的问题。你能看一下吗?我还有一个类似的问题。但我提出了一个单独的问题。你能看一下吗?