如何从 JFrame 的中心元素显示不同的页面(已设置为 BorderLayout)

How to show different pages from the center element of JFrame (having set to BorderLayout)

import java.awt.BorderLayout;
import java.awt.Component;  
import javax.swing.JFrame;
import javax.swing.border.Border;

public class GuiController extends JFrame {

    private CentreFrameController centreFrameController;
    private CustomerPage customerPage;
    private LoginPage loginPage;
    public GuiController(){
        centreFrameController=new CentreFrameController(this);      
        setLayout(new BorderLayout());      

        add(centreFrameController,BorderLayout.CENTER);     

        setTitle("Courier System-Login Page");
        setVisible(true);
        setSize(550,650);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }   
}

CentreFrameController

import java.awt.BorderLayout;
import java.awt.Graphics; 
import javax.swing.JPanel;

public class CentreFrameController extends JPanel {

    private GuiController guiController;
    private CustomerPage customerPage;
    private LoginPage loginPage;
    public CentreFrameController(GuiController guiController){
        this.guiController=guiController;
        loginPage=new LoginPage(this);
        setLayout(new BorderLayout());

        add(loginPage,BorderLayout.CENTER);
        loginPage.addLoginPageListener(new LoginPageListener(){
            public void getLoginPageReply(int reply) {
                switch(reply){
                case 0:System.out.println("Customer login sucess");
                setCustomerHomePage();
                break;
                case 1:System.out.println("Admin login success");
                }
            }
        });
    }
    public void setCustomerHomePage(){
        customerPage=new CustomerPage(this,loginPage);
        add(customerPage,BorderLayout.CENTER);///This is the part which is not working
    }
}

这部分代码的目的是:

  1. 我想让 GuiController 框架在我的应用程序的所有页面上都有一些共同的样式(不考虑任何 activity)
  2. GuiController 框架将 BorderLayout 的中心元素样式提供给 CentreFrameController 面板
  3. 现在 CentreFrameController 应该在听到 LoginListener 的消息后切换页面。但是在这里,当它收到 LoginListener 消息时,它并没有将 CustomerPage 添加到其布局中(请注意,这里 CustomerPage class 是为了扩展 JPanel 我还没有上传那个,因为它不是真的有必要)。

这是一个 mcve 演示如何按照 Andrew Thompson 的建议使用 CardLayout

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GuiController extends JFrame {

    private boolean isRedShowing;

    public GuiController(){

        setTitle("CardLayout Demo");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        CentreFrameController centreFrameController = new CentreFrameController();
        centreFrameController.showRedPane();
        isRedShowing = true;
        setLayout(new BorderLayout());
        add(centreFrameController,BorderLayout.CENTER);
        JButton toggle = new JButton("Toggle");
        toggle.addActionListener(e ->{
            if(! isRedShowing) {
                centreFrameController.showRedPane();
            } else {
                centreFrameController.showYelloPane();
            }
            isRedShowing = ! isRedShowing;
        });
        add(toggle,BorderLayout.PAGE_END);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new GuiController();
    }
}

class CentreFrameController extends JPanel {

    public final String YELLOW_PAGE = "yellow page";
    public final String RED_PAGE = "red page";
    private final CardLayout cLayout;

    public CentreFrameController(){

        cLayout = new CardLayout();
        setLayout(cLayout);
        setPreferredSize(new Dimension(200, 150));
        add(YELLOW_PAGE, new YellowPane());
        add(RED_PAGE, new RedPane());
    }

    //two convenience methods that encapsulate CardLayout#show(Container, String)
    void showRedPane() {
        cLayout.show(this, RED_PAGE);
    }

    void showYelloPane() {
        cLayout.show(this, YELLOW_PAGE);
    }
}

class RedPane extends JPanel{

    RedPane(){
        setBackground(Color.RED);
    }
}

class YellowPane extends JPanel{

    YellowPane(){
        setBackground(Color.YELLOW);
    }
}

您可以使用 CarrdLayout show.

来控制显示哪张卡片