2011-04-26 7 views
1

J'ai un JFrame qui héberge mon applet. Il y a un KeyListener sur l'applet pour gérer les touches fléchées et la touche Entrée/Échap. Lorsque j'exécute mon JFrame dans Eclipse, tout fonctionne correctement, les touches fléchées répondent ainsi que les touches Entrée et Échap.java keylistener ne fonctionne pas après l'exportation

Toutefois, lorsque j'exporte mon projet dans un fichier Jar exécutable ... les touches fléchées fonctionnent toujours, mais pas les touches Entrée et Échap. Comment puis-je résoudre ce problème?

Le code dans la classe principale:

public static void main(String[] args) throws Exception { 
    new SnakeApp().snake(); 
} 

public void snake() throws Exception { 
    // Set windows look 
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 

    // Create window 
    JFrame window = new JFrame("FinalSnake"); 
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    // Add FinalSnake applet 
    FinalSnake finalSnake = new FinalSnake(); 
    finalSnake.init(); 
    finalSnake.start(); 
    window.add(finalSnake); 

    // Set size 
    window.setResizable(false); 
    window.getContentPane().setPreferredSize(new Dimension(FinalSnake.GRIDSIZE * FinalSnake.GRIDX, FinalSnake.GRIDSIZE * FinalSnake.GRIDY)); 
    window.pack(); 

    // Set Icon 
    window.setIconImage(new ImageIcon(this.getClass().getResource("gfx/icon.png")).getImage()); 

    // Center the frame 
    Dimension frameSize = Toolkit.getDefaultToolkit().getScreenSize(); 
    window.setLocation((frameSize.width - window.getSize().width)/2, (frameSize.height - window.getSize().height)/2); 

    // Show the window 
    window.setVisible(true); 

    // And focus the FinalSnake applet 
    finalSnake.requestFocusInWindow(); 
} 

code de la FinalSnake Applet:

@Override 
public void keyPressed(KeyEvent e) { 
    if (this.world == null) { 
     if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_LEFT) { 
      this.gameType--; 

      if (this.gameType == 0) { 
       this.gameType = 2; 
      } 
     } 

     if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_RIGHT) { 
      this.gameType++; 

      if (this.gameType == 3) { 
       this.gameType = 1; 
      } 
     } 

     if (e.getKeyCode() == KeyEvent.VK_ENTER || e.getKeyCode() == KeyEvent.VK_SPACE) { 
      this.world = new World(this.gameType); 
     } 
    } else { 
     if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { 
      this.world = null; 
     } 
    } 
} 

quelqu'un L'espoir peut effacer cela pour moi ... Thnx

+0

Pourquoi 'FinalSnake' est-il une applet plutôt qu'une' JPanel'? S'il s'agissait d'un panneau, il pourrait être ajouté à une applet ou à une application, et les problèmes de mise au point disparaîtraient très probablement. –

Répondre

0

Vous pouvez en utilisant la méthode consume() sur votre variable KeyEvent. Il dit:

Consomme cet événement afin qu'il pas traité de la manière par défaut par la source qui a pris naissance il.

Cela remplacera la manière par défaut je suppose.

public void keyPressed(KeyEvent e) { 
//Do your actions 
e.consume(); 
} 
+0

Le problème est que l'événement n'est pas appelé du tout pour la clé d'entrée et d'échappement, pas qu'il soit manipulé trop de fois. – FinalFrager

+0

Il y avait une question: [stackoverflow] (http://stackoverflow.com/questions/1053673/japplet-not-stopping-or-reacting-to-keyboard-input), et [ici] (http: // www. daniweb.com/software-development/java/threads/155991), peut-être que cela vous aidera à avoir une idée. – JMelnik

+0

Pas vraiment ... mon application fonctionne à 100% en courant à travers eclipse ... Quand je l'exporte vers un fichier exécutable (pour la distribution) ça ne marche plus ... – FinalFrager