开发者

java keylistener not working after export

开发者 https://www.devze.com 2023-03-01 05:31 出处:网络
I have a JFrame that hosts my Applet. There is a KeyListener on the applet to handle arrow keys and the enter/escape key.

I have a JFrame that hosts my Applet. There is a KeyListener on the applet to handle arrow keys and the enter/escape key.

When I run my JFrame in Eclipse, everything works fine, the arrow keys respond as well as the enter and escape key.

However, when I export my project to a Executable Jar file... the arrow keys still work, but the enter and escape key do not. How can I resolve this issue?

The code in the main class:

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 t开发者_开发技巧he 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 from the 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;
        }
    }
}

Hope someone can clear this out for me... Thnx


You can try using consume() method on your KeyEvent variable. It says:

Consumes this event so that it will not be processed in the default manner by the source which originated it.

This will override default manner I guess.

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

精彩评论

暂无评论...
验证码 换一张
取 消