返回列表 發帖
  1. package asdf;
  2. import java.awt.EventQueue;
  3. import javax.swing.JFrame;
  4. import javax.swing.JPanel;
  5. import java.awt.BorderLayout;
  6. import java.awt.FlowLayout;
  7. import java.awt.CardLayout;
  8. import java.awt.Window;
  9. import net.miginfocom.swing.MigLayout;
  10. import javax.swing.ImageIcon;
  11. import javax.swing.JLabel;
  12. import javax.swing.GroupLayout;
  13. import javax.swing.GroupLayout.Alignment;
  14. import javax.swing.border.EmptyBorder;
  15. public class Main {

  16.         private JPanel contentPane;
  17.         protected Object frame;
  18.         /**
  19.          * Launch the application.
  20.          */
  21.         public static void main(String[] args) {
  22.                 EventQueue.invokeLater(new Runnable() {
  23.                         public void run() {
  24.                                 try {
  25.                                         Main window = new Main();
  26.                                         ((Window) window.frame).setVisible(true);
  27.                                 } catch (Exception e) {
  28.                                         e.printStackTrace();
  29.                                 }
  30.                         }
  31.                 });
  32.         }

  33.         /**
  34.          * Create the application.
  35.          */
  36.         public Main() {
  37.                   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  38.           setBounds(100, 100, 450, 300);
  39.           contentPane = new JPanel();
  40.           contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  41.           setContentPane(contentPane);
  42.           contentPane.setLayout(null);
  43.          
  44.           JLabel label = new JLabel("");
  45.           label.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\r.gif"));
  46.           label.setBounds(22, 40, 75, 64);
  47.           contentPane.add(label);
  48.           MyThread1 t = new MyThread1(label);
  49.           t.start();
  50.          
  51.           JLabel label_1 = new JLabel("");
  52.           label_1.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\r.gif"));
  53.           label_1.setBounds(10, 132, 87, 64);
  54.           contentPane.add(label_1);
  55.           MyThread2 t2 = new MyThread2(label_1);
  56.           t2.start();
  57.         }
  58. }
複製代碼
  1. package asdf;
  2. import javax.swing.JLabel;
  3. public class MyThread1 extends Thread{
  4.         JLabel label = null;
  5.         public MyThread1(JLabel label) {
  6.                 this.label = label;
  7.         }
  8.         public void run()
  9.         {
  10.                 int x = label.getLocation().x;
  11.                 int sleep=0;
  12.                 while(true)
  13.                 {
  14.                         x+=20;
  15.                         try {
  16.                                 Thread.sleep(75);
  17.                         } catch (InterruptedException e) {
  18.                                 // TODO 自動產生的 catch 區塊
  19.                                 e.printStackTrace();
  20.                         }
  21.                         label.setLocation(x, 40);
  22.                         sleep=(int)(Math.random()*3000);
  23.                         try {
  24.                                 Thread.sleep(sleep);
  25.                         } catch (InterruptedException e) {
  26.                                 // TODO 自動產生的 catch 區塊
  27.                                 e.printStackTrace();
  28.                         }
  29.                 }
  30.         }
  31. }
複製代碼
  1. package asdf;
  2. import javax.swing.JLabel;
  3. public class MyThread2 extends Thread{
  4.         JLabel label_1 = null;
  5.         public MyThread2(JLabel label_1) {
  6.                 this.label_1 = label_1;
  7.         }
  8.         public void run()
  9.         {
  10.                 int x = label_1.getLocation().x;
  11.                 while(true)
  12.                 {
  13.                         x+=2;
  14.                         try {
  15.                                 Thread.sleep(100);
  16.                         } catch (InterruptedException e) {
  17.                                 // TODO 自動產生的 catch 區塊
  18.                                 e.printStackTrace();
  19.                         }
  20.                         label_1.setLocation(x, 132);
  21.                 }
  22.         }
  23. }
複製代碼
我是眾神之王XXX  I love you
0000000000

TOP

返回列表