简体   繁体   中英

JButton with actionlistener does not execute its code

I'm developing a litte game in my freetime, but i encountered a problem which i don't really understand why its happening.

public class PauseMenu extends JFrame {
       ...
/**
 * Create the frame.
 */
public PauseMenu() {
           ...
    JButton btnContinue = new JButton("Continue");

    btnContinue.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            GamePanel.var.setPause(false);
            dispose();
        }
    });
    contentPane.add(btnContinue);

    ...
}

This is the code which doesn't really work. It is a JFrame with three buttons(i cut out the other two). It is supposed to be a Pause menu, a "popup" when the key 'p' is pressed. It works quite good, the problem is

GamePanel.var.setPause(false);

This line should set a boolean on false, which then continues my game loop. But for some reasons it never executes, the boolean stays on true. This Problem only occurred after i changed the main game window from JPanel to JFrame, so i could show other jframes. I link them both because they are quite long:

This is the old code with JPanel. The button worked with this code.

https://github.com/westerwave/dogfight_remake/blob/master/main/Dogfight.java

Here is the current code (JPanel)

https://github.com/westerwave/dogfight_remake/blob/master/main/GamePanel.java

(JFrame)

    public static GameFrame frame;
    public GamePanel game;

    public GameFrame() {
       setUndecorated(true);
       setResizable(false);
       setTitle("Dogfight-Frame");
       setSize(dim);
       setVisible(true);
       game = new GamePanel();
       getContentPane().add(game);
       frame = this;
    }

I hope i made myself clear on the question and that you can help me.

EDIT: I made a mistake and relied on my paintComponent method to give me the information about the boolean. I now found out that the boolean is changed, so everything working as intended. But somehow does my gameloop not resume after i click that button

That should work properly, I suspect some confusion here.

What if you put a breakpoint into the actionPerformed() method (or simply put a System.out.println() statement there)? Is it reached/executed?

Good luck with the project, seems fun!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM