Java操作侦听器在创建主菜单后不工作
在创建菜单之前,我的蛇游戏完美无瑕。现在,我已经创建了一个菜单,在点击“开始”按钮时开始游戏,游戏不再注册到我的按键 作为控制器工作的简单管理器:Java操作侦听器在创建主菜单后不工作,java,swing,user-interface,actionlistener,Java,Swing,User Interface,Actionlistener,在创建菜单之前,我的蛇游戏完美无瑕。现在,我已经创建了一个菜单,在点击“开始”按钮时开始游戏,游戏不再注册到我的按键 作为控制器工作的简单管理器: public class GameManager { public GameManager() { new mainMenu(); } public static void main(String[] args) { new GameManager(); } } 新实现的主菜单类
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是最上面的容器,所以所有关键事件都被困在这里,而不会被重新路由到游戏板(我假设它是另一个容器)。我建议在主菜单中添加一个按键侦听器,并将按键事件重新发送到游戏板。您以前在哪里使用过该
TAdapter
?如果游戏中应该是美元,那么你应该输入游戏本身的代码,而不是菜单。为了更快地获得更好的帮助,请添加or。你不应该使用KeyListener。相反,您应该使用键绑定
。有关更多信息和工作示例,请参阅。谢谢!关于如何实现这一点,有什么建议吗?很抱歉延迟回复。您可能已经找到了答案,但是,如果我要这样做,我会让mainMenu实现KeyListener,并在GameBoard类中,将按键事件处理程序移出TAdapter。然后在主菜单中的新按键事件处理程序中,在游戏板中调用keyPressed函数,并传递key参数。希望这有帮助。