如何同时运行ActionListener和KeyListener?

时间:2018-03-28 16:12:02

标签: java actionlistener keylistener

我试图制作一个简单的游戏,但出于某种原因,当我尝试使用KeyPressed方法时没有任何反应。我尝试了几件事情,我发现错误是当我输入内容时程序永远不会进入keyPressed方法。我可以更改什么以便程序进入keyPressed方法?

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

public class Game extends JFrame {
    public static void main(String[] args) {
        Game maze = new Game();
        maze.run();
    }

    public void run() {
        JFrame frame = new JFrame("Game");
        frame.setSize(800, 600);
        frame.setLocation(50, 50);
        frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        frame.setResizable(true);
        GamePanel accessF = new GamePanel();
        frame.setContentPane(accessF);
        frame.setFocusable(true);
        frame.setVisible(true);
    }
}

class GamePanel extends JPanel {

    int xDino, velX, velPlayerX, velPlayerY, xPlayer, yPlayer, code, code2;

    public GamePanel() {
        Timer tmD = new Timer(100, new DinoHandler());
        tmD.start();
        xDino = -800;
        velX = 2;
        velPlayerX = 0;
        velPlayerY = 0;
        xPlayer = 50;
        yPlayer = 50;
        KeyHandler ship = new KeyHandler();
        addKeyListener(ship);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        setBackground(Color.WHITE);
        Image space = new ImageIcon("simple-star-space-background-effect-footage-023768280_prevstill.jpg").getImage();
        g.drawImage(space, xDino, 0, 1600, 600, null);
        if (xDino == 0) xDino = -800;

        g.setColor(Color.GRAY);

        g.fillRect(xPlayer, yPlayer, 50, 50);
    }

    class DinoHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            xDino = xDino + velX;
            System.out.print(xDino);
            repaint();
        }
    }

    class KeyHandler implements KeyListener {
        public void keyPressed(KeyEvent e) {
            requestFocus();
            xDino = 100;
            code = e.getKeyCode();
            if (e.getKeyCode() == KeyEvent.VK_UP) {
                xPlayer = 60;
                velPlayerY = 2;
            }

            if (code == KeyEvent.VK_DOWN) {
                velPlayerY = -2;
            }

            if (code == KeyEvent.VK_LEFT) {
                velPlayerX = -2;
            }

            if (code == KeyEvent.VK_RIGHT) {
                velPlayerX = 2;
            }
            repaint();
        }

        public void keyReleased(KeyEvent e) {
            code2 = e.getKeyCode();
            if (code2 == KeyEvent.VK_UP) {
                velPlayerY = 0;
            }

            if (code2 == KeyEvent.VK_DOWN) {
                velPlayerY = 0;
            }

            if (code2 == KeyEvent.VK_LEFT) {
                velPlayerX = 0;
            }

            if (code2 == KeyEvent.VK_RIGHT) {
                velPlayerX = 0;
            }
            repaint();
        }

        public void keyTyped(KeyEvent e) {
            System.out.println("hi");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

使面板可聚焦并请求焦点

public void run() {
    JFrame frame = new JFrame("Game");
    frame.setSize(800, 600);
    frame.setLocation(50, 50);
    frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    frame.setResizable(true);
    GamePanel accessF = new GamePanel();
    frame.setContentPane(accessF);
    accessF.setFocusable(true);
    accessF.requestFocusInWindow();

    frame.setVisible(true);
}