KeyListener事件显然没有在java applet中触发

前端之家收集整理的这篇文章主要介绍了KeyListener事件显然没有在java applet中触发前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我刚刚开始学习编程applet和 java GUI,我试图制作一个简单的游戏.目前,我有主要类(游戏)和玩家类(玩家)我让玩家画到屏幕上,但我很难让关键事件发挥作用,任何帮助赞赏!

玩家图像显示完全正常,但当我完全按下按键时位置不会改变.

编辑:为播放器发布错误代码,更新

Game.java:

public class Game extends Applet {

    Player player;

    @Override
    public void init() {
        player = new Player(this);
        this.setSize(800,600);
        addKeyListener(player);
    }

    @Override
    public void paint(Graphics g) {        
        player.paint(g);
    }



}

Player.java:

public class Player implements KeyListener {

    Game game;
    Image image;
    int x;
    int y;

    public Player(Game game) {
        this.game = game;
        x = 0;
        y = 0;
        image = game.getImage(game.getCodeBase(),"images/Player.png");
    }

    public void paint(Graphics g) {
        g.drawImage(image,x,y,game);
    }

    @Override
    public void keyTyped(KeyEvent e) {
        return;
    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_A:
                x -= 1;
                break;
            case KeyEvent.VK_D:
                x += 1;
                break;
            case KeyEvent.VK_S:
                y += 1;
                break;
            case KeyEvent.VK_W:
                y -= 1;
                break;
        }
        game.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        return;
    }
}

解决方法

面板可能要么没有焦点,要么没有焦点.

试试这个代码.我将键更改为左/右/上/下箭头.为简单起见,我把它留作了基于AWT的applet.

// <applet code='Game' width=400 height=100></applet>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Game extends Applet implements KeyListener {

    Player player;

    @Override
    public void init() {
        player = new Player(getImage(32));
        addKeyListener(this);
        setFocusable(true);
        requestFocusInWindow();
    }

    @Override
    public void paint(Graphics g) {
        player.paint(g);
    }

    public Image getImage(int size) {
        Image img = createImage(size,size);
        Graphics g = img.getGraphics();
        g.setColor(Color.RED);
        g.fillRect(0,size,size);

        g.dispose();

        return img;
    }

    @Override
    public void keyTyped(KeyEvent e) {
        return;
    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_LEFT:
                player.x -= 1;
                break;
            case KeyEvent.VK_RIGHT:
                player.x += 1;
                break;
            case KeyEvent.VK_DOWN:
                player.y += 1;
                break;
            case KeyEvent.VK_UP:
                player.y -= 1;
                break;
        }
        repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        return;
    }
}

class Player {

    Image image;
    int x;
    int y;

    public Player(Image image) {
        x = 0;
        y = 0;
        this.image = image;
    }

    public void paint(Graphics g) {
        g.drawImage(image,null);
    }

}

猜你在找的Java相关文章