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