1

我很难弄清楚为什么 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();
            }
        }
    }
}
4

1 回答 1

0

我的第一个猜测是您的 JPanel 后代Game尚未获得焦点。尝试:

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

在请求焦点之前。还请务必阅读 Oracle 关于 Swing 焦点管理如何工作的文档 - 它会为您节省很多时间,因为存在更多陷阱(异步行为......):-)!

祝你好运!

于 2013-06-05T09:04:48.527 回答