0

我无法让按键事件正常工作,它无法识别按键输入,有人可以帮忙吗?

我有三类播放器,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();

}

}

提前致谢

4

1 回答 1

2

KeyListener仅当组件可聚焦且具有焦点时才会响应关键事件。

最好使用Key Bindings API,它可以让你克服这个限制(如果你愿意的话)

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

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

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

于 2013-10-03T21:27:55.097 回答