KeyEvents不会触发

时间:2013-06-05 08:52:36

标签: java swing response keylistener key-events

我无法弄清楚为什么KeyEvents在理论上会被按下/释放时不会触发并在网络上找到满足该问题的答案。

当按下/释放四个实现键中的任何一个时,没有任何反应。

(我将代码缩减到我认为需要的内容,但不要介意全部发布)

//Outter Class imports
import javax.swing.*;

import java.util.*;
import java.util.ArrayList;

import java.awt.*;
import java.awt.Image;
import java.awt.image.*;
import javax.imageio.*;


//Inner Class(1) imports
import java.awt.event.*;    
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Game extends JPanel {

      //fields...


    public Game(){
        GWin = new JFrame("Space Invaders");
        JPanel panel = new JPanel();
        panel.setSize(new Dimension(Length, Height));
        panel.setLayout(null);
        addKeyListener(new KeyInputHandler());
        GWin.add(panel);

        WindowListener exitListener = new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                int confirm = JOptionPane.showOptionDialog(null, "Are You Sure to Close Application?", "Exit Confirmation",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);

                if (confirm == 0) {
                   GWin.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);      
                }  
            }
        };

        GWin.addWindowListener(exitListener);

        GWin.setExtendedState(Frame.MAXIMIZED_BOTH); 
        GWin.setUndecorated(true);  

        GWin.setVisible(true);
        requestFocus();

        startGame();
    }

    public static void main(String arg[]) { 
        SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new WIN(Length ,Height);
                }
            });
         }

    private class KeyInputHandler extends KeyAdapter {
    /**
     * A class to handle keyboard input from the user. The class
     * handles both dynamic input during game play, i.e. left/right 
     * and shoot, and more static type input (i.e. press any key to
     * continue)
     */
        @Override 
        public void keyPressed(KeyEvent e) {
            if (waitingForKeyPress) {
                return;
            }   

            int key = e.getKeyCode();
            if (key == KeyEvent.VK_A){
                leftPressed = true;
            }
            if (key == KeyEvent.VK_F) {
                    rightPressed = true;
            }
            if (key == KeyEvent.VK_SPACE) {
                    firePressed = true;                                                                                                                                                                                                                                                                                     
            }
        }

        @Override 
        public void keyReleased(KeyEvent e) {
            if (waitingForKeyPress) {
                return;
            }   

            int key = e.getKeyCode();

            if (key == KeyEvent.VK_A) {
                    leftPressed = false;
            }
            if (key == KeyEvent.VK_F) {
                    rightPressed = false;
            }
            if (key == KeyEvent.VK_SPACE) {
                    firePressed = false;                                                                                                                                                                                                                                                                                        
            }
        }

        @Override
        public void keyTyped(KeyEvent e){   
            int key = e.getKeyCode();
            if (waitingForKeyPress) {
                if (pressCount == 1) {
                    waitingForKeyPress = false;
                    pressCount = 0;
                } else {
                    pressCount++;
                }
            }

            if (e.getKeyChar() == 27) {
                GWin.dispose();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我的第一个猜测是你的JPanel后代Game还没有专注。尝试:

this.setFocusable(true);
this.requestFocus();

在请求焦点之前。另外一定要阅读有关Swing焦点管理如何工作的Oracle文档 - 它会为您节省大量时间,因为存在更多陷阱(异步行为......): - )!

祝你好运!