Question

I'm trying to add a JPanel to my Frame on the click of a JMenuItem. I've looked in past posts and I've tried it using:

getContentPane().add(myJPanel)
revalidate();
repaint();

To no avail. My Game class is just simply a constructor with the size set and a background colour for now. Oh and sorry for the messy code, I had deleted my non working original and done this up quickly to give an idea of what I've tried. Any Suggestions will be appreciated.

package game;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class Main extends JFrame{

static boolean gameActive = false;
Game game;
JMenuBar menuBar;
JMenu file;
JMenuItem startGame;
JMenuItem checkScore;
JMenuItem closeGame;
int userScore;

public Main() {

    game = new Game();
    gameActive = false;

    //set JMenuBar
    menuBar = new JMenuBar();
    file = new JMenu("File");
    startGame = new JMenuItem("Start Game");
    checkScore = new JMenuItem("Check Score");
    closeGame = new JMenuItem("Close Game");
    menuBar.add(file);
    file.add(startGame);
    file.add(checkScore);
    file.add(closeGame);

    startGame.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            gameActive = true;
        }

    });

    if(gameActive == true){
        getContentPane().add(game);
        revalidate();
        repaint();
    }

    checkScore.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(checkScore, "your Score is " + userScore);

        }

    });

    closeGame.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });

    setJMenuBar(menuBar);

    //JFrame Properties
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 300);
    setLocationRelativeTo(null);
    setTitle("BattleGrounds");
    setResizable(false);
    setVisible(true);

}



   public static void main(String[] args){
       SwingUtilities.invokeLater(new Runnable(){
           public void run(){
                    new Main();
           }
       });
   }

 }
Was it helpful?

Solution

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.

OTHER TIPS

The code inside the if block will never be executed because gameActive is false when the constructor is called

 if(gameActive == true){
    getContentPane().add(game);
    revalidate();
    repaint();
}

You need to show your game activity when the startGame is clicked; however you have to check that the game is not added to your contentPane already so when the user click the menu many times it does not add a new game instance everytime

startGame.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
       gameActive = true;
       if(game == null) {
          game = new Game(); // remove the game creation from the constructor
          getContentPane().add(game);           
       }
    }

});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top