Hello,
I am writing a Tic Tac Toe java program, and it's almost all working; however, I noticed a bug in it recently and I cannot find the source of the problem. Basically the game has a New Game option in the file menu, and the first time it works fine, and it closes the current game screen and opens the next. Every time after that it keeps the current game window open and then opens a new one. So let's say you open the program and press new game 5 times, there will be 4 windows of the game open. I want the program to close the window and reopen a new game window. Any ideas?
My code is below:
public static void main(String[] args) {
final JMenuBar menubar = new JMenuBar();
JMenu File = new JMenu();
JMenuItem NewGame = new JMenuItem();
JMenuItem Exit = new JMenuItem();
JMenuItem About = new JMenuItem();
JMenuItem Instructions = new JMenuItem();
menubar.add(File);
File.add(NewGame);
File.add(Instructions);
File.add(About);
File.add(Exit);
File.setText("File");
NewGame.setText("New Game");
Exit.setText("Exit");
About.setText("About");
Instructions.setText("Instructions");
final GUI go = new GUI();
go.setJMenuBar(menubar);
go.setLocationRelativeTo(null);
go.setLocation(new Point(go.getX()-225,go.getY()-225));
go.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
go.setSize(450, 450);
go.setVisible(true);
go.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int i = JOptionPane.showOptionDialog(null, "Are you sure you want to exit the game?", "Exit Dialog", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, null, null);
if (i == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
NewGame.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
go.dispose();
GUI go = new GUI();
go.setJMenuBar(menubar);
go.setSize(450, 450);
go.setLocationRelativeTo(null);
go.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
go.setVisible(true);
go.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int i = JOptionPane.showOptionDialog(null, "Are you sure you want to exit the game?", "Exit Dialog", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, null, null);
if (i == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
}
});