else if (e.getActionCommand().equals("load and play saved file")) {
JButton source = (JButton)(e.getSource());
try {
newgame = loadChapterState(source.getText() + ".dat");
newgame.SetUpGame();
//newgame.setCursor(newgame.cursor);
CardLayout layout = (CardLayout)(overallgame.getLayout());
layout.show(overallgame, GAME);
//requestFocusInWindow
newgame.requestFocus /*InWindow*/ (true);
dummyframe.repaint();
} catch (IOException ex) {
JOptionPane.showMessageDialog(dummyframe, "Darn it!", "Error in retrieval", JOptionPane.ERROR_MESSAGE);
//ex.printStackTrace();
}
这是loadChapterState(游戏状态加载)方法的代码
public static RPGGame loadChapterState(String file) throws IOException {
ObjectInputStream ois = null;
RPGGame gamedata = null;
try {
ois = new ObjectInputStream(new FileInputStream(file));
gamedata = (RPGGame) ois.readObject();
gamedata.setCursor(gamedata.cursor);
} catch (IOException ex) {
System.out.println("Sorry, there was an error reaching the file");
System.out.println("You will not be able to play with this state");
//ex.printStackTrace();
} finally {
ois.close();
return gamedata;
}
}
让我感到困惑的是,当我运行它时,我完全得到了游戏地图的图像,但我实际上无法通过输入到keylistener来移动光标,尽管在上面的加载方法中将其重置为setCursor方法(我刚刚获得该游戏地图的任何旧副本时遇到了麻烦)。
现在,我确实稍后在我的main方法中加载了保存状态,就像这样。
System.out.println("Would you like to create a file?");
try {
newgame = loadChapterState("stuff.dat");
newgame.SetUpGame();
} catch (IOException e) {
e.printStackTrace();
}
//finally constructs master panel that can flip between main menu and running game
overallgame = new JPanel(new CardLayout());
overallgame.add(mainmenupanel, MAIN_MENU);
overallgame.add(newgame, GAME);
overallgame.add(creditpanel, CREDITS);
overallgame.add(chapterstartmenupanel, CHAPTERSTART);
overallgame.add(showsavefilespanel, SAVE_FILES);
(这个可以完美地使用光标,将我的actionlistener的另一部分与下一位相结合后,将该侦听器分配给带有" start" actioncommmand的JButton)
if (e.getActionCommand().equals("start")) {
//dummyframe.add(newgame);
//mainmenupanel=null;
CardLayout layout = (CardLayout)(overallgame.getLayout()); //retrieves the overall layout
layout.show(overallgame, GAME); //uses it to switch to main game panel
newgame.requestFocus(true); //need to redirect focus to the game rather than original main menu
dummyframe.repaint();
}
如果请求,我将显示rpggame类。 我想我想要的是,如何让我的光标正常工作?#34;加载和播放保存fie"命令? 谢谢你的帮助!