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

  2. import java.awt.BorderLayout;
  3. import java.awt.EventQueue;
  4. import java.awt.Image;

  5. import javax.swing.ImageIcon;
  6. import javax.swing.JFrame;
  7. import javax.swing.JPanel;
  8. import javax.swing.border.EmptyBorder;
  9. import javax.swing.JLabel;
  10. import java.awt.event.KeyAdapter;
  11. import java.awt.event.KeyEvent;

  12. public class Main extends JFrame {

  13.         private JPanel contentPane;
  14.     private int x=100, y=100;
  15.     private ImageIcon icon_1 = new ImageIcon(Main.class.getResource("crab.png"));
  16.     private ImageIcon icon_2 = new ImageIcon(Main.class.getResource("crab2.png"));
  17.     private ImageIcon icon_3 = new ImageIcon(Main.class.getResource("crab3.png"));
  18.     private ImageIcon icon_4 = new ImageIcon(Main.class.getResource("crab4.png"));

  19.         public static void main(String[] args) {
  20.                 EventQueue.invokeLater(new Runnable() {
  21.                         public void run() {
  22.                                 try {
  23.                                         Main frame = new Main();
  24.                                         ImageIcon icon = new ImageIcon(Main.class.getResource("icon.png"));
  25.                                         Image img = icon.getImage().getScaledInstance(45, 45, java.awt.Image.SCALE_FAST);
  26.                                         frame.setIconImage(img);
  27.                                         frame.setVisible(true);
  28.                                 } catch (Exception e) {
  29.                                         e.printStackTrace();
  30.                                 }
  31.                         }
  32.                 });
  33.         }


  34.         public Main() {
  35.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  36.                 setBounds(100, 100, 483, 359);
  37.                 contentPane = new JPanel();       
  38.                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  39.                 setContentPane(contentPane);
  40.                 contentPane.setLayout(null);
  41.                
  42.                 ImageIcon icon = new ImageIcon(Main.class.getResource("crab2.png"));
  43.                 Image img = icon.getImage().getScaledInstance(150, 150, java.awt.Image.SCALE_FAST);
  44.                 ImageIcon temp = new ImageIcon(img);
  45.                
  46.                 JLabel lblNewLabel = new JLabel(temp);           
  47.                 lblNewLabel.setBounds(94, 86, 169, 142);      
  48.                 contentPane.add(lblNewLabel);
  49.                 addKeyListener(new KeyAdapter() {
  50.                         public void keyPressed(KeyEvent e) {                       
  51.                                 if(e.getKeyCode() == KeyEvent.VK_UP){       
  52.                                  y-=5;
  53.                                  lblNewLabel.setIcon(icon_1);
  54.                                 }
  55.                             if(e.getKeyCode() == KeyEvent.VK_DOWN){
  56.                                  y+=5;
  57.                                  lblNewLabel.setIcon(icon_3);
  58.                             }
  59.                         if(e.getKeyCode() == KeyEvent.VK_LEFT){
  60.                                  x-=5;
  61.                                  lblNewLabel.setIcon(icon_4);
  62.                         }
  63.                                 
  64.                                 if(e.getKeyCode() == KeyEvent.VK_RIGHT){
  65.                                         x+=5;       
  66.                                         lblNewLabel.setIcon(icon_2);
  67.                                 }      
  68.                                 lblNewLabel.setLocation(x, y);
  69.                         }
  70.                 });               
  71.         }
  72. }
複製代碼

TOP

返回列表