Java操作监听器在创建主菜单后不起作用

huangapple go评论88阅读模式
英文:

Java Action listener not working after creating a main menu

问题

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;
        }
    }
}

请注意,我已经删除了您给出的代码中的HTML转义字符(例如:" 被替换为双引号),以便使代码正确显示。

英文:

My Snake game worked flawlessly before creating a menu. Now that I have created a menu that starts the game upon clicking the "start" button, the game no longer registers to my key presses.

Simple manager working as a controler:

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

Newly implemented main menu class


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);
}
}

Relevant methods from Pre-existing game class:

     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;
}
}
}
}

答案1

得分: 1

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

英文:

It's likely that your key events are lost in the event chain. Since your JFrame is the top most container, all the key events are being trapped here and not getting re-routed to your game board (I am assuming it's another container). I would recommend adding a key listener at the main menu, and re-route the key events to your game board.

huangapple
  • 本文由 发表于 2020年4月8日 04:01:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/61088463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定