3
votes

I'm writing a simple game that is based on two JFrames. The user is only displayed on JFrame at a time. To get to the other JFrame, the user clicks a button. The button in the new JFrame will have an overriden method that should navigate the user back to the old JFrame.

I've found success doing this however the .dispose() method doesn't seem to close the new frame when the user tries to navigate back to the old frame.

Here's a snippet of my code (original JFrame class):

public class TicTacToe extends JFrame implements ActionListener{

....

public class gameModeListener implements ActionListener
{
    @Override public void actionPerformed(ActionEvent e)
    {
        TicTacToeSingle singlePlayer = new TicTacToeSingle();
        singlePlayer.setVisible(true);
        dispose();
    }
}
}

And from the other JFrame class:

public class TicTacToeSingle extends TicTacToe{

private int i;
private int j;
 JButton boardArray[][];
 Random random_generator = new Random();
 int randomI;
 int randomJ;

public TicTacToeSingle()
{
    super("Single Player");
    gameMode.setText("Two Player"); //gameMode is the button that has it's actionlistener method overriden. It navigates the user to and back from JFrame to JFrame

    gameMode.addActionListener(new gameModeListener()); 

    ....
}

 ....

public class gameModeListener implements ActionListener
{
    @Override public void actionPerformed(ActionEvent e)
    {
        TicTacToe twoPlayer = new TicTacToe("Two Player");
        twoPlayer.setVisible(true);
        dispose();
    }
}

Your help is greatly appreciated :)

1
Don't throw a bunch of windows at the user. Instead give them a visual break and swap views in a single JFrame using a CardLayout. - Hovercraft Full Of Eels
Did you set the defaultCloseOperation to something other then DO_NOTHING? - MadProgrammer
"I'm writing a simple game that is based on two JFrames." See The Use of Multiple JFrames, Good/Bad Practice? - Andrew Thompson
How does this have 4 upvotes?? There isn't enough code to express the issue, while there is also irrelevant code (like random_generator). - Dioxin

1 Answers

-1
votes

Well when u have used Object of the class and u are calling just Dispose(); then how compiler came to know that for this particular class object should be dispose on that call.? so for giving reference u need to use this try this:

 TicTacToe twoPlayer = new TicTacToe("Two Player");
        twoPlayer.setVisible(true);
        twoPlayer.dispose();

if still there is prob let me know.