Java KeyListener不';行不通

Java KeyListener不';行不通,java,swing,keylistener,key-bindings,java-canvas,Java,Swing,Keylistener,Key Bindings,Java Canvas,我正在编写一个类似Bomberman的游戏,我的KeyListener有问题 问题是,当游戏运行时,KeyListener不会响应,但当它不运行时,它会按照我告诉他的去做 这是我的密码 public class direction extends Canvas implements KeyListener { static float bmx = 35; static float bmy = 35; static float v = 0.03f; public static BufferSt

我正在编写一个类似Bomberman的游戏,我的KeyListener有问题

问题是,当游戏运行时,KeyListener不会响应,但当它不运行时,它会按照我告诉他的去做

这是我的密码

public class direction extends Canvas implements KeyListener {

static float bmx = 35;
static float bmy = 35;
static float v = 0.03f;

public static BufferStrategy strategie;
public static BufferedImage image;

public direction()  {                                           //pas sûr de ce que ça fait
    GraphicsEnvironment ge =                                    //on m'a dit de le mettre           
            GraphicsEnvironment.getLocalGraphicsEnvironment();  //le programme marche très bien sans
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gd.getDefaultConfiguration();
    image =  gc.createCompatibleImage(700,  700);
    setSize(700, 700);

}

    static boolean gauche;
    static boolean droite;
    static boolean haut;
    static boolean bas;


    public void keyPressed(KeyEvent e) {

        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            gauche = true;
        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            droite = true;
            System.out.println("lal");
        }
        if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            bas = true;
        }
        if (e.getKeyCode() == KeyEvent.VK_UP) {
            haut = true;
        }
    }
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            gauche = false;
        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            droite = false;
        }
        if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            bas = false;
        }
        if (e.getKeyCode() == KeyEvent.VK_UP) {
            haut = false;
        }
    }
    public void keyTyped(KeyEvent e) {
    }


public static void draw(){

    Graphics2D g = (Graphics2D) strategie.getDrawGraphics();

    for(int ligne= 0 ; ligne < board.gridHauteur; ligne++){
        for(int colonne= 0 ; colonne < board.gridLargeur ; colonne++){

            switch (board.plateau1[ligne][colonne]) {
            case 0 : g.setColor(Color.lightGray);
            g.fillRect( 30*ligne, 30*colonne , 30, 30);
            break;
            case 1 : g.setColor(Color.black);              //dessine et donne la propriété 'BLOCKED' à certains bloques
            g.fillRect( 30*ligne, 30*colonne , 30, 30);
            board.plateau1[ligne][colonne] = board.BLOCKED;
            break;
            case 2 : g.setColor(Color.darkGray);
            g.fillRect( 30*ligne, 30*colonne , 30, 30);
            board.plateau1[ligne][colonne] = board.BLOCKED;
            break;
            }
        }
    }
    g.setColor(Color.red);
    g.fillRect((int)bmx, (int)bmy, 20, 20);   //dessine le "joueur"
    strategie.show();
}

public static void run() {

    long dernierTempsLoop = System.currentTimeMillis();   // avec cette ligne, nous demandons un t0

    while(true) {

        long delta = (System.currentTimeMillis() - dernierTempsLoop); // puis, à chaque nouvel boucle
        dernierTempsLoop = System.currentTimeMillis();               //nous calculons le temps que la 
                                                                    //boucle à mise pour s'effectuer

        for (int i=0;i<delta / 5;i++) {
            logic(5);                   //permet d'appeler à ce qu'il y ait peu de mouvements si la boucle est rapide
        }                               // et plus de mouvements si elle est plus lente ce qui garantit une vitesse constante
                                        // quelle que soit le temps que la boucle met pour s'effectuer
        if ((delta % 5) != 0) {
            logic(delta % 5);
        }

        draw();     //dessine le terrain et le rectangle

        try { Thread.sleep(10); } catch (Exception e) {};   //permet au programme de ne pas "surchauffer"
    }

我希望我能理解,谢谢你的阅读。

我想你的问题可能是在画完所有你可能需要做的事情后:

g.setFocusable(true);
g.requestFocusInWindow();
不仅如此:

g.setFocusable(true);

我认为你的问题可能是在画完所有你可能需要做的事情后:

g.setFocusable(true);
g.requestFocusInWindow();
不仅如此:

g.setFocusable(true);

尝试在单独的类中实现KeyListener接口

并将此界面添加到图形上下文中


尝试在单独的类中实现KeyListener接口

并将此界面添加到图形上下文中


您需要重新格式化代码。(发布前查看预览窗口并查看编辑帮助)。仅供参考:使用Swing键绑定,而不是使用KeyListener。并使用Swing组件(例如JComponent、JPanel等),而不是使用AWT组件(例如Canvas)。另外,停止使用如此糟糕的
static
。用适当的
new Something()
替换它。谢谢Guillaume Polet,我会这样做,看看它是否会改变您需要重新格式化代码的任何内容。(发布前查看预览窗口并查看编辑帮助)。仅供参考:使用Swing键绑定,而不是使用KeyListener。并使用Swing组件(例如JComponent、JPanel等),而不是使用AWT组件(例如Canvas)。另外,停止使用如此糟糕的
static
。用合适的
new Something()
替换它。谢谢Guillaume Polet,我会这样做,看看它是否改变了我刚刚尝试的任何东西,问题仍然存在,这将如何解决问题?重构通常不会改变行为,只会改变代码结构。这个问题很可能与焦点有关,它将使用密钥绑定iso KeyListener解决。我刚刚尝试过,问题仍然存在,这将如何解决问题?重构通常不会改变行为,只会改变代码结构。这个问题很可能与焦点有关,使用键绑定iso KeyListenersIt不会更改任何内容,抱歉,它不会更改任何内容
public class MyListener implements KeyListener{

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

    }

}
g.addKeyListener(new MyListener ());