简体   繁体   English

关键侦听器无法正常工作

[英]Key Listener isn't working

I've been experimenting with creating a java game but I've hit a roadblock, I can't get java to listen to any of my keys even when I'm just using print statements to test it out. 我一直在尝试创建Java游戏,但遇到了障碍,即使我仅使用print语句对其进行测试,也无法让Java监听任何键。 From what I understand I've implemented KeyListener correctly and added the key listener to the Applet but it still isn't working. 据我了解,我已经正确实现了KeyListener并将键侦听器添加到Applet中,但是它仍然无法正常工作。

My main class: 我的主班:

import java.awt.*;
import javax.swing.*;


public class Container extends JApplet implements Runnable {
    private static final long serialVersionUID = 1L;

    public static Dimension size = new Dimension(720,560); //Size of Screen

    private static final int PIXELSIZE = 2;
    public static Dimension pixel = new Dimension(size.width/PIXELSIZE,
            size.height/PIXELSIZE); // Dimesions of screen in terms of pixels


    public static final String NAME = "Game";
    public static boolean isRunning = false;
    private Image screen;

    public static Level level;
    public static MainCharacter p1;

    public Container(){
        setPreferredSize(size);
        addKeyListener(p1);
    }

    public void start(){
        new Tile();
        level = new Level();
        p1 = new MainCharacter(20,40);

        isRunning = true;
        new Thread(this).start();
    }

    public void tick(){
        p1.tick();
    }

    public void render(){
        Graphics g = screen.getGraphics();
        g.setColor(new Color(130,160,255));
        g.fillRect(0, 0, pixel.width, pixel.height);
        level.render(g);
        p1.render(g);
        g = getGraphics();
        g.drawImage(screen, 0, 0, size.width, size.height, 
                0, 0, pixel.width, pixel.height, null);
        g.dispose();
    }

    public void run() {
        screen = createVolatileImage(pixel.width,pixel.height);
        while(isRunning){
            tick();
            render();
            try{
                Thread.sleep(5);
            }catch(InterruptedException e){}    
        }
    }

    public static void main(String[] args){
        Container container = new Container();
        JFrame frame = new JFrame();
        frame.add(container);
        frame.pack();
        frame.setTitle(NAME);
        frame.setResizable(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        container.start();
    }

    public static void right(){
        p1.right();
    }

    public static void left(){
        p1.left();
    }

}

My character class: 我的角色课:

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class MainCharacter extends Tall implements KeyListener{
public double fallSpeed = 1.5;
public double moveSpeed = 1.0;
public double xSpeed = 1;


public MainCharacter(int width, int height){
    setBounds(Container.pixel.width/2 - width/2,
            Container.pixel.height/2 - height/2,
            width, height);
}

public void tick(){
    if(Container.level.space[(int)(x+width)][(int)(y+height)] &&
            Container.level.space[(int)(x)][(int)(y+height)] &&
            Container.level.space[(int)(x+width)][(int)(y)] &&
            Container.level.space[(int)(x)][(int)(y)])
        y += fallingSpeed;
}

public void render(Graphics g){
    g.drawImage(Tile.tileset_terrain, (int)x, (int)y,
            (int)(x+width),(int)(y+height),
            Tile.CHARACTER[0]*Tile.TILE_SIZE,
            Tile.CHARACTER[1]*Tile.TILE_SIZE,
            Tile.CHARACTER[0]*Tile.TILE_SIZE +(int)width,
            Tile.CHARACTER[1]*Tile.TILE_SIZE + (int)height, null);
}

public void right(){
    x += xSpeed;
}

public void left(){
    x -= xSpeed;
}

@Override
public void keyPressed(KeyEvent e) {
    System.out.println("hey");

}

@Override
public void keyReleased(KeyEvent e) {
    System.out.println("hey");

}

@Override
public void keyTyped(KeyEvent e) {
    System.out.println("hey");
}
}

It looks like p1 is null when you add it as a KeyListener. 当您将其添加为KeyListener时,看起来p1为null。

You add it as a KeyListener here: 您可以在此处将其添加为KeyListener:

public Container(){
    setPreferredSize(size);
    System.out.println(p1);  // try this...
    addKeyListener(p1);
}

But instantiate it here: 但是在这里实例化它:

public void start(){
    new Tile();
    level = new Level();
    p1 = new MainCharacter(20,40);

    isRunning = true;
    new Thread(this).start();
}

KeyListener s are fickle. KeyListener善变。 They require that the component they are registered to are not only focusable, but have keyboard focus. 他们要求注册的组件不仅要具有焦点,而且还要具有键盘焦点。

It's recommend that instead, you should use Key Bindings instead 建议您改用按键绑定

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

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