Interface KeyListener

cs_Romain62 Messages postés 7 Date d'inscription dimanche 13 avril 2008 Statut Membre Dernière intervention 21 mars 2010 - 21 mars 2010 à 17:16
darkme08 Messages postés 4 Date d'inscription vendredi 25 avril 2008 Statut Membre Dernière intervention 26 avril 2010 - 26 avril 2010 à 18:40
Bonjour tout le monde, alors voila j'ai une JFrame qui implémente l'interface KeyListener et je lui ajoute 3 écouteurs, elle-même et les JPanel qu'elle contient et pourtant je n'ai aucune réaction de la méthode keyPressed. Si quelqu'un pouvait m'éclairer ...


package snake;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class AppliSnake extends JFrame implements KeyListener, ActionListener{
    private static final int CADRE_MENU 0, CADRE_JEU 1, CADRE_SCORES = 2, CADRE_OPTIONS = 3;
    private JPanel cadre = new JPanel();
    private CadreMenu menu;
    private CadreJeu jeu;
    private int cadreCourant;
    
    public AppliSnake(){
        
        this.setTitle("Snake");
        this.setSize(400, 320);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        
        this.cadre.setLayout(new BorderLayout());      
        this.initCadre(CADRE_MENU);
        // Ajout de KeyListener   1  !!!!!!!!!!                   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        this.addKeyListener(this);
        this.setVisible(true);
    }
    
    // Modifie le contenu de la fenetre
    public void initCadre(int x){
        this.cadre.removeAll();
        switch (x) {
            case CADRE_MENU :
                this.menu = new CadreMenu();             
     // Ajout de KeyListener   2 !!!!!!     <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                this.menu.addKeyListener(this);                
                this.cadre.add(this.menu,BorderLayout.CENTER);     
     // Ajout de KeyListener  3 !!!          <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                this.cadre.addKeyListener(this);
                this.setContentPane(this.cadre);

                this.cadreCourant = CADRE_MENU;
                this.menu.getBtStart().addActionListener(this);
                this.menu.getBtScores().addActionListener(this);
                this.menu.getBtOptions().addActionListener(this);
                break;
            case CADRE_JEU :  
                this.jeu = new CadreJeu();
                this.cadre.add(this.jeu,BorderLayout.CENTER);
                this.setContentPane(this.cadre);
                this.cadreCourant = CADRE_JEU;
                break;
            case CADRE_SCORES :
                
                break;
        }
        this.repaint();                 
    }
    
    /*********************************************************************
     ********************* Interface KeyListener    **********************
     *********************************************************************/
    public void keyPressed(KeyEvent event){
        System.out.println("Code touche pressée : " + event.getKeyCode() + 
                        " - caractère touche pressée : " + event.getKeyChar());
    }
    public void keyReleased(KeyEvent event){}
    public void keyTyped(KeyEvent event){}
    
    /*********************************************************************
     ********************* Interface ActionListener    **********************
     *********************************************************************/
    public void actionPerformed(ActionEvent arg0) {                
        if (this.cadreCourant == CADRE_MENU){
            if(arg0.getSource() == this.menu.getBtStart()){this.initCadre(CADRE_JEU);}         
            if(arg0.getSource() == this.menu.getBtScores()){}
            if(arg0.getSource() == this.menu.getBtOptions()){}
        }
   }  
    public static void main(String[] args) {
        new AppliSnake();
    }
}

2 réponses

cs_Romain62 Messages postés 7 Date d'inscription dimanche 13 avril 2008 Statut Membre Dernière intervention 21 mars 2010
21 mars 2010 à 17:53
Après quelques tests je me suis rendu compte que si la méthode keyPressed ne se déclenche pas c'est parce que ma fenêtre contient plusieurs JButton. Quelqu'un pourrait-il m'expliquer pourquoi l'interface KeyListener ne fonctionne pas lorsque qu'il y a des boutons sur la même fenêtre ?
0
darkme08 Messages postés 4 Date d'inscription vendredi 25 avril 2008 Statut Membre Dernière intervention 26 avril 2010
26 avril 2010 à 18:40
Salut, j'ai le meme probleme je pense. As-tu trouvé la solution?

Romain.
0
Rejoignez-nous