【问题标题】:Focused Windows in XP and 7 [duplicate]XP和7中的重点Windows [重复]
【发布时间】:2014-04-07 17:04:23
【问题描述】:

我正在运行一个简单的 Pong 游戏。当用户按下空格键时,游戏开始。在 Windows XP 中运行良好,但在 Windows 7 中变得很奇怪。如果按空格键,然后单击游戏窗口,它就会运行。如果你先点击,然后点击空格,什么都没有。我已经粘贴了下面的主要课程。

 public class Game extends JPanel {

static final int FW = 400;
static final int FH = 300;

Ball ball = new Ball(this);
Paddle paddleL = new Paddle(this);
PaddleAI paddleR = new PaddleAI(this);
public static boolean go;
static KeyListener key;


public Game(){
   setFocusable(true);
   addKeyListener(key = new KeyListener(){
       @Override
        public void keyTyped(KeyEvent e){

       }
       @Override
        public void keyReleased(KeyEvent e){
           paddleL.keyReleased(e);
           if(e.getKeyCode() == KeyEvent.VK_SPACE)
               go = true;
       }
       @Override
       public void keyPressed(KeyEvent e){
           paddleL.keyPressed(e);
           if(e.getKeyCode()==KeyEvent.VK_SPACE)
               go = true;
       }
   });


 }


void setup(){
   ball.setup();
   paddleL.setup();
   paddleR.setup();
}

void move(){
    ball.move();
    paddleL.move();
    paddleR.move();
}


public void paint(Graphics g){
    super.paint(g);
    ball.paint(g);
    paddleL.paint(g);
    paddleR.paint(g);
}

public static void main(String[] args) throws InterruptedException{

    //final int FW = 400;
   // final int FH = 300;

    JFrame frame = new JFrame();
    Game game = new Game();

    game.setBackground(Color.black);
    frame.add(game);
    frame.getContentPane().setBackground(Color.cyan);
    frame.setBackground(Color.black);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(FW, FH);
    frame.setLocationRelativeTo(null);
    frame.setTitle("Pong");
    frame.setResizable(true);
    frame.setVisible(true);


   game.setup();

    while(true){

        if(go){
            game.move();
            game.repaint();
            Thread.sleep(10);
        }

    }
}

}

【问题讨论】:

标签: java swing windows-7 keylistener


【解决方案1】:

您将要使用键绑定而不是键侦听器。 KeyListener 对关注的组件非常挑剔,这就是您遇到的问题。下面是一个如何使用Key Binding 的简单示例:

import java.awt.event.*;
import javax.swing.*;

public class KeyBindings extends Box{
    public KeyBindings(){
        super(BoxLayout.Y_AXIS);
        final JTextPane textArea = new JTextPane();
        textArea.insertComponent(new JLabel("Text"));
        add(textArea);

        Action action = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText("New Text");
            }};
         String keyStrokeAndKey = "control SPACE";
         KeyStroke keyStroke = KeyStroke.getKeyStroke(keyStrokeAndKey);
         textArea.getInputMap().put(keyStroke, keyStrokeAndKey);
         textArea.getActionMap().put(keyStrokeAndKey, action);
    }


    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new KeyBindings());
        frame.pack();
        frame.setVisible(true);
    }
}

【讨论】:

    猜你喜欢
    • 2011-12-23
    • 2020-09-24
    • 2014-12-14
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多