所以我有我的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
}
}