简体   繁体   English

关键事件监听器不起作用

[英]Key Event Listener Not Working

So I'm working on implementing key input to move a sprite around, but I'm having trouble because, for some unknown reason, I can't get the keyboard input to register. 因此,我正在努力实现按键输入以移动精灵,但是由于某些未知原因,我无法注册键盘输入,因此遇到了麻烦。 I've been able to track down my issue to the fact that it's not calling a the TAdapter method, but I don't know why. 我已经能够找出问题所在,因为它没有调用TAdapter方法,但是我不知道为什么。

Here is my code: 这是我的代码:

public class Game extends JPanel implements ActionListener
{
    private static final long serialVersionUID = 1L;

    private static Action upKeyAction;

    //Frame Width and Height
    static int WIDTH = 600;
    static int HEIGHT = 400;

    Timer timer;
    Magnus magnus;

    //Get Background
    BufferedImage background = null;{
        try {
            background = ImageIO.read(new URL("http://i.imgur.com/YXlnj1g.png"));
            System.out.println("Gotten Background");
        } catch (IOException e) {
            System.out.println("Incorrect Background");
        }}

    public Game()
    {

        addKeyListener(new TAdapter());
        setFocusable(true);
        setDoubleBuffered(true);
        requestFocusInWindow();

        magnus = new Magnus();

        timer = new Timer(5, this);
        timer.start();

        System.out.println("Game Intialized");
    }

    public void paint(Graphics g)
    {
        super.paint(g);
        //System.out.println("Graphics Painted");
        g.drawImage(background, 0, 0, null);
        g.drawImage(Magnus.magnus, magnus.getX(), magnus.getY(), this);
    }

    public void actionPerformed(ActionEvent e)
    {
        magnus.move();
        repaint();
    }

    private class TAdapter extends KeyAdapter
    {
        public void keyReleased(KeyEvent e)
        {
            magnus.keyReleased(e);
        }

        public void keyPressed(KeyEvent e)
        {
            magnus.keyPressed(e);
            System.out.println("Key Pressed");
        }
    }

And what should happen when the keys are pressed: 按下按键时会发生什么:

public class Magnus
{
    private int dx;
    private int dy;
    private int x;
    private int y;

    public static BufferedImage magnus;

    public Magnus()
    {
        try {
            magnus = ImageIO.read(new URL("http://i.imgur.com/eoz7j06.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Gotten Magnus");

        //Set Position
        x = 40;
        y = 60;
    }

    public void move()
    {
        x += dx;
        y += dy;
    }

    public int getX()
    {
        return x;
    }

    public int getY()
    {
        return y;
    }

    public BufferedImage getImage()
    {
        return magnus;
    }

    public void keyPressed(KeyEvent e)
    {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT)
        {
            dx = -1;
            System.out.println("Left button pressed");
        }

        if (key == KeyEvent.VK_RIGHT)
        {
            dx = 1;
            System.out.println("Right button pressed");
        }

        if (key == KeyEvent.VK_UP)
        {
            dy = -1;
            System.out.println("Up button pressed");
        }

        if (key == KeyEvent.VK_DOWN)
        {
            dy = 1;
            System.out.println("Down button pressed");
        }
    }

    public void keyReleased(KeyEvent e)
    {
        System.out.print("Key event initialized");
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT)
        {
            dx = 0;
        }

        if (key == KeyEvent.VK_RIGHT)
        {
            dx = 0;
        }

        if (key == KeyEvent.VK_UP)
        {
            dy = 0;
        }

        if (key == KeyEvent.VK_DOWN)
        {
            dy = 0;
        }
    }
}

尝试implements KeyListener而不是implements ActionListener

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM