有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我的软件布局有点像向导。因此,基本面板被分为两个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"));
    }
}

这是一张让你产生想法的图片: enter image description here

正如我所解释的,我可以使用以下方法从第一页到第二页

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

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


共 (1) 个答案

  1. # 1 楼答案

    问题是LeftBarmainPanel成员已初始化为MainPanel的新实例。因此有两个MainPanel实例,一个在Base中分配并添加到帧中,另一个在LeftBar中分配

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