姬長信(Redy)

java – KeyListener无法正常工作


出于某种原因,我的KeyListener只是没有响应KeyPressed事件.

如果重要,我在Ubuntu 12.04上.每按一次键就应该打印“按键”,但不是.

这是代码:

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

public class DisplayPanel extends JPanel
{
    private Tile[][] tiles;
    private Creature[] creatures;
    private Dungeon dungeon;
    private Player player;

    public DisplayPanel(Dungeon dungeon, Tile[][] tiles, Creature[] creatures, Player player)
    {
        this.tiles = tiles;
        this.creatures = creatures;
        this.dungeon = dungeon;
        this.player = player;
        addKeyListener(new DungeonKeyListener());
        requestFocus();
    }

    protected void paintComponent(Graphics g)
    {
        int maximum = (getWidth()