JAVA linux KeyListener似乎无法正常工作

时间:2017-07-08 18:22:40

标签: java events listener keylistener keyevent

我正在开发JAVA蛇游戏,似乎我的KeyListener因某些原因无法工作。我试图寻找答案,但我找不到任何有用的东西。我是JAVA的初学者,所以我不知道我是否正确使用了keylistener。我根本没有输出。

public class GameBoard extends JPanel implements Runnable, KeyListener{
    /**
     * 
     */
    private boolean up, down, right, left; 

    private Thread thread;
    private boolean running;
    private long targetTime;

    public GameBoard() {
        setPreferredSize(new Dimension(WIDTH,HEIGHT));
        setFocusable(true);
        requestFocus(); 
        addKeyListener(this);
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int k = e.getKeyCode();
        System.out.println("pressed");
        if( k == KeyEvent.VK_W || k == KeyEvent.VK_UP) up = true;
        if( k == KeyEvent.VK_S || k == KeyEvent.VK_DOWN) down = true;
        if( k == KeyEvent.VK_A || k == KeyEvent.VK_LEFT) left = true;
        if( k == KeyEvent.VK_D || k == KeyEvent.VK_RIGHT) right = true;
    }

    @Override
    public void keyReleased(KeyEvent e) {
        int k = e.getKeyCode();
        System.out.println("Released");
        if( k == KeyEvent.VK_W || k == KeyEvent.VK_UP) up = false;
        if( k == KeyEvent.VK_S || k == KeyEvent.VK_DOWN) down = false;
        if( k == KeyEvent.VK_A || k == KeyEvent.VK_LEFT) left = false;
        if( k == KeyEvent.VK_D || k == KeyEvent.VK_RIGHT) right = false;
    }

    @Override
    public void keyTyped(KeyEvent e) {
        int k = e.getKeyCode();
        System.out.println("typed");
    }

    @Override
    public void run() {
        if(running) return;
        init();
        while(running) {
            startTime = System.nanoTime();

            update();
            requestRender();


            elapsed = System.nanoTime()-startTime;
            wait = targetTime - elapsed / 10000000;
            if (wait > 0) {
                try {
                    Thread.sleep(wait);
                }catch(Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void init() {
        addKeyListener(this);
    }


    private void update() {

            if(up && dy == 0) {
                dy = -SIZE;
                dx = 0;
            }
            if(down && dy == 0) {
                dy = SIZE;
                dx = 0;
            }
            if(left && dx == 0) {
                dx = -SIZE;
                dy = 0;
            }
            if(right && dy == 0) {
                dx = SIZE;
                dy = 0;
            }

            if(dy!=0 || dx!= 0) {
                for( int i = snake.size() -1 ;i > 0; i-- ) {
                    snake.get(i).setPosition(snake.get(i-1).getX(),snake.get(i-1).getY());
                }
            }
            head.move(dx,dy);

            if(head.getX() < 0 ) head.setX(WIDTH);
            if(head.getY() < 0 ) head.setY(HEIGHT);
            if(head.getX() > WIDTH) head.setX(0);
            if(head.getY() > HEIGHT) head.setY(0);
    }

    }

}

1 个答案:

答案 0 :(得分:-2)

如果没有显示main()方法,我们所说的一切只是猜测。但有一个问题是你的run()方法是一个阻塞事件线程的无限循环。