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);
						}
					}
				});


			}


		});

Edited 5 Years Ago by shootingrubber: n/a

The problem is you have:

GUI go = new GUI();

in two different places. one of them final.
what you want to do is replace these instantiations by:

go = new GUI();

and declare the variable go outside of the main method as a static instance of GUI:

private static GUI go;

The problem is you have:

GUI go = new GUI();

in two different places. one of them final.
what you want to do is replace these instantiations by:

go = new GUI();

and declare the variable go outside of the main method as a static instance of GUI:

private static GUI go;

Thank you!!

hi,shootingrubber can you give the code of GUI

that ís the code to the GUI class.
just put

public class GUI extends JFrame{

....



}

around it.
basides, creating a GUI is not that difficult. if you need one, why not write one of your own?

Ok, so I am having one more problem now. Basically what I am trying to do now is check if a window is open and if it is then execute the dispose() method on that window, and if the window has not been opened then do nothing. Basically for the Tic Tac Toe program I am putting the code for each level of difficulty against the computer in a different class, so in the menubar I can put an ActionListener on a JMenuItem so that if the user clicks that it will go straight to the level of difficulty selected.

Here is some example code...

NewGame.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				go.dispose();

I want to say if go has been opened or is active then dispose it, etc...

Any ideas?

This question has already been answered. Start a new discussion instead.