java小程序出错!!!实现键盘方向键移动不了,帮我看看代码那里错,谢谢!!
importjava.awt.*;importjava.awt.Graphics;importjava.applet.Applet;importjava.awt.even...
import java.awt.*;
import java.awt.Graphics;
import java.applet.Applet;
import java.awt.event.*;
import java.awt.event.KeyEvent;
public class TestKey extends Applet implements KeyListener{
int x,y;
public void init() {
addKeyListener(this);
x=30;
y=30;
}
public void keyTyped(KeyEvent e) {
if(e.getKeyCode()==e.VK_UP)
y+=10;
else if(e.getKeyCode()==e.VK_DOWN)
y-=10;
else if(e.getKeyCode()==e.VK_LEFT)
x+=10;
else if
(e.getKeyCode()==e.VK_RIGHT)
x-=10;
}
public void paint(Graphics g){
g.drawString("java程序设计", x, y );
}
public void keyReleased( KeyEvent e ) { }
public void keyPressed( KeyEvent e ) { }
}
编译是可以的,就是运行那个htm程序的时候只显示那段“java程序设计”,但要按方向键却移动不了,大侠帮我看看下,谢谢!!
按照一楼的建议加了repaint(),看结果还是移动不了,不知道哪里错了~~晕 展开
import java.awt.Graphics;
import java.applet.Applet;
import java.awt.event.*;
import java.awt.event.KeyEvent;
public class TestKey extends Applet implements KeyListener{
int x,y;
public void init() {
addKeyListener(this);
x=30;
y=30;
}
public void keyTyped(KeyEvent e) {
if(e.getKeyCode()==e.VK_UP)
y+=10;
else if(e.getKeyCode()==e.VK_DOWN)
y-=10;
else if(e.getKeyCode()==e.VK_LEFT)
x+=10;
else if
(e.getKeyCode()==e.VK_RIGHT)
x-=10;
}
public void paint(Graphics g){
g.drawString("java程序设计", x, y );
}
public void keyReleased( KeyEvent e ) { }
public void keyPressed( KeyEvent e ) { }
}
编译是可以的,就是运行那个htm程序的时候只显示那段“java程序设计”,但要按方向键却移动不了,大侠帮我看看下,谢谢!!
按照一楼的建议加了repaint(),看结果还是移动不了,不知道哪里错了~~晕 展开
3个回答
展开全部
import java.awt.Graphics;
import java.applet.Applet;
import java.awt.event.*;
import java.awt.event.KeyEvent;
public class TestKey extends Applet implements KeyListener {
int x=30, y=30;
public void init() {
addKeyListener(this);
}
public void keyTyped(KeyEvent e) {
}
public void paint(Graphics g) {
g.drawString("java程序设计", x, y);
}
public void keyReleased(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == e.VK_DOWN)
y += 10;
else if (e.getKeyCode() == e.VK_UP)
y -= 10;
else if (e.getKeyCode() == e.VK_RIGHT)
x += 10;
else if (e.getKeyCode() == e.VK_LEFT)
x -= 10;
repaint();
}
}
改成这样试试
import java.applet.Applet;
import java.awt.event.*;
import java.awt.event.KeyEvent;
public class TestKey extends Applet implements KeyListener {
int x=30, y=30;
public void init() {
addKeyListener(this);
}
public void keyTyped(KeyEvent e) {
}
public void paint(Graphics g) {
g.drawString("java程序设计", x, y);
}
public void keyReleased(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == e.VK_DOWN)
y += 10;
else if (e.getKeyCode() == e.VK_UP)
y -= 10;
else if (e.getKeyCode() == e.VK_RIGHT)
x += 10;
else if (e.getKeyCode() == e.VK_LEFT)
x -= 10;
repaint();
}
}
改成这样试试
展开全部
你木有repaint()啊
public void keyTyped(KeyEvent e) {
if(e.getKeyCode()==e.VK_UP) {
y+=10;
repaint();
}
else if(e.getKeyCode()==e.VK_DOWN) {
y-=10;
repaint();
}
else if(e.getKeyCode()==e.VK_LEFT) {
x+=10;
repaint();
}
else if
(e.getKeyCode()==e.VK_RIGHT) {
x-=10;
repaint();
}
}
public void keyTyped(KeyEvent e) {
if(e.getKeyCode()==e.VK_UP) {
y+=10;
repaint();
}
else if(e.getKeyCode()==e.VK_DOWN) {
y-=10;
repaint();
}
else if(e.getKeyCode()==e.VK_LEFT) {
x+=10;
repaint();
}
else if
(e.getKeyCode()==e.VK_RIGHT) {
x-=10;
repaint();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你这个按下之后没代码了,所以检测到了按键操作,图像也不能移动。
这样做:
检测到按键按下以后,再重新画图,根据新坐标。然后再repaint()
这样做:
检测到按键按下以后,再重新画图,根据新坐标。然后再repaint()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询