返回列表 發帖
本帖最後由 黃茂勛 於 2018-5-26 11:21 編輯
  1. [code]package bbs.istak.org.tw;

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

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

  13. public class Main extends JFrame {

  14.         private JPanel contentPane;
  15.         private JLabel lblNewLabel;

  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.                                         Main frame = new Main();
  24.                                         frame.setVisible(true);
  25.                                 } catch (Exception e) {
  26.                                         e.printStackTrace();
  27.                                 }
  28.                         }
  29.                 });
  30.         }

  31.         /**
  32.          * Create the frame.
  33.          */
  34.         public Main() {
  35.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  36.                 setBounds(100, 100, 495, 398);
  37.                 contentPane = new JPanel();
  38.                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  39.                 setContentPane(contentPane);
  40.                 contentPane.setLayout(null);
  41.                
  42.                 lblNewLabel = new JLabel("");
  43.                 lblNewLabel.setBounds(150, 75, 50, 50);
  44.                 lblNewLabel.setIcon(new ImageIcon(Main.class.getResource("/bbs/istak/org/tw/box.png")));
  45.                 contentPane.add(lblNewLabel);
  46.                 addKeyListener(new KeyAdapter() {
  47.                         @Override
  48.                         public void keyReleased(KeyEvent arg0) {
  49.                                 int x=lblNewLabel.getLocation().x;
  50.                                 int y=lblNewLabel.getLocation().y;
  51.                                 RunPanel rp = new RunPanel(x,y,arg0.getKeyCode(),lblNewLabel);
  52.                                 rp.start();                               
  53.                         }
  54.                 });
  55.                
  56.         }
  57. }
複製代碼
  1. package bbs.istak.org.tw;

  2. import javax.swing.JLabel;
  3. import javax.swing.JPanel;

  4. public class RunPanel extends Thread{

  5.         int x=0,y=0,keycode=0;
  6.         JLabel lblNewLabel = null;
  7.         RunPanel(int x, int y, int keycode, JLabel lblNewLabel)
  8.         {
  9.                 this.x = x;
  10.                 this.y = y;
  11.                 this.keycode = keycode;
  12.             this.lblNewLabel = lblNewLabel;
  13.         }
  14.         public void run()
  15.         {
  16.                 switch(this.keycode)
  17.                 {
  18.                    case 38:
  19.                            y-=5;
  20.                            break;
  21.                    case 40:
  22.                            y+=5;
  23.                            break;
  24.                    case 37:
  25.                            x-=5;
  26.                            break;
  27.                    case 39:
  28.                            x+=5;
  29.                            break;
  30.                default:
  31.                        break;
  32.                 }
  33.                 lblNewLabel.setLocation(this.x, this.y);
  34.         }
  35. }
複製代碼

TOP

返回列表