为什么我的代码不在keyPressed()方法中? (即使我按键)

时间:2014-04-09 15:09:43

标签: java graphics keypress keylistener keyevent

我试图制作一个破砖游戏。显然,我仍然在努力开始。我需要我的"阻止"移动,但如果我按下某个键,我的代码甚至不会进入keyPressed() - 方法。有人知道为什么吗?

import java.util.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class VGKernel extends JPanel implements KeyListener {

public Rectangle screen, ball, block; // The screen area and ball location/size.
public Rectangle bounds;  // The boundaries of the drawing area.
public JFrame frame; // A JFrame to put the graphics into.
public VGTimerTask vgTask; // The TimerTask that runs the game.
public boolean down, right; // Direction of ball's travel.

public VGKernel(){
    super();
    screen = new Rectangle(0, 0, 600, 400);
    ball   = new Rectangle(0, 0, 20, 20);
    block = new Rectangle(0, 350, 40, 10);
    bounds = new Rectangle(0, 0, 600, 400); // Give some starter values.
    frame = new JFrame("VGKernel");
    vgTask = new VGTimerTask();
    addKeyListener(this);
}

class VGTimerTask extends TimerTask{
    public void run(){
      moveBall();
      frame.repaint();
    }
  }

// Now the instance methods:
  public void paintComponent(Graphics g){
    // Get the drawing area bounds for game logic.
    bounds = g.getClipBounds();
    // Clear the drawing area, then draw the ball.
    g.clearRect(screen.x, screen.y, screen.width, screen.height);
    g.fillRect(ball.x, ball.y, ball.width, ball.height);
    g.fillRect(block.x, block.y, block.width, block.height);
  }

  public void moveBall(){
  // Ball should really be its own class with this as a method.
    if (right) ball.x+=1; // If right is true, move ball right,
    else ball.x-=1;       // otherwise move left.
    if (down)  ball.y+=1; // Same for up/down.
    else ball.y-=1;
    if (ball.x > (bounds.width - ball.width)) // Detect edges and bounce.
      { right = false; ball.x = bounds.width -  ball.width; }
    if (ball.y > (bounds.height - ball.height))
      { down  = false; ball.y = bounds.height - ball.height;}
    if (ball.x <= 0) { right = true; ball.x = 0; }
    if (ball.y <= 0) { down  = true; ball.y = 0; }
  }

  public void keyPressed(KeyEvent evt) {

      if(evt.getKeyCode() == KeyEvent.VK_G && block.x > 0) {
          block.x -= 10;
      }

      if(evt.getKeyCode() == KeyEvent.VK_H && block.x < 560) {
          block.x += 10;
      }
  }

  public void keyTyped(KeyEvent evt){ } 
  public void keyReleased(KeyEvent evt){ }

  public static void main(String arg[]){
    java.util.Timer vgTimer = new java.util.Timer();  // Create a Timer.
    VGKernel panel = new VGKernel(); // Create and instance of our kernel.

    // Set the intial ball movement direction.
    panel.down = true;
    panel.right = true;

    // Set up our JFRame
    panel.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel.frame.setSize(panel.screen.width, panel.screen.height);
    panel.frame.setContentPane(panel); 
    panel.frame.setVisible(true);

    // Set up a timer to do the vgTask regularly.
    vgTimer.schedule(panel.vgTask, 0, 10);
  }

}

1 个答案:

答案 0 :(得分:0)

setFocusable(true);

在构造函数中。