关键事件监听器不起作用

时间:2014-02-16 01:56:42

标签: java swing

所以我正在努力实现键输入以移动精灵,但是我遇到了麻烦,因为出于某种未知的原因,我无法让键盘输入进行注册。我已经能够追踪我的问题,因为它没有调用TAdapter方法,但我不知道为什么。

这是我的代码:

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

当按下按键时会发生什么:

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

1 个答案:

答案 0 :(得分:0)

尝试使用implements KeyListener代替implements ActionListener