按键不会工作

时间:2013-10-03 21:18:19

标签: java swing awt keylistener arrow-keys

我无法按下按键事件,它无法识别按键输入,有人可以帮忙吗?

我有三个类播放器,exe和board

这是董事会成员

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Board extends JPanel implements ActionListener{
private Timer timer;
private Player player;

//private Floor

public Board() {

    addKeyListener( new TAdapter() );
    setFocusable(true);
    setBackground(Color.WHITE);
    setDoubleBuffered(true);

    player = new Player();
    //floor = new Floor();

    timer = new Timer(5, this);
    timer.start();
}

public void paint(Graphics g) {

    super.paint(g);

    Graphics2D g2d = (Graphics2D)g;
    g2d.drawImage(player.getImage(), player.getX(), player.getY(), this);
    // g2d.drawImage(floor.getImage(), floor.getX(), floor.getY(), this);
    //System.out.println(player.getX() + ", " + player.getY());
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}

public void actionPerformed(ActionEvent e) {
    // checkPlayerOnGround();
    player.move();
    repaint();  
}

private class TAdapter extends KeyAdapter implements KeyListener{

    public void keyTyped(KeyEvent e) {
        player.keyReleased(e);
        System.out.println("Released");
    }

    public void keyPressed(KeyEvent e) {
        player.keyPressed(e);
        System.out.println("Pressed");

    }
}

}

这是玩家类

import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.Timer;
import java.util.TimerTask;
public class Player{

private String player = "player.jpg";
private int moveX;
private int moveY;
private int x;
private int y;
private Image image;
private boolean canFall = false;

//private Timer jumpTimer = new Timer();
boolean canJump = true;

public Player() {
    ImageIcon playeriImage = new ImageIcon(this.getClass().getResource(player));
    image = playeriImage.getImage();
    x = 40;
    y = 60;
}

public void checkFall() {
    if(canFall) {
        moveY = -4;
    }
}

public void move() {
    //checkFall();
    x = moveX + x;
    y  = moveY + y;

}

public int getX() {
    return x;
}

public int getY() {
    return y;
}

public Image getImage() {
    return image;
}

public void keyPressed(KeyEvent e) {

    int key = e.getKeyCode();

    if (key == KeyEvent.VK_A) {
        moveX = -4;

    }

    if (key == KeyEvent.VK_D) {
        moveX = 4;
    }

    if (key == KeyEvent.VK_W) {
        if(canJump)
            this.jump();

    }

}

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

    if (key == KeyEvent.VK_A) {
        moveX = 0;

    }

    if (key == KeyEvent.VK_D) {
        moveX = 0;

    }

}

这是exe类

import javax.swing.JFrame;

public class Exe extends JFrame {

public Exe() {

    add(new Board());

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500, 500);
    setLocationRelativeTo(null);
    setTitle("TSA Video Game");
    setResizable(false);
    setVisible(true);
}

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

}

}

提前致谢

1 个答案:

答案 0 :(得分:2)

KeyListener仅响应关键事件,如果该组件是可聚焦的并具有焦点。

最好使用Key Bindings API来帮助你克服这个限制(如果你愿意的话)

我还建议您覆盖paintComponent而不是paint。您也不应该调用Graphics#dispose,因为您没有创建Graphics上下文,这实际上可以防止在某些系统上绘制任何内容

我还建议延迟5毫秒(在你的计时器上)可能有点过分(5毫秒= 200fps!)。

16(60fps)或40(25fps)的值可能更合适并减少系统上的开销...恕我直言