Key Listener无法正常工作

时间:2014-02-11 22:26:04

标签: java swing keylistener

我一直在尝试创建一个java游戏,但我遇到了障碍,即使我只是使用print语句来测试它,我也无法让java听我的任何键。根据我的理解,我已经正确实现了KeyListener,并将关键监听器添加到Applet,但它仍然无法正常工作。

我的主要课程:

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

}

我的角色类:

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

2 个答案:

答案 0 :(得分:5)

当您将其添加为KeyListener时,看起来p1为null。

您可以在此处将其添加为KeyListener:

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

但是在这里实例化:

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

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

答案 1 :(得分:2)

KeyListener变幻莫测。他们要求他们注册的组件不仅可以聚焦,而且还有键盘焦点。

建议您改为使用Key Bindings代替