JAVA的键盘监听为什么没有反应
麻烦各位大侠解救一下!!packagecn.bjsxt.plane;importjava.awt.Graphics;importjava.awt.Image;import...
麻烦各位大侠解救一下!!
package cn.bjsxt.plane;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import cn.bjsxt.util.GameUtil;
import cn.bjsxt.util.MyFrame;
public class PlaneGameFrame extends MyFrame{
Image bg = GameUtil.getImage("images/bg.jpg");
Plane p = new Plane("images/plane.png",50,50);
public void paint(Graphics g){
g.drawImage(bg, 0, 0, null);
p.draw(g);
}
public static void main(String[] args) {
new PlaneGameFrame().lanuchFrame();
}
//重写父类的LAUNCHFRAME方法,在这里被调用
public void launchFrame(){
super.lanuchFrame();
//增加键盘的监听
addKeyListener(new KeyMonitor());
}
//内部类,可以方便的使用外部类的普通属性
class KeyMonitor extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("按下"+e.getKeyCode());
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("弹起"+e.getKeyCode());
}
}
}
父类是MyFrame
我启动了一下程勋,但是按下键盘后,控制台上没有任何输出,左对右对都没有发现问题,实在不知道错在哪里啊!我已经让鼠标点击的程序窗口了,然后才按键盘的!
package cn.bjsxt.util;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyFrame extends Frame{
public void lanuchFrame(){
setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);
setLocation(100, 100);
setVisible(true);
new PaintThread().start();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0); 展开
package cn.bjsxt.plane;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import cn.bjsxt.util.GameUtil;
import cn.bjsxt.util.MyFrame;
public class PlaneGameFrame extends MyFrame{
Image bg = GameUtil.getImage("images/bg.jpg");
Plane p = new Plane("images/plane.png",50,50);
public void paint(Graphics g){
g.drawImage(bg, 0, 0, null);
p.draw(g);
}
public static void main(String[] args) {
new PlaneGameFrame().lanuchFrame();
}
//重写父类的LAUNCHFRAME方法,在这里被调用
public void launchFrame(){
super.lanuchFrame();
//增加键盘的监听
addKeyListener(new KeyMonitor());
}
//内部类,可以方便的使用外部类的普通属性
class KeyMonitor extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("按下"+e.getKeyCode());
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("弹起"+e.getKeyCode());
}
}
}
父类是MyFrame
我启动了一下程勋,但是按下键盘后,控制台上没有任何输出,左对右对都没有发现问题,实在不知道错在哪里啊!我已经让鼠标点击的程序窗口了,然后才按键盘的!
package cn.bjsxt.util;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyFrame extends Frame{
public void lanuchFrame(){
setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);
setLocation(100, 100);
setVisible(true);
new PaintThread().start();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0); 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询