Move your statements inside your ActionListener
rather than having them in the constructor:
startGame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
gameActive = true;
add(game);
revalidate();
repaint();
}
});
Notice the gameActive
check is redundant for the current code. Also getContentPane().add(game)
can simply be written as add(game)
.
Note that CardLayout is purposely designed to manage this type of functionality.