我的Keylistener不能工作,我想知道为什么

时间:2015-11-20 20:25:11

标签: java swing jframe keylistener

当我运行该程序时,所有内容都会显示,但是当我尝试移动键时,如果有人知道为什么会这样或者如何修复它,就不会发生任何事情,我将非常感激。

我的主要游戏窗口类

public class GameWindow extends JFrame {
    private static final long serialVersionUID = 1L;
    public int WIDTH = 160, HEIGHT = WIDTH / 12 * 9, SCALE = 3;
    public boolean running = false;
    BackGround bg = new BackGround();
    Ranger R = new Ranger();
    TimerClass T = new TimerClass();
    public static void main(String[] args) {
        new GameWindow();
    }
    public GameWindow() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(WIDTH * SCALE, HEIGHT * SCALE);
        setResizable(false);
        running = true;
        add(bg);
        bg.add(R);
        bg.setFocusable(true);
        R.setFocusable(true);
        setFocusable(true);
        setVisible(true);
        bg.repaint();
        run();
    }
    public void run() {
        while (running) {
            render();
        }
    }
    public void render() {
        bg.setLocation(Ranger.bgX, Ranger.bgY);
        R.setLocation(Ranger.X, Ranger.Y);
        R.setIcon(Ranger.rangerA[Ranger.I]);
        R.repaint();
        bg.repaint();
    }
}

我的游侠(我要移动的精灵)类

public class Ranger extends JLabel implements KeyListener {
    private static final long serialVersionUID = 1L;
    public static int X, Y, dX, dY, bgX, bgY, I = 0, jumpTime = 100;
    public static boolean moving = false, movingLeft = false, movingRight = false, onFloor = false, jumping = false, movingUp = false, movingDown = false;
    public int totalImages = 6;
    public BufferedImage ranger1, ranger2, ranger3, ranger4, ranger5, ranger6;
    public static ImageIcon[] rangerA;
    static TileMap TileMap = new TileMap();
    public Ranger() {
        try {
            ranger1 = ImageIO.read(getClass().getResource("/Images/Sprites/ranger/Ranger0.png"));
            ranger2 = ImageIO.read(getClass().getResource("/Images/Sprites/ranger/Ranger1.png"));
            ranger3 = ImageIO.read(getClass().getResource("/Images/Sprites/ranger/Ranger2.png"));
            ranger4 = ImageIO.read(getClass().getResource("/Images/Sprites/ranger/Ranger3.png"));
            ranger5 = ImageIO.read(getClass().getResource("/Images/Sprites/ranger/Ranger4.png"));
            ranger6 = ImageIO.read(getClass().getResource("/Images/Sprites/ranger/Ranger5.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        array();
    }
    public void array() {
        rangerA = new ImageIcon[6]; {
            rangerA[0] = new ImageIcon(ranger1);
            rangerA[1] = new ImageIcon(ranger2);
            rangerA[2] = new ImageIcon(ranger3);
            rangerA[3] = new ImageIcon(ranger4);
            rangerA[4] = new ImageIcon(ranger5);
            rangerA[5] = new ImageIcon(ranger6);
        }
    }
    public static void move() {
        X += dX;
        Y += dY;
        System.out.println("X =" + X + " Y= " + Y + " dX = " + dX + " dY= " + dY + " jumpTime= " + jumpTime + " ");
        dX = 0;
        dY = 0;
        if (movingRight || movingLeft) {
            moving = true;
        }
    }@Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_RIGHT) {
            movingRight = true;
        }
        if (keyCode == KeyEvent.VK_LEFT) {
            movingLeft = true;
        }
        if (keyCode == KeyEvent.VK_UP) {
            jumping = true;
            movingUp = true;
        }
        if (keyCode == KeyEvent.VK_DOWN) {
            movingDown = true;
        }
        if (keyCode != KeyEvent.VK_RIGHT) {
            movingRight = false;
        }
        if (keyCode != KeyEvent.VK_LEFT) {
            movingLeft = false;
        }
        if (keyCode != KeyEvent.VK_UP) {
            jumping = false;
            movingUp = false;
        }
        if (keyCode != KeyEvent.VK_DOWN) {
            movingDown = false;
        }
        if (keyCode == KeyEvent.VK_RIGHT) dX += 1;
        if (movingRight) bgX -= 1;
        if (keyCode == KeyEvent.VK_LEFT) dX += -1;
        if (movingLeft) bgX += 1;
        if (keyCode == KeyEvent.VK_DOWN) dY += 1;
        if (movingUp) bgY += 1;
        if (keyCode == KeyEvent.VK_UP) dY += -1;
        if (movingDown) bgY += -1;
    }@Override
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent arg0) {}
}

我会提供所有课程,如果他们需要,并感谢任何帮助,即使它是在编程惯例。

0 个答案:

没有答案