0
我原本計劃不依賴於java的窗口組件系統。但是,我被迫使用它,因爲它具有雙緩衝。所以這導致我目前的問題。有兩個JPanel組件MainMenuDisplay和GameDisplay。無論何時您點擊開始按鈕,我都希望它擺脫mainMenuDisplay,並用新的遊戲顯示替換它。但是,每當我嘗試這樣做時,它都會刪除mainMenuDisplay,即使代碼顯然正在運行,也不會添加gameDisplay。所以它只是讓JFrame沒有任何組件來運行它們的paintComponent方法。沒有錯誤被拋出。那麼爲什麼它不添加新的GameDisplay? P.S.請不要因爲使用靜態對象而對我大吼大叫。未添加到JFrame的組件
Start.WINDOW.changeCurrentDisplay(new GameDisplay());
//Creates a new GameDisplay and passes it the JFrame
//This is inside MainMenuDisplay
//This is part of the JFrame class
Display mainMenuDisplay;
Display gameDisplay;
//Display class extends JPanel
public WindowManager() {
super("Mountours - And - Men");
setSize(settings.windowHeight, settings.windowWidth);
setLocationRelativeTo(null);
setResizable(settings.resizable);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.WHITE);
currentDisplay = new MainMenuDisplay();
addKeyListener(currentDisplay.input);
addMouseListener(currentDisplay.input);
add(currentDisplay);
setVisible(true);
}
public void changeCurrentDisplay(Display display) {
removeAll();//Removes the MainMenuDisplay
mainMenuDisplay = display;
add(display);//Attempts to add component to JFrame, but doesn't, no errors either
addKeyListener(display.input);
addMouseListener(display.input);
}
當您調用add方法時,組件層次結構將失效。如果容器已經顯示(因爲它在這種情況下),你需要調用revalidate方法。 OTOH Google for CardLayout。它完全正是你需要做的。 – dic19 2014-09-29 03:23:02