简体   繁体   English

具有actionlistener的JButton不执行其代码

[英]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). 这是一个带有三个按钮的JFrame(我剪掉了另外两个按钮)。 It is supposed to be a Pause menu, a "popup" when the key 'p' is pressed. 它应该是“暂停”菜单,当按下键“ p”时是“弹出”菜单。 It works quite good, the problem is 它工作得很好,问题是

GamePanel.var.setPause(false); GamePanel.var.setPause(false);

This line should set a boolean on false, which then continues my game loop. 该行应在false上设置一个布尔值,然后继续我的游戏循环。 But for some reasons it never executes, the boolean stays on true. 但是由于某些原因它永远不会执行,布尔值保持为true。 This Problem only occurred after i changed the main game window from JPanel to JFrame, so i could show other jframes. 仅在将主游戏窗口从JPanel更改为JFrame之后,才出现此问题,因此我可以显示其他jframe。 I link them both because they are quite long: 我将它们链接在一起是因为它们很长:

This is the old code with JPanel. 这是JPanel的旧代码。 The button worked with this code. 该按钮适用于此代码。

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

Here is the current code (JPanel) 这是当前代码(JPanel)

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

(JFrame) (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. 编辑:我犯了一个错误,并依靠我的paintComponent方法向我提供有关布尔值的信息。 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)? 如果将断点放入actionPerformed()方法(或在那里简单地放置System.out.println()语句)怎么办? Is it reached/executed? 是否已达成/执行?

Good luck with the project, seems fun! 该项目祝您好运,似乎很有趣!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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