[英]Why wont my Java program acknowledge any key strokes using KeyListener?
I have literally no idea why my program isn't recognizing keyboard input. 我完全不知道为什么我的程序没有识别键盘输入。 I have places print statements throughout the program to determine the issue, and I have determined that the keyPressed method never activates.
我在整个程序中放置了print语句来确定问题,并且我已经确定keyPressed方法永远不会激活。 This is for a game which I am making for a class project, and yes I am a relatively beginner programmer.
这是我为一个课程项目制作的游戏,是的,我是一个相对初学的程序员。 Thanks in advance!
提前致谢! (Code below)
(以下代码)
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
public class Dodger extends JApplet implements Runnable, KeyListener {
Thread myThread;
public Image bg;
public Image pic;
public boolean loaded;
public int cx, cy, speed, x, y;
public void init(){
setSize(800,800);
loaded = false;
x = 2;
y = 400;
cx = 0;
cy = 0;
speed = 3;
myThread = new Thread(this);
myThread.start();
addKeyListener(this);
}
public void run(){
loadpic();
repaint();
while (myThread!=null)
{
try{
myThread.sleep(18);
}catch(InterruptedException e){
e.printStackTrace();
}
repaint();
}
}
public void upMotion(){
cy = cy + speed;
}
public void downMotion(){
cy = cy - speed;
}
public void leftMotion(){
cx = cx - speed;
}
public void rightMotion(){
cx = cx + speed;
}
@Override
public void keyPressed(KeyEvent k) {
if (k.getKeyCode() == KeyEvent.VK_LEFT) {
System.out.println("work");
leftMotion();
}
if (k.getKeyCode() == KeyEvent.VK_RIGHT) {
rightMotion();
}
if (k.getKeyCode() == KeyEvent.VK_UP) {
upMotion();
}
if (k.getKeyCode() == KeyEvent.VK_DOWN) {
downMotion();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
public void loadpic(){
bg = new ImageIcon(getClass().getResource("back.png")).getImage();
pic = new ImageIcon(getClass().getResource("smile.png")).getImage();
loaded = true;
repaint();
}
public void paint(Graphics g){
g.drawImage(bg, 0, 0, this);
g.drawImage(pic, cx, cy, this);
}
}
The key events are detected just fine when the applet is focusable & has focus. 当applet可聚焦并具有焦点时,可以检测到关键事件。 Managing focus has always been a problem with applets.
管理焦点一直是applet的问题。 The problem is mostly that Sun never bothered to specify the focus behavior that should ideally apply to a mixed page of focusable HTML elements and applet(s).
问题主要在于Sun从不打算指定应该理想地应用于可聚焦HTML元素和applet的混合页面的焦点行为。
As per Tom's advice, add to the end of init()
根据Tom的建议,添加到
init()
的末尾
setFocusable(true);
To be safe, also override: 为了安全起见,还要覆盖:
public void start() {
this.requestFocusInWindow();
}
As an aside, generally it is better to use key bindings in Swing. 顺便说一句,通常最好在Swing中使用键绑定 。 They also require the applet to have input focus.
他们还要求applet具有输入焦点。
First off, you probably want to separate out your class from your KeyListener, as it makes it a bit harder to read. 首先,您可能希望将您的类与KeyListener分开,因为它使得它更难以阅读。
Next, you want to get rid of the bare Thread
and wrap it in a timer. 接下来,您想要摆脱裸
Thread
并将其包装在计时器中。
import javax.swing.Timer;
class Dodger extends JApplet {
Timer imageUpdater; //replaces Thread
/*...*/
public void init() {
/*etc*/
loadpic();
int repaintInterval = 100;
imageUpdater = new Timer(repaintInterval,
new ActionListener() {
public void actionPerformed(ActionEvent e) {
repaint();
}
}
);
imageUpdater.start();
addKeyListener(new KeyHandler());
setFocusable(true);
}
/*...*/
private class KeyHandler extends KeyAdapter {
/* Note that with this implementation, you do not have to override
* unnecessary methods, as KeyAdapter is an abstract class that
* implements all of the methods of KeyListener.
*/
@Override
public void keyPressed(KeyEvent e) {
/*...*/
}
}
/*...*/
}
Most of this is just code cleanup - the actual problem may be fixed (according to Tom, see comments) with the setFocusable(true)
. 大多数情况只是代码清理 - 实际问题可能是使用
setFocusable(true)
修复的(根据Tom,见注释setFocusable(true)
。
I'm not really sure if it applies to Applets, but your Thread
may not allowing the event dispatching to occur. 我不确定它是否适用于Applet,但您的
Thread
可能不允许发生事件调度。
Try to run your "work" with SwingWorker . 尝试使用SwingWorker运行“工作”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.