问题描述:java se GUI编程,写一个坦克大战游戏。每次在键盘的监听的时候都被卡住住了。第一次是看书没有看到gui这一章。感觉java gui编程有点难,忘而却步。这次,明明已经看书读了gui这章,图形界面编程等。老师掌握不准键盘监听这一神秘的事件!
我发现,把一个JPanel放入JFrame中,如果画图,那么就不可以监听键盘,但是依旧可以监听窗口,依旧可以监听鼠标,唯独不能监听键盘!如果监听到键盘,那么就不允许画图。
本人想寻找一个既可以在JPanel 上画图有可以监听键盘的方法!下面是写的几个坦克类的代码,请大神指出错在哪儿?我按了无数次,键盘监听事件根本就没有触发过!
import java.awt.*;
import javax.swing.*;
public class TankClient extends JFrame {
MyPanel mp = null;
public TankClient() {
mp = new MyPanel();
mp.setFocusable(true);
this.addMouseListener(mp);
this.addKeyListener(mp);
this.addWindowListener(mp);
this.add(mp);
}
public static void main(String[] args) {
TankClient frame = new TankClient();
frame.setTitle("TankGame1.0");
frame.setSize(400, 300);
frame.setLocation(300, 250);
frame.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import cn.com.shizongger.tank.*;
import javax.swing.JPanel;
public class MyPanel extends JPanel implements WindowListener, MouseListener,
KeyListener {
Hero hero = null;
Enemy enemy = null;
public MyPanel() {
hero = new Hero(10, 10);
//enemy = new Enemy(40, 10);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(hero.getX(), hero.getY(), 20, 20);
//g.fillOval(enemy.getX(), enemy.getY(), 20, 20);
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
if(arg0.getKeyCode() == KeyEvent.VK_DOWN) {
System.out.println(arg0.getKeyChar());
hero.setY(hero.getY() + hero.SPEDD);
}
System.out.println(arg0.getKeyChar() +"键被按下");
}
@Override
public void mouseClicked(MouseEvent arg0) {
System.out.println("鼠标被按下");
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
}
public class Tank {
private int x = 10;
private int y = 10;
public final int SPEDD = 5;
public static int dirction = 0;
public Tank() {
}
public Tank(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public static int getDirction() {
return dirction;
}
public static void setDirction(int dirction) {
Tank.dirction = dirction;
}
public int getSPEDD() {
return SPEDD;
}
}
public class Hero extends Tank {
public Hero() {
}
public Hero(int x, int y) {
super(x, y);
}
}
public class Enemy extends Tank {
public Enemy() {
}
public Enemy(int x, int y) {
super(x, y);
}
}
------解决思路----------------------
把keyType里面的代码弄到keyPressed里面去,keyType中keyEvent用getkeyCode返回值总是0
------解决思路----------------------
TankClient构造函数里增加两句:
mp.addMouseListener(mp);
mp.addKeyListener(mp);
让MyPanel也参与监听
因为按键的Event事件先传到MyPanel实例
如果没处理,默认就不传到框架里了
监听器的事件传递
------解决思路----------------------
去掉第十行就好了