即使面板打印,KeyListener也无法工作

时间:2012-11-26 05:46:11

标签: java keylistener

我有以下代码(我在扩展frame.add(new LevelPanel())的main方法中JFrame。由于某种原因,KeyListener的代码正是它对其他人和但它不会识别任何东西。

当我按下一个键时,它至少应该打印出“得到这里”(它没有)。代码会打印面板,但面板对我按下的任何键都没有反应。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*
 * Items represent the things in the level that can be picked up.
 * Subclasses: Gem
 * 
 * @author Tim Ochsner
 * @version 1.0 11/18/2012
 */

public class LevelPanel extends JPanel {
private Game game;
private Level level;
private Player player;
private char[][] table =
        {{'p', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f'},
        {'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'w', 'f', 'f', 'f', 'f', 'f'},
        {'f', 'f', 'f', 'f', 'w', 'f', 'f', 'f', 'f', 'w', 'f', 'f', 'f', 'f', 'f'},
        {'f', 'f', 'f', 'f', 'w', 'f', 'f', 'f', 'w', 'w', 'w', 'f', 'f', 'f', 'f'},
        {'f', 'f', 'f', 'f', 'w', 'f', 'f', 'f', 'f', 'f', 'f', 'w', 'f', 'f', 'f'},
        {'f', 'f', 'f', 'f', 'w', 'f', 'f', 'f', 'f', 'f', 'f', 'w', 'f', 'f', 'f'},
        {'f', 'f', 'f', 'f', 'w', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f'},
        {'w', 'f', 'f', 'f', 'w', 'w', 'w', 'w', 'w', 'f', 'f', 'f', 'f', 'f', 'f'},
        {'w', 'w', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'w', 'f'},
        {'w', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'w', 'f'},
        {'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'w', 'f'},
        {'f', 'f', 'f', 'f', 'f', 'f', 'w', 'w', 'w', 'w', 'f', 'f', 'w', 'w', 'f'},
        {'f', 'f', 'w', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'w', 'f', 'f', 'w', 'f'},
        {'f', 'f', 'w', 'f', 'f', 'f', 'f', 'f', 'f', 'w', 'f', 'f', 'f', 'f', 'f'},
        {'w', 'w', 'w', 'w', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f'}};

public LevelPanel(Game game)
{
    this.game = game;
    player = game.getPlayer();
    level = new Level(game, table);
    setBackground(Color.WHITE);
    addKeyListener(new ArrowListener());

}

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    level.draw(g);
}

public class ArrowListener implements KeyListener {

    public void keyPressed(KeyEvent e) {
        System.out.println("Got here");
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            if (!(player.getCol() == 14) && level.getTileLayout()[player.getRow() + 1][player.getCol()].isPassable()) {
                System.out.println("Got here");
                player.move(player.getRow() + 1, player.getCol());
                table[player.getRow()][player.getCol()] = 'f';
                table[player.getRow() + 1][player.getCol()] = 'p';
            }
        }

    }

    public void keyReleased(KeyEvent e) {
        System.out.println("Got here");

    }

    public void keyTyped(KeyEvent arg0) {
            System.out.println("Got here");         
    }

}

}

1 个答案:

答案 0 :(得分:1)

KeyListener是一个善变的情妇,我建议你避免使用key bindings

KeyListener只会触发一个事件,如果1-该组件是可聚焦的,而且该组件具有焦点。键绑定允许您绕过此限制。

如果您必须使用KeyListener,请先在setFocusable(true)上致电LevelPanel,然后拨打requestFocusInWindow