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

為{幸葍}努か 提交于 2019-11-28 14:46:29

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

You control which card shows by using CarrdLayout show.


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!