【问题标题】:Java Action listener not working after creating a main menu创建主菜单后 Java Action 侦听器不起作用
【发布时间】:2020-07-20 03:26:09
【问题描述】:

我的 Snake 游戏在创建菜单之前完美运行。现在我创建了一个菜单,点击“开始”按钮即可开始游戏,游戏不再注册到我的按键。

作为控制者工作的简单经理:

public class GameManager {

    public GameManager() {
        new mainMenu();
    }

    public static void main(String[] args) {
        new GameManager();
    }
}

新实现的主菜单类


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class mainMenu extends JFrame{
    private final int WIDTH = 300;
    private final int HEIGHT = 300;
    private final int BUTTON_HEIGHT = 30;
    private final int BUTTON_WIDTH = 60;
    private final int SPACING = 10;
    private String title = "Jody Snake";
    Font smallText = new Font("Helvetica", Font.BOLD, 14);
    Font titleText = new Font("Helvetica", Font.BOLD, 30);
    JLabel titleLabel;
    JPanel buttonPanel;
    JButton playButton;
    JButton readMeButton;
    JButton quitButton;

    public mainMenu() {
        mainMenuGUI();
    }

    private void mainMenuGUI(){
        this.setSize(WIDTH, HEIGHT);
        titleLabel = new JLabel("SNAKE", SwingConstants.CENTER);
        titleLabel.setFont(titleText);
        this.add(titleLabel, BorderLayout.CENTER);

        buttonPanel = new JPanel(new FlowLayout());

        playButton = new JButton("PLAY");
        playButton.setFont(smallText);
        playButton.setBackground(Color.LIGHT_GRAY);
        playButton.setFocusPainted(false);
        buttonPanel.add(playButton);
        playButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                mainMenu.super.remove(buttonPanel);
                mainMenu.super.remove(titleLabel);
                mainMenu.super.add(new gameBoard());
                mainMenu.super.pack();
            }
        });

        readMeButton = new JButton("READ ME");
        readMeButton.setFont(smallText);
        readMeButton.setBackground(Color.LIGHT_GRAY);
        readMeButton.setFocusPainted(false);
        buttonPanel.add(readMeButton);


        quitButton = new JButton("QUIT");
        quitButton.setFont(smallText);
        buttonPanel.add(quitButton);
        quitButton.setBackground(Color.LIGHT_GRAY);
        quitButton.setFocusPainted(false);
        this.add(buttonPanel, BorderLayout.SOUTH);
        quitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });

        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

}

现有游戏类的相关方法:

     public gameBoard() {
        super();
        createGameBoard();

    }

    private void createGameBoard() {

        addKeyListener(new TAdapter());
        setBackground(Color.DARK_GRAY);
        setFocusable(true);

        setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
        loadImages();
        runGame();
    }
    private class TAdapter extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {

            int key = e.getKeyCode();

            if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) {
                leftDirection = true;
                upDirection = false;
                downDirection = false;
            }

            if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) {
                rightDirection = true;
                upDirection = false;
                downDirection = false;
            }

            if ((key == KeyEvent.VK_UP) && (!downDirection)) {
                upDirection = true;
                rightDirection = false;
                leftDirection = false;
            }

            if ((key == KeyEvent.VK_DOWN) && (!upDirection)) {
                downDirection = true;
                rightDirection = false;
                leftDirection = false;
            }
        }
    }
}

【问题讨论】:

标签: java swing user-interface actionlistener


【解决方案1】:

您的关键事件很可能在事件链中丢失。由于您的 JFrame 是最顶层的容器,因此所有关键事件都被困在这里并且不会重新路由到您的游戏板(我假设它是另一个容器)。我建议在主菜单中添加一个关键监听器,并将关键事件重新路由到您的游戏板。

【讨论】:

  • 您不应该使用 KeyListener。相反,您应该使用Key Bindings。有关更多信息和工作示例,请参阅 Motion Using the Keyboard
  • 谢谢!关于如何实现它的任何提示?
  • 抱歉回复延迟。您可能已经找到了答案,但是,如果我要这样做,我会让 mainMenu 实现 KeyListener,并在 GameBoard 类中,将 keyPressed 事件处理程序移出 TAdapter。然后在 mainMenu 中新的 keyPressed 事件处理程序中,调用游戏板中的 keyPressed 函数,并传递 key 参数。希望这会有所帮助。
猜你喜欢
  • 1970-01-01
  • 2013-04-30
  • 2013-12-16
  • 2011-11-24
  • 1970-01-01
  • 1970-01-01
  • 2022-10-17
  • 2017-09-27
  • 2013-05-16
相关资源
最近更新 更多