返回列表 發帖
  1. package bbs.istak.org.tw;

  2. import java.awt.BorderLayout;
  3. import java.awt.EventQueue;
  4. import java.awt.Image;
  5. import java.awt.event.KeyAdapter;
  6. import java.awt.event.KeyEvent;

  7. import javax.swing.ImageIcon;
  8. import javax.swing.JFrame;
  9. import javax.swing.JPanel;
  10. import javax.swing.border.EmptyBorder;
  11. import javax.swing.JLabel;

  12. public class movecharetar extends JFrame {

  13.         private JPanel contentPane;
  14.     private static ImageIcon icon;
  15.     private int x =97,y=10;
  16.         /**
  17.          * Launch the application.
  18.          */
  19.         public static void main(String[] args) {
  20.                 EventQueue.invokeLater(new Runnable() {
  21.                         public void run() {
  22.                                 try {
  23.                                         movecharetar frame = new movecharetar();
  24.                                         frame.setVisible(true);
  25.                                 } catch (Exception e) {
  26.                                         e.printStackTrace();
  27.                                 }
  28.                         }
  29.                 });
  30.         }

  31.         /**
  32.          * Create the frame.
  33.          */
  34.         public movecharetar() {
  35.                 icon = new ImageIcon(udicon.class.getResource("Actions-go-down-icon.png"));
  36.                 Image img = icon.getImage().getScaledInstance(75,102,java.awt.Image.SCALE_FAST );
  37.                 ImageIcon newicon = new ImageIcon(img);
  38.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.                 setBounds(100, 100, 450, 300);
  40.                 contentPane = new JPanel();
  41.                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  42.                 setContentPane(contentPane);
  43.                 contentPane.setLayout(null);
  44.                
  45.                 JLabel label = new JLabel(newicon);
  46.                 label.setBounds(97, 10, 100, 100);
  47.                 contentPane.add(label);
  48.                 addKeyListener(new KeyAdapter() {
  49.                         @Override
  50.                         public void keyPressed(KeyEvent e) {
  51.                                 if(e.getKeyCode()==e.VK_LEFT){
  52.                                         x-=3;
  53.                                         label.setIcon(turndir("Actions-go-previous-icon.png"));
  54.                                 }
  55.                                 if(e.getKeyCode()==e.VK_UP){
  56.                                         y-=3;
  57.                                         label.setIcon(turndir("Actions-go-up-icon.png"));
  58.                                 }
  59.                                 if(e.getKeyCode()==e.VK_RIGHT){
  60.                                         x+=3;       
  61.                                         label.setIcon(turndir("Actions-go-next-icon.png"));
  62.                                 }
  63.                             if(e.getKeyCode()==e.VK_DOWN){
  64.                                     y+=3;
  65.                                     label.setIcon(turndir("Actions-go-down-icon.png"));
  66.                             }
  67.                             label.setLocation(x, y);
  68.                         }
  69.                         });
  70.                                        
  71.         }
  72.         public static ImageIcon turndir(String picname){
  73.                
  74.                 icon = new ImageIcon(udicon.class.getResource(picname));
  75.                 Image img = icon.getImage().getScaledInstance(100,100,java.awt.Image.SCALE_FAST );
  76.                 ImageIcon diricon = new ImageIcon(img);
  77.                
  78.                 return diricon;
  79.         }

  80. }
複製代碼

TOP

返回列表