I am refactoring my code based on advice from my CodeReview.SE question. https://codereview.stackexchange.com/questions/126634/button-clicking-ui-for-a-game
Research:
I know that you have to create a method to call elsewhere. The doSomething code can't just sit in the class level.
Error:
I am encountering the error
Syntax error on token "saveGameOnClose", Identifier expected after this token
within
FRAME.addWindowListener(new WindowAdapter() {
SaveGameData.saveGameOnClose();
});
of my main class.
Main Class:
public Game() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
NewGameMessage.LoadNewGameMessage();
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }
catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace(); }
catch (IllegalAccessException ex) {
ex.printStackTrace(); }
catch (InstantiationException ex) {
ex.printStackTrace(); }
catch (ClassNotFoundException ex) {
ex.printStackTrace(); }
FRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FRAME.getContentPane().add(new addComponentsToPane());
FRAME.setSize(300, 500);
FRAME.setLocationRelativeTo(null);
FRAME.setVisible(true);
FRAME.setResizable(false);
FRAME.addWindowListener(new WindowAdapter() {
SaveGameData.saveGameOnClose();
});
}
});
}
SaveGameData class:
public class SaveGameData {
// Game.FRAME.addWindowListener(new WindowAdapter() {
static void saveGameOnClose(WindowEvent e) {
BufferedWriter writerOut = null;
try {
writerOut = new BufferedWriter(new FileWriter("res/saved_game_data.txt"));
writerOut.write(String.valueOf(Game.biDamageOutput) + "\r\n" +
String.valueOf(Game.biPoints) + "\r\n" +
String.valueOf(Game.biNewPoints) + "\r\n" +
String.valueOf(Game.biSpentPoints) + "\r\n" +
String.valueOf(Game.biKnifeCount) + "\r\n" +
String.valueOf(Game.biPistolCount) + "\r\n" +
String.valueOf(Game.biShotgunCount) + "\r\n" +
String.valueOf(Game.biRifleCount) + "\r\n" +
String.valueOf(Game.biRLauncherCount) + "\r\n" +
String.valueOf(Game.intTotalClicks));
writerOut.close();
} catch (Exception e1) {
System.err.println("Error: " + e1.getMessage());
}
};
}
Personal thought to the problem:
I'm writing the Window Listener incorrectly. But I'm trying to class each notable action, load, save, UI, individually as part of learning more about classes, sustainability, re-usability of my code. Or I just might be missing one brace or bracket.
Thanks.
Your method SaveGameData.saveGameOnClose(WindowEvent e)
requires a parameter of type WindowEvent
but you are calling it without any parameters SaveGameData.saveGameOnClose();
and that too without implementing any listener method
It should be something like this
FRAME.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
SaveGameData.saveGameOnClose(e);
}
});