Java 为什么';我的代码不在keyPressed()方法中吗?(即使我按了键)
我正在尝试创建一个砖匠游戏。很明显,我还在为它的开始而挣扎。我需要移动我的“块”,但如果我按某个键,我的代码甚至不会进入keyPressed()方法。有人知道为什么吗Java 为什么';我的代码不在keyPressed()方法中吗?(即使我按了键),java,graphics,keypress,keylistener,keyevent,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 Rectangl
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);
}
import java.util.*;
导入java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类VGKernel扩展了JPanel实现了KeyListener{
公共矩形屏幕、球、块;//屏幕区域和球的位置/大小。
公共矩形边界;//绘图区域的边界。
public JFrame;//要将图形放入的JFrame。
public VGTimerTask vgTask;//运行游戏的TimerTask。
公共布尔向下,右;//球的移动方向。
公共VGKernel(){
超级();
屏幕=新矩形(0,0,600,400);
ball=新矩形(0,0,20,20);
块=新矩形(0,350,40,10);
bounds=新矩形(0,0,600,400);//给出一些起始值。
框架=新的JFrame(“VGKernel”);
vgTask=新的VGTimerTask();
addKeyListener(此);
}
类VGTimerTask扩展了TimerTask{
公开募捐{
移动球();
frame.repaint();
}
}
//现在,实例方法:
公共组件(图形g){
//获取游戏逻辑的绘图区域边界。
bounds=g.getClipBounds();
//清除绘图区域,然后绘制球。
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);
}
公共空间移动球(){
//Ball应该是它自己的类,并以此作为方法。
如果(右)ball.x+=1;//如果right为true,则向右移动ball,
else ball.x-=1;//否则向左移动。
如果(向下)ball.y+=1;//向上/向下也是如此。
else-ball.y-=1;
if(ball.x>(bounds.width-ball.width))//检测边缘和反弹。
{right=false;ball.x=bounds.width-ball.width;}
if(ball.y>(bounds.height-ball.height))
{down=false;ball.y=bounds.height-ball.height;}
如果(球xPut
在构造函数中。确保JPanel具有。
setFocusable(true);