我怎样才能让我的cardLayout工作?

Pur*_*ret 1 java layout swing

我正在尝试开发一个LARP角色管理器,我在我的框架内,一个面板包含我想要使用CardLayout交换的所有窗口.这是我的ContainerPane代码.

public class ContainerPane extends JPanel {
    private static final long serialVersionUID = -4799973935806714569L;
    private JPanel deckOfPanes = null;
    private PlayerManagerPane myPlayerManagerPane = null;
    private GameManagerPane myGameManagerPane= null;
    private CharacterManagerPane myCharacterManagerPane = null;

    final static String CHANGETOCHARACTERMANAGERPANE = "Character Manager";
    final static String CHANGETOPLAYERMANAGERPANE = "Player Manager";
    final static String CHANGETOGAMEMANAGERPANE = "Game Manager";

    public ContainerPane(EventListener myEventListener) {
        myPlayerManagerPane = new PlayerManagerPane(myEventListener);
        myGameManagerPane = new GameManagerPane(myEventListener);
        myCharacterManagerPane = new CharacterManagerPane(myEventListener);
        deckOfPanes= new JPanel(new CardLayout());
        deckOfPanes.add(myCharacterManagerPane,CHANGETOCHARACTERMANAGERPANE);
        deckOfPanes.add(myPlayerManagerPane,CHANGETOPLAYERMANAGERPANE);
        deckOfPanes.add(myGameManagerPane,CHANGETOGAMEMANAGERPANE);

        CardLayout cardLayout = (CardLayout) ((ContainerPane) this).getDeckOfPanes().getLayout();
        cardLayout.show(deckOfPanes,CHANGETOCHARACTERMANAGERPANE);
    }

public JPanel getDeckOfPanes() {
    return deckOfPanes;
}
Run Code Online (Sandbox Code Playgroud)

首先,我想象构造函数的最后一行将确保在调用时它显示在顶部的某个卡片.

在我的代码的其他地方,我想使用菜单栏交换卡片.这是我的EventHandler类的代码:

public void swapView(String key) {
    CardLayout cardLayout = (CardLayout) ((ContainerPane) myContainerPane).getDeckOfPanes().getLayout();
    cardLayout.show(myContainerPane.getDeckOfPanes(),key);
}
Run Code Online (Sandbox Code Playgroud)

这也不起作用.我刚刚开始使用Java,我真的很感激任何帮助,我已经检查了教程和网络上的其他地方(包括堆栈溢出),从我看到的,它应该工作.请,任何帮助将不胜感激.

Rei*_*eus 5

您尚未将deckOfPanes添加到ContainerPane.加:

    deckOfPanes = new JPanel(cardLayout);
    add(deckOfPanes); 
    // etc. 
Run Code Online (Sandbox Code Playgroud)