我正在嘗試開發一個LARP角色管理器,並且在我的框架內部有一個面板來包含我想要使用CardLayout交換的所有窗口。這是我的ContainerPane代碼。如何讓我的cardLayout工作?
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;
}
首先,我想象一下構造函數的最後一行可以確保當它被調用時它顯示的是某個卡的頂部。
在我的代碼中的其他地方,我想使用菜單欄交換卡片。以下是我的EventHandler類的代碼:
public void swapView(String key) {
CardLayout cardLayout = (CardLayout) ((ContainerPane) myContainerPane).getDeckOfPanes().getLayout();
cardLayout.show(myContainerPane.getDeckOfPanes(),key);
}
這也不起作用。我剛開始使用Java,我非常感謝這方面的幫助,我已經檢查了教程和Web上的其他地方(包括堆棧溢出),並且從我可以看到的情況來看,它應該可行。請,任何幫助,將不勝感激。
第一個窗格現在可以工作,但我仍然無法獲取菜單來更改窗格。這是一個單獨的問題? – Pureferret 2012-07-20 16:20:28
@ Pureferret:爲此你必須顯示你用於'MenuItem'的代碼...... – 2012-07-20 16:37:25
這是一個單獨的問題。您尚未發佈任何與您的菜單調用交換相關的代碼。 – Reimeus 2012-07-20 16:38:20