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
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. –