返回列表 發帖
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. public class Ch93 implements KeyListener{

  4.         private JFrame f;
  5.         private JLabel lb;
  6.         private ImageIcon icon1,icon2;
  7.         private int x=200,y=200;
  8.         Ch93()
  9.         {
  10.                 f=new JFrame("\ucfcf\uff9a");
  11.                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  12.                 f.setVisible(true);
  13.                 f.setLayout(null);
  14.                 f.setResizable(false);
  15.                 f.setBounds(100,100,500,500);
  16.                 f.addKeyListener(this);
  17.                 icon1=new ImageIcon(Ch93.class.getResource("pic/icon.png"));
  18.                 icon2=new ImageIcon(Ch93.class.getResource("pic/baby.png"));
  19.                 f.setIconImage(icon1.getImage());
  20.                 lb=new JLabel(icon2);
  21.                 lb.setBounds(x,y,128,128);
  22.                 f.add(lb);
  23.         }
  24.         public static void main(String[] args)
  25.         {
  26.                 new Ch93();
  27.         }
  28.         public void keyPressed(KeyEvent e)
  29.         {
  30.                 if(e.getKeyCode()==KeyEvent.VK_UP)
  31.                         y-=5;
  32.                 else if(e.getKeyCode()==KeyEvent.VK_DOWN)
  33.                         y+=5;
  34.                 else if(e.getKeyCode()==KeyEvent.VK_LEFT)
  35.                         x-=5;
  36.                 else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
  37.                         x+=5;
  38.                 lb.setLocation(x,y);
  39.         }
  40.         public void keyReleased(KeyEvent e)        {}
  41.         public void keyTyped(KeyEvent e){}
  42. }
複製代碼

TOP

返回列表