Java Action监听器在创建主菜单后无法工作



在创建菜单之前,我的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;
}
}
}
}

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

最新更新