Java新手求助,关于事件监听

lanxinglinxi 2015-08-21 09:08:04
我在看韩顺平的教程时,自己尝试写的坦克移动小程序一直不能动,希望各位大神轻拍,指出哪里出问题了,我实在是没找到。。。


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class demo {
JFrame frame;
MyPanel panel;

public demo() {
frame = new JFrame();
panel = new MyPanel();
panel.addKeyListener(panel);// 添加监听
frame.add(panel);
frame.setTitle("坦克");
frame.setSize(500, 500);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public static void main(String[] args) {
demo demo = new demo();
}
}

class Tank {
int x;
int y;
int direction;// 方向:0-上,1-右,2-下,3-左
int interval;// 步长

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 int getDirection() {
return direction;
}

public void setDirection(int direction) {
this.direction = direction;
}

public int getInterval() {
return interval;
}

public void setInterval(int interval) {
this.interval = interval;
}

public Tank(int x, int y, int direction, int interval) {
this.x = x;
this.y = y;
this.direction = direction;
this.interval = interval;
}

public void MoveUp() {
y -= interval;
}

public void MoveDown() {
y += interval;
}

public void MoveLeft() {
x -= interval;
}

public void MoveRight() {
x += interval;
}
}

class Enemy extends Tank {
public Enemy(int x, int y, int direction, int interval) {
super(x, y, direction, interval);
}
}

class Ally extends Tank {
public Ally(int x, int y, int direction, int interval) {
super(x, y, direction, interval);
}
}

class MyPanel extends JPanel implements KeyListener {
int x;
int y;
int direction;
int interval;
Ally a1;

public MyPanel() {
x = 100;
y = 100;
direction = 0;
interval = 10;
a1 = new Ally(x, y, direction, interval);
}

public void paint(Graphics gra) {// 第一次显示的时候会被自动调用,窗口最小最大
super.paint(gra);
drawTank(a1.getX(), a1.getY(), a1.getDirection(), gra);
}

public void drawTank(int x, int y, int direction, Graphics gra) {
switch (direction) {
case 0: {
gra.draw3DRect(x, y, 5, 30, false);
gra.draw3DRect(x + 15, y, 5, 30, false);
gra.draw3DRect(x + 5, y + 5, 10, 20, false);
gra.fillOval(x + 5, y + 10, 10, 10);
gra.drawLine(x + 10, y, x + 10, y + 15);
break;
}
case 1: {
gra.draw3DRect(x, y, 30, 5, false);
gra.draw3DRect(x, y + 15, 30, 5, false);
gra.draw3DRect(x + 5, y + 5, 20, 10, false);
gra.fillOval(x + 10, y + 5, 10, 10);
gra.drawLine(x+15, y + 10, x + 30, y + 10);
break;
}
case 2: {
gra.draw3DRect(x, y, 5, 30, false);
gra.draw3DRect(x + 15, y, 5, 30, false);
gra.draw3DRect(x + 5, y + 5, 10, 20, false);
gra.fillOval(x + 5, y + 10, 10, 10);
gra.drawLine(x + 10, y + 30, x + 10, y + 15);
break;
}
case 3: {
gra.draw3DRect(x, y, 30, 5, false);
gra.draw3DRect(x, y + 15, 30, 5, false);
gra.draw3DRect(x + 5, y + 5, 20, 10, false);
gra.fillOval(x + 10, y + 5, 10, 10);
gra.drawLine(x, y + 10, x + 15, y + 10);
break;
}
default:
break;
}
}

public void keyPressed(KeyEvent e) {
System.out.println("(" + a1.getX() + "," + a1.getY() + ")");
if (e.getKeyCode() == KeyEvent.VK_UP) {
a1.setDirection(0);
a1.MoveUp();
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
a1.setDirection(2);
a1.MoveDown();
} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
a1.setDirection(3);
a1.MoveLeft();
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
a1.setDirection(1);
a1.MoveRight();
}
repaint();
}

public void keyReleased(KeyEvent e) {
}

public void keyTyped(KeyEvent e) {
}

}
...全文
245 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
三仙半 2015-08-25
  • 打赏
  • 举报
回复
在demo的构造函数中启动一个线程,不断的调用panel的requestFocusInWindow()方法,直到panel的isFocusOwner()方法的返回值为true,这样,就不用先点击依次这个panel了,另外,给panel注册个焦点事件监听,一旦失去焦点,立刻调用panel的requestFocusInWindow()方法申请获得焦点。
lanxinglinxi 2015-08-23
  • 打赏
  • 举报
回复
引用 2 楼 tuyugg123 的回复:
KeyEvent.VK_DOWN 这个是数字键盘的上下左右键哦,换成 VK_DOWN 试试看
看我一楼的回复
MarkTuna 2015-08-23
  • 打赏
  • 举报
回复
KeyEvent.VK_DOWN 这个是数字键盘的上下左右键哦,换成 VK_DOWN 试试看
lanxinglinxi 2015-08-22
  • 打赏
  • 举报
回复
自己找到的答案 http://stackoverflow.com/questions/11487369/jpanel-doesnt-response-to-keylistener-event JPanel is not Focusable by default. That is, it can not respond to focus related events, meaning that it can not respond to the keyevents. I would suggest trying to setFocusable on the pane to true and trying again. Make sure you click the panel first to make sure it receives focus.

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧