如何从JFrame的中心元素显示不同的页面(已设置为BorderLayout) [英] How to show different pages from the center element of JFrame (having set to BorderLayout)

查看:141
本文介绍了如何从JFrame的中心元素显示不同的页面(已设置为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



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 frame在我的应用程序的所有页面上都有一些共同的样式(无论是否有任何活动)

  2. GuiController frame将 BorderLayout 的中心元素样式赋予 CentreFrameController 面板

  3. 现在 CentreFrameController 应该在听到 LoginListener 后切换页面。但是在这里,当它从 LoginListener 中听到时,它没有在其布局中添加 CustomerPage (请注意,这里, CustomerPage 类是为了扩展 JPanel 而我没有上传,因为它不是必需的。)

  1. I want to make the GuiController frame have some common style for my application on all pages (irrespective of any activity)
  2. The GuiController frame gives the center element styling of the BorderLayout to the CentreFrameController panel
  3. Now the CentreFrameController should switch pages after hearing from the LoginListener. But here,when it hears from LoginListener, it's not adding the CustomerPage to its layout (please note that here, the CustomerPage class is made to extend JPanel and I have not uploaded that because its not really necessary).


推荐答案

这是 mcve 演示如何使用Andrew Thompson建议的 CardLayout

Here is a mcve demonstrating how you could use CardLayout as suggested by Andrew Thompson:

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

public class GuiController extends JFrame {

    public GuiController(){

        CentreFrameController centreFrameController = new CentreFrameController(this);
        centreFrameController.showRedPane();
        setLayout(new BorderLayout());
        add(centreFrameController,BorderLayout.CENTER);
        setTitle("Courier System-Login Page");
        setVisible(true);
        setSize(550,650);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    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 CardLayout cLayout;

    public CentreFrameController(GuiController guiController){

        cLayout = new CardLayout();
        setLayout(cLayout);
        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);
    }
}

您可以使用<$ c $控制显示哪张卡片c> CarrdLayout 显示

这篇关于如何从JFrame的中心元素显示不同的页面(已设置为BorderLayout)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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