回复 5# 的帖子
import java.awt.*;
import java.awt.event.*;
import *;
import java.util.*;
import java.awt.image.*;
import javax.swing.*;
public class MoveCar extends JFrame {
Toolkit tkit;
Image img;
int x,y;
public MoveCar(String title)
{
super(title);
tkit=getToolkit();
try{
Class c1=this.getClass();
URL url=c1.getResource("/images/Red_Car.gif");
img=tkit.getImage(url);
tkit.prepareImage(img, -1, -1, this);
}catch(Exception e){
System.out.println("URL错误");
}
this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent arg0) {
int imgX = (int) img.getLocation().getX();
int imgY = (int) img.getLocation().getY();
switch (arg0.getKeyCode()) {
case KeyEvent.VK_UP:
img.setLocation((int) (imgX),(int) (imgY-5));
break;
case KeyEvent.VK_DOWN:
img.setLocation((int) (imgX),(int) (imgY+5));
break;
case KeyEvent.VK_LEFT:
img.setLocation((int) (imgX-5),(int) (imgY));
break;
case KeyEvent.VK_RIGHT:
img.setLocation((int) (imgX+5),(int) (imgY));
break;
default:
break;
}
}
});
}
public static void main(String[] args)
{
MoveCar car=new MoveCar("移动图标");
car.setSize(200,200);
car.setVisible(true);
}
public void paint(Graphics g)
{
g.drawImage(img,50,80,this);
}
}
只能写成这样了,好多方法还不会使用,那些方法,使用也有错,高手帮忙改下~,谢谢