简体   繁体   English

KeyListener-我是否需要在主程序中调用keyPressed方法?

[英]KeyListener - do I need to call the keyPressed Method in my main?

Here is what's inside my 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. 按下箭头键时,我的对象不会移动。

All I want to know is - is this because I need to call the keyPressed(KeyEvent e) method in my main? 我只想知道-这是因为我需要在我的main中调用keyPressed(KeyEvent e)方法吗? 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. 我要移动的对象处于巨大的循环中。

If you never add the key listener to some Swing component, then it will never receive events. 如果您从未将密钥侦听器添加到某些Swing组件,则它将永远不会接收事件。

KeyListener itself isn't magical and doesn't listen for keypresses. KeyListener本身并不具有魔力,也不会监听按键。 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. 使用KeyListener是:按下某个键时,告诉其他Swing组件(例如窗口或文本框)调用KeyListener The component is what looks for keypresses, not the listener. 该组件用于查找按键,而不是侦听器。

In your case, it looks like you meant to add the key listener to the window, with this.addKeyListener(this); 在您的情况下,您似乎打算使用this.addKeyListener(this);将密钥侦听器添加到窗口this.addKeyListener(this); (since in your case this is both a KeyListener and a JFrame ). (因为在您的情况下, this既是KeyListener又是JFrame )。

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方法,那么你的代码里面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). 一种可能的解决方案是确保在创建新的Movie之后调用init (您尚未在发生这种情况的地方显示代码)。

Another solution would be to use a constructor, instead of a method, like this: 另一个解决方案是使用构造函数,而不是像这样的方法:

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

- 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 . -构造函数在创建对象时运行,因此,只要创建Movie对象,就会调用addKeyListener ,而创建者不必记住调用init

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

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