I am writing a simple game based on two JFrames. The user is displayed only on the JFrame at a time. To switch to another JFrame, the user clicks a button. The button in the new JFrame will have an overriden method, which should transfer the user to the old JFrame.
I found success, but the .dispose () method does not seem to close the new frame when the user tries to revert to the old frame.
Here is 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 another 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.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 much appreciated :)
source
share