使用keylistener输出按下的键

时间:2016-12-21 18:18:23

标签: java swing keylistener

我正在按照在线教程阅读按键。这是一个很老的视频,所以我不确定语法是否已经改变。我也在这里看了其他问题,似乎无法找到解决方案。

我的问题是它没有将按键打印到控制台。没有错误被抛给我

Game.Java:

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.Random;

public class Game extends Canvas implements Runnable {

    private static final long serialVersionUID = 240840600533728354L;
    public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9;
    private Thread thread;
    private boolean running = false;
    private Random r;
    private Handler handler;


    public Game(){
         handler = new Handler();

         this.addKeyListener(new KeyInput());
         new Window(WIDTH, HEIGHT, "The Game", this);

         r = new Random();

         handler.addObject(new Player(WIDTH/2-32,HEIGHT/2-32,ID.Player));
         handler.addObject(new Player(WIDTH/2+64,HEIGHT/2-32,ID.Player2));
    }

    public synchronized void start() {
        thread = new Thread(this);
        thread.start();
        running = true;
    }

    public synchronized void stop() {
        try{
            thread.join();
            running = false;
        }catch(Exception e ) {
            e.printStackTrace();
        }
    }

    public void run() {
        long lastTime = System.nanoTime();
        double amountOfTicks = 60.0;
        double ns = 10000000; //amount of ticks
        double delta = 0;
        long timer = System.currentTimeMillis();
        int frames = 0;
        while(running){
            long now = System.nanoTime();
            delta += (now - lastTime) / ns;
            lastTime = now;
            while(delta >= 1){
                tick();
                delta--;
            }
            if(running)
                render();
                frames++;

                if(System.currentTimeMillis() - timer > 1000){
                    timer += 1000;
                    //System.out.println("FPS: " + frames);
                    frames = 0;
                 }
            }
            stop();
        }

        private void tick(){
            handler.tick();
        }

        private void render(){
            BufferStrategy bs = this.getBufferStrategy();
            if(bs == null){
                this.createBufferStrategy(3);
                return;
            }

            Graphics g = bs.getDrawGraphics();

            g.setColor(Color.black);
            g.fillRect(0, 0, WIDTH, HEIGHT);

            handler.render(g);

            g.dispose();
            bs.show();
        }


    public static void main(String args[]){
     new Game();   
    }
}

KeyInput.Java

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyInput extends KeyAdapter  {

    private Handler handler;

    //public KeyInput(Handler handler){
    //  this.handler = handler;
    //}

    public void keyPressed(KeyEvent e){
        int key = e.getKeyCode();
        System.out.println(key);
    }

    public void keyReleased(KeyEvent e){
        int key = e.getKeyCode();
    }

}

2 个答案:

答案 0 :(得分:2)

您的线程在启动后立即终止,因为:

 thread.start();
 running = true;

这导致你的线程终止,因为当它启动时,变量尚未设置为true

public void run() {
    ...
    while(running){ //here is the problem, this variable is stil false now

    }

更改两个语句的顺序,它应该有效:

public synchronized void start() {
    thread = new Thread(this);
    running = true;
    thread.start();
}

修改:请注意您应该在EventDispatchThread进行渲染!

答案 1 :(得分:0)

我尝试添加焦点并且它有效,感谢@ user7291698

public Game(){
     setFocusable(true);
     requestFocus();
     requestFocusInWindow();
     handler = new Handler();

     this.addKeyListener(new KeyInput());
     new Window(WIDTH, HEIGHT, "The Game", this);

     r = new Random();

     handler.addObject(new Player(WIDTH/2-32,HEIGHT/2-32,ID.Player));
     handler.addObject(new Player(WIDTH/2+64,HEIGHT/2-32,ID.Player2));
}