import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MovePic extends KeyAdapter{
JFrame jf=new JFrame("键盘移到图片");
ImageIcon icon=new ImageIcon
("pc1.png");
JLabel jlb=new JLabel(icon);
int x=50,y=50;
public MovePic(){
jf.setLayout(null);
jlb.setBounds(x,y,150,150);
jf.add(jlb);
jf.setSize(400, 400);
jf.setVisible(true);
jf.addKeyListener(this);
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_DOWN){
if(y<250){
y=y+10;
}
}
if(e.getKeyCode()==KeyEvent.VK_UP){
if(y<250){
y=y-10;
}
}
if(e.getKeyCode()==KeyEvent.VK_LEFT){
if(x<250){
x=x-10;
}
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
if(x<250){
x=x+10;
}
}
jlb.setBounds(x,y, 150, 150);
}
public static void main(String[] args) {
// TODO Auto-generated method
MovePic mp=new MovePic();
}
}