返回列表 發帖
  1. import javax.swing.JLabel;


  2. public class MyThread1 extends Thread{
  3.         JLabel lblNewLabel = null;
  4.         public MyThread1(JLabel lblNewLabel) {
  5.                 this.lblNewLabel = lblNewLabel;
  6.         }

  7.         public void run()
  8.     {
  9.                 int x = lblNewLabel.getLocation().x;
  10.                 int sleep=0;
  11.                 while(true)
  12.                 {
  13.                         x+=20;
  14.                         try {
  15.                                 Thread.sleep(100);
  16.                         } catch (InterruptedException e) {
  17.                                 // TODO 自動產生的 catch 區塊
  18.                                 e.printStackTrace();
  19.                         }
  20.                         lblNewLabel.setLocation(x, 40);
  21.                         sleep=(int)(Math.random()*3000);
  22.                         try {
  23.                                 Thread.sleep(sleep);
  24.                         } catch (InterruptedException e) {
  25.                                 // TODO 自動產生的 catch 區塊
  26.                                 e.printStackTrace();
  27.                         }
  28.                 }
  29.     }
  30. }
複製代碼
  1. import javax.swing.JLabel;


  2. public class MyThread2 extends Thread{
  3.         JLabel lblNewLabel_1 = null;
  4.         public MyThread2(JLabel lblNewLabel_1) {
  5.                 this.lblNewLabel_1 = lblNewLabel_1;
  6.         }

  7.         public void run()
  8.     {
  9.                 int x = lblNewLabel_1.getLocation().x;
  10.                 while(true)
  11.                 {
  12.                         x+=2;
  13.                         try {
  14.                                 Thread.sleep(100);
  15.                         } catch (InterruptedException e) {
  16.                                 // TODO 自動產生的 catch 區塊
  17.                                 e.printStackTrace();
  18.                         }
  19.                         lblNewLabel_1.setLocation(x, 132);
  20.                 }
  21.     }
  22. }
複製代碼
  1. import java.awt.BorderLayout;
  2. import java.awt.EventQueue;

  3. import javax.swing.JFrame;
  4. import javax.swing.JPanel;
  5. import javax.swing.border.EmptyBorder;
  6. import javax.swing.JLabel;
  7. import javax.swing.ImageIcon;


  8. public class main extends JFrame {

  9.         private JPanel contentPane;

  10.         /**
  11.          * Launch the application.
  12.          */
  13.         public static void main(String[] args) {
  14.                 EventQueue.invokeLater(new Runnable() {
  15.                         public void run() {
  16.                                 try {
  17.                                         main frame = new main();
  18.                                         frame.setVisible(true);
  19.                                 } catch (Exception e) {
  20.                                         e.printStackTrace();
  21.                                 }
  22.                         }
  23.                 });
  24.         }

  25.         /**
  26.          * Create the frame.
  27.          */
  28.         public main() {
  29.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30.                 setBounds(100, 100, 450, 300);
  31.                 contentPane = new JPanel();
  32.                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  33.                 setContentPane(contentPane);
  34.                 contentPane.setLayout(null);
  35.                
  36.                 JLabel lblNewLabel = new JLabel("");
  37.                 lblNewLabel.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\r.gif"));
  38.                 lblNewLabel.setBounds(22, 40, 75, 64);
  39.                 contentPane.add(lblNewLabel);
  40.                 MyThread1 t = new MyThread1(lblNewLabel);
  41.         t.start();
  42.         
  43.                
  44.                 JLabel lblNewLabel_1 = new JLabel("New label");
  45.                 lblNewLabel_1.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\t.gif"));
  46.                 lblNewLabel_1.setBounds(10, 132, 87, 64);
  47.                 contentPane.add(lblNewLabel_1);
  48.                 MyThread2 t2 = new MyThread2(lblNewLabel_1);
  49.                 t2.start();
  50.                
  51.         }
  52. }
複製代碼

TOP

返回列表