KeyListener - 我需要在main中调用keyPressed方法吗? [英] KeyListener - do I need to call the keyPressed Method in my main?

查看:194
本文介绍了KeyListener - 我需要在main中调用keyPressed方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是 keyPressed 中的内容:

public class Movie extends JFrame implements KeyListener {

public static Sprite star1 = new Sprite("Assets/star1.png");
public static Sprite star2 = new Sprite("Assets/star2.png");
public static Sprite star3 = new Sprite("Assets/star3.png");

public void init(){
    this.addKeyListener(this);
}
@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

}
@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}
public void keyPressed(KeyEvent e) {

    System.out.println("KEY PRESSED: " + e.getKeyChar());
    animation window = new animation(500, 450); //length , height

    if (e.getKeyCode() == KeyEvent.VK_DOWN)
    {
        setFocusable(true);
        Movie.star1.setPosition( Movie.star1.getXposition() -100, 0);
        window.frameFinished();
    }
    else if  (e.getKeyCode() == KeyEvent.VK_UP)
    {
        setFocusable(true);
        Movie.star1.setPosition( Movie.star1.getXposition() +100, 0);
        window.repaint();
    }

}

我的对象在按下箭头键。

My object does not move when the arrow keys are pressed.

所有我想知道的是 - 这是因为我需要调用 keyPressed(KeyEvent e)方法我的主要?当我打电话给它时,我收到一个错误,指出:

All I want to know is - is this because I need to call the keyPressed(KeyEvent e) method in my main? When I do call it, I get an error that states:


无法在变量中解析

cannot be resolved in a variable

我要移动的对象是一个巨大的循环。

The objects that I want to move are in a giant loop.

推荐答案

如果你从未将密钥监听器添加到某些Swing组件,那么它将永远不会收到事件。

If you never add the key listener to some Swing component, then it will never receive events.

KeyListener 本身并不神奇,也不会监听按键。你使用 KeyListener 做的是:你告诉其他一些Swing组件(如窗口或文本框)来调用你的 KeyListener 按下某个键时。该组件是查找按键的内容,而不是侦听器。

KeyListener itself isn't magical and doesn't listen for keypresses. What you do with a KeyListener is: you tell some other Swing component (like a window or a textbox) to call your KeyListener when a key is pressed. The component is what looks for keypresses, not the listener.

在您的情况下,看起来您打算将关键侦听器添加到窗口, this.addKeyListener(this); (因为在你的情况下这个都是 KeyListener JFrame )。

In your case, it looks like you meant to add the key listener to the window, with this.addKeyListener(this); (since in your case this is both a KeyListener and a JFrame).

但是,如果没有人调用你的 init 方法,然后你的 init 方法中的代码(就像任何方法一样)永远不会运行,因此键监听器不会被添加到窗口中,所以按下键时窗口不会调用它!

However, if nothing calls your init method, then the code inside your init method (like any method) never runs, so the key listener doesn't get added to the window, so the window doesn't call it when a key is pressed!

一种可能的解决方案是确保调用 init 创建一个新的电影后(你没有显示发生这种情况的代码)。

One possible solution would be to make sure to call init after creating a new Movie (you haven't shown the code where that happens).

另一个解决方案是是使用构造函数,而不是像这样的方法:

Another solution would be to use a constructor, instead of a method, like this:

public Movie() {
    this.addKeyListener(this);
}

- 构造函数在创建对象时运行,这样,<$ c只要创建 Movie 对象,就会调用$ c> addKeyListener ,而创建者不必记得调用 init

- constructors run when the object is created, so that way, addKeyListener will be called whenever a Movie object is created, without the creator having to remember to call init.

这篇关于KeyListener - 我需要在main中调用keyPressed方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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