如何制作(keyPressed)KeyListener? [英] How do I make a (keyPressed) KeyListener?

查看:82
本文介绍了如何制作(keyPressed)KeyListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个非常简单的程序,该程序允许您使用箭头键控制精灵并在屏幕上移动.据我了解,要做到这一点,我需要一个keyPressed() KeyListener.我相信我是根据Java Doc正确执行此操作的,但是当我按指定的键时,它不会返回正确的输出.有人可以告诉我我的代码有什么问题吗,如果可能,请提供此类用法的简单示例?谢谢!

Im trying to write a very simple program that allows you to control a sprite with arrow keys and move around the screen. From what I understand, to do this I need a keyPressed() KeyListener. I believe Im doing this correctly based on the Java Doc but its not returning the correct output when I press the designated key. Can someone please tell me what is wrong with my code, and if possible, provide a simple example of the usage of this class? Thanks!

(注意,代码未完成)

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Display extends JPanel implements ActionListener, KeyListener {
    Display() {

        // super();
        loadImages();
        initTimer();
        this.addKeyListener(this);

    }

    BufferedImage sprite;
    Timer timer;
    int up = 0;

    public void loadImages() {
        File spriteImage = new File("Pacman_sprite.png");
        try {
            sprite = ImageIO.read(spriteImage);
        } catch (IOException e) {

            System.out.println("Sprite import failed");
        }
    }

    public void initTimer() {
        timer = new Timer(100, this);
        timer.start();
        this.addKeyListener(this);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();
        System.out.println("Key press registered"); //does not print this?
        if (key == KeyEvent.VK_UP) {
            System.out.println("sucess"); // does not print this?
            up++;
            repaint();
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {

        System.out.println("release");
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        setBackground(Color.WHITE);
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(sprite, 500, 500 + up, null);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

    repaint();
}

}

可能在这里找到了答案.我将密钥处理代码移到了一个名为KeyHandler的新类中,然后将以下两行添加到了构造函数中:

May have found the answer here. I moved the key handling code to a new class called KeyHandler then added these two lines to the constructor:

addKeyListener(new KeyHandler());
setFocusable(true);

现在看来工作正常(至少在检测何时按下了向上键.我的图形没有.)

It now appears to be working just fine (sort of, at least it is detecting when the up key is hit. My graphics aren't.)

推荐答案

您没有将侦听器添加到JPanel.

You are not adding the listener to the JPanel.

注意:我建议您更改设计.现在,Display类既是JPanel又是Listener(在我看来这没有意义).

Note: I would suggest you to change your design. Right now, Display class is both a JPanel and a Listener (which doesn't make sense in my opinion).

但是要在代码中添加侦听器,请执行以下操作,

But to add the listener in your code do something like,

this.addKeyListener(this); // This looks awkward right. That's why you should change the design.

在您的构造函数中.

这篇关于如何制作(keyPressed)KeyListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆