3

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 :)

nobody
  • 19,814
  • 17
  • 56
  • 77
justinSYDE
  • 307
  • 1
  • 2
  • 11
  • 3
    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 Mar 18 '14 at 03:04
  • 4
    Did you set the `defaultCloseOperation` to something other then `DO_NOTHING`? – MadProgrammer Mar 18 '14 at 03:09
  • 4
    *"I'm writing a simple game that is based on two JFrames."* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Mar 18 '14 at 09:20
  • How does this have 4 upvotes?? There isn't enough code to express the issue, while there is also irrelevant code (like `random_generator`). – Vince May 20 '18 at 14:13

1 Answers1

-1

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.

Kishan Bheemajiyani
  • 3,429
  • 5
  • 34
  • 68
  • I highly doubt he wants to dispose the frame he is displaying milliseconds before the dispose call. Notice how the class that code resides in is an inner class. The outter class extends `TicTacToeSingle`, which extends `JFrame`, and I believe *that's* the frame he's attempting to dispose, not the frame from `twoPlayer`. If so, then his call to `dispose()` is fine. – Vince May 20 '18 at 14:18