为什么这个keyListener不工作?我怎样才能让它发挥作用?

时间:2013-09-19 22:04:54

标签: java keylistener

所以我的keyListener名为TAdapter。由于某种原因,我不能让这工作正常。我已将焦点设置到面板上,但仍然无法正常工作。我搜索并搜索了网络,发现为什么这不能正常工作。我是java新手,完全难倒

import java.awt.*;
import javax.swing.JPanel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.TimerTask;
import java.util.Timer;
import java.awt.Toolkit;

public class Game extends JPanel implements Shared{

private static Brick bricks[];
private static Ball ball;
private static Paddle paddle;
Timer timer;

public Game(){
super();
 this.addKeyListener(new TAdapter());
 this.setFocusable(true);
this.requestFocusInWindow();
  setSize(Shared.WIDTH, Shared.HEIGHT);
 bricks = new Brick[100];

 timer = new Timer();
 timer.scheduleAtFixedRate(new ScheduleTask(), 1000, 10);
}

public void addNotify(){
  super.addNotify();
  gameInit();
 }

public static void gameInit(){
  ball = new Ball();
  paddle = new Paddle();

  GradientPaint gp = new GradientPaint(75, 75, Color.BLACK, 95, 95, Color.GREEN,     true);
  int k = 0;
  for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
          switch(i){
          case 0:
              gp = new GradientPaint(75, 75, Color.DARK_GRAY, 95, 95,     new Color(255, 0, 255), true);
              break;
          case 1:
              gp = new GradientPaint(75, 75, Color.DARK_GRAY, 95, 95, new Color(255, 20, 127), true);
              break;
          case 2:
              gp = new GradientPaint(75, 75, Color.DARK_GRAY, 95, 95, new Color(255, 0, 0), true);
              break;
          case 3:
              gp = new GradientPaint(75, 75, Color.DARK_GRAY, 95, 95, new Color(255, 127, 0), true);
              break;
          case 4:
              gp = new GradientPaint(75, 75, Color.DARK_GRAY, 95, 95, new Color(255, 255, 0), true);
              break;
          case 5:
              gp = new GradientPaint(75, 75, Color.DARK_GRAY, 95, 95, new Color(0, 255, 0), true);
              break;
          case 6:
              gp = new GradientPaint(75, 75, Color.DARK_GRAY, 95, 95, new Color(0, 255, 127), true);
              break;
          case 7:
              gp = new GradientPaint(75, 75, Color.DARK_GRAY, 95, 95, new Color(0, 127, 255), true);
              break;
          case 8:
              gp = new GradientPaint(75, 75, Color.DARK_GRAY, 95, 95, new Color(0, 0, 255), true);
              break;
          case 9:
              gp = new GradientPaint(75, 75, Color.DARK_GRAY, 95, 95, new Color(127, 0, 255), true);
              break;
          }
          bricks[k] = new Brick((j*BRICK_WIDTH) + (j*BRICK_SEP), (i*BRICK_HEIGHT) +  BRICK_Y_OFFSET+(i*BRICK_SEP), gp);
          k++;
      }
  }
}

public void paint(Graphics g){
  super.paint(g);
  GradientPaint gp = new GradientPaint(75, 75, Color.BLACK, 95, 95, Color.RED, true);
  Graphics2D g2 = (Graphics2D) g;
  g2.setPaint(gp);
  g2.fillOval(ball.getX(), ball.getY(), BALL_RADIUS, BALL_RADIUS);
  g2.fillRoundRect((Shared.WIDTH/2) - PADDLE_WIDTH/2, Shared.HEIGHT - PADDLE_Y_OFFSET*2, PADDLE_WIDTH, PADDLE_HEIGHT, 3, 3);
  for(int i = 0; i< 100; i++){
          if(!bricks[i].isDestroyed()){
              g2.setPaint(bricks[i].getPaint());
              g2.fillRoundRect(bricks[i].getX(), bricks[i].getY(), bricks[i].getWidth(), bricks[i].getHeight(), 5, 5);
          }

    }
  Toolkit.getDefaultToolkit().sync();
  g2.dispose();
}

private class TAdapter extends KeyAdapter{

    public void keyReleased(KeyEvent e){
        paddle.keyReleased(e);
    }

    public void keyPressed(KeyEvent e){
        paddle.keyPressed(e);
    }
}
class ScheduleTask extends TimerTask{
    public void run(){
        ball.move();
        paddle.move();
        checkCollision();
        repaint();
    }
}
public void stopGame(){
    timer.cancel();
}
public void checkCollision() {

    if (ball.getRect().getMaxY() > Shared.HEIGHT) {
        stopGame();
    }

    for (int i = 0, j = 0; i < 100; i++) {
        if (bricks[i].isDestroyed()) {
            j++;
        }
        if (j == 100) {
            stopGame();
        }
    }

    if ((ball.getRect()).intersects(paddle.getRect())) {

        int paddleLPos = (int)paddle.getRect().getMinX();
        int ballLPos = (int)ball.getRect().getMinX();

        int first = paddleLPos + 8;
        int second = paddleLPos + 16;
        int third = paddleLPos + 24;
        int fourth = paddleLPos + 32;

        if (ballLPos < first) {
            ball.setXDir(-1);
            ball.setYDir(-1);
        }

        if (ballLPos >= first && ballLPos < second) {
            ball.setXDir(-1);
            ball.setYDir(-1 * ball.getYDir());
        }

        if (ballLPos >= second && ballLPos < third) {
            ball.setXDir(0);
            ball.setYDir(-1);
        }

        if (ballLPos >= third && ballLPos < fourth) {
            ball.setXDir(1);
            ball.setYDir(-1 * ball.getYDir());
        }

        if (ballLPos > fourth) {
            ball.setXDir(1);
            ball.setYDir(-1);
        }


    }


    for (int i = 0; i < 100; i++) {
        if ((ball.getRect()).intersects(bricks[i].getRect())) {

            int ballLeft = (int)ball.getRect().getMinX();
            int ballHeight = (int)ball.getRect().getHeight();
            int ballWidth = (int)ball.getRect().getWidth();
            int ballTop = (int)ball.getRect().getMinY();

            Point pointRight =
                new Point(ballLeft + ballWidth + 1, ballTop);
            Point pointLeft = new Point(ballLeft - 1, ballTop);
            Point pointTop = new Point(ballLeft, ballTop - 1);
            Point pointBottom =
                new Point(ballLeft, ballTop + ballHeight + 1);

            if (!bricks[i].isDestroyed()) {
                if (bricks[i].getRect().contains(pointRight)) {
                    ball.setXDir(-1);
                }

                else if (bricks[i].getRect().contains(pointLeft)) {
                    ball.setXDir(1);
                }

                if (bricks[i].getRect().contains(pointTop)) {
                    ball.setYDir(1);
                }

                else if (bricks[i].getRect().contains(pointBottom)) {
                    ball.setYDir(-1);
                }

                bricks[i].setDestroyed(true);
            }
        }
    }
}
}`

然后这是主要的

import javax.swing.*;

public class BreakOut extends JFrame{

/**
 * 
 */
private static final long serialVersionUID = 1L;
static Game game =new Game();

public BreakOut()
{
    add(game);
    setTitle("Breakout");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(Shared.WIDTH, Shared.HEIGHT);
    setResizable(false);
    setVisible(true);
    setIgnoreRepaint(true);
}
public static void main(String arg[]){
      new BreakOut();a
      }

}

2 个答案:

答案 0 :(得分:0)

您的主要听众只是将事件发送到paddle

您可能希望包含paddle.keyPressed(e)paddle.keyReleased(e)的代码(或至少查看代码);但实际上,您可能不应该让您的paddle对象直接处理密钥。将你的按键转换为游戏内有意义的调用很好,比如paddle.moveUp()或其他什么。

答案 1 :(得分:-1)

我不确定共享接口来自哪里,但是尝试实现KeyListener。 “公共类游戏扩展了JPanel实现Shared,KeyListner”,如果你继续使用共享,但就像我说idk它的目的是什么或它做什么就在我的头顶。