Java Swing -.dispose () Method Does Not Close JFrame

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 is the button that has it 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 much appreciated :)

+4
source share
1 answer

, u Object , u Dispose();, , .? :

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

, .

0

Source: https://habr.com/ru/post/1532238/


All Articles