返回列表 發帖
  1. 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 javax.swing.JLabel;
  8. import javax.swing.ImageIcon;


  9. public class Rabbit_and_Turtle_Run extends JFrame {

  10.         private JPanel contentPane;

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

  26.         /**
  27.          * Create the frame.
  28.          */
  29.         public Rabbit_and_Turtle_Run() {
  30.                 setTitle("\u9F9C\u5154\u8CFD\u8DD1");
  31.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32.                 setBounds(100, 100, 898, 269);
  33.                 contentPane = new JPanel();
  34.                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  35.                 setContentPane(contentPane);
  36.                 contentPane.setLayout(null);
  37.                
  38.                 JLabel lblNewLabel = new JLabel("");
  39.                 lblNewLabel.setIcon(new ImageIcon(Rabbit_and_Turtle_Run.class.getResource("/bbs/istak/org/tw/r.gif")));
  40.                 lblNewLabel.setBounds(22, 40, 75, 64);
  41.                 contentPane.add(lblNewLabel);
  42.                 rabbitTH t = new rabbitTH(lblNewLabel);
  43.         t.start();
  44.         
  45.                
  46.                 JLabel lblNewLabel_1 = new JLabel("New label");
  47.                 lblNewLabel_1.setIcon(new ImageIcon(Rabbit_and_Turtle_Run.class.getResource("/bbs/istak/org/tw/t.gif")));
  48.                 lblNewLabel_1.setBounds(22, 132, 75, 64);
  49.                 contentPane.add(lblNewLabel_1);
  50.                 turtleTH t2 = new turtleTH(lblNewLabel_1);
  51.                
  52.                 JLabel lblNewLabel_2 = new JLabel("New label");
  53.                 lblNewLabel_2.setIcon(new ImageIcon(Rabbit_and_Turtle_Run.class.getResource("/bbs/istak/org/tw/endline.jpg")));
  54.                 lblNewLabel_2.setBounds(738, 85, 92, 158);
  55.                 contentPane.add(lblNewLabel_2);
  56.                
  57.                 JLabel label = new JLabel("New label");
  58.                 label.setIcon(new ImageIcon(Rabbit_and_Turtle_Run.class.getResource("/bbs/istak/org/tw/endline.jpg")));
  59.                 label.setBounds(738, 0, 92, 142);
  60.                 contentPane.add(label);
  61.                
  62.                 JLabel label_1 = new JLabel("New label");
  63.                 label_1.setIcon(new ImageIcon(Rabbit_and_Turtle_Run.class.getResource("/bbs/istak/org/tw/endline.jpg")));
  64.                 label_1.setBounds(738, 10, 92, 208);
  65.                 contentPane.add(label_1);
  66.                 t2.start();
  67.                
  68.         }
  69. }

  70. class rabbitTH extends Thread{
  71.     JLabel lblNewLabel = null;
  72.     public rabbitTH(JLabel lblNewLabel) {
  73.             this.lblNewLabel = lblNewLabel;
  74.     }

  75.     public void run()
  76. {
  77.             int x = lblNewLabel.getLocation().x;
  78.             int sleep=0;
  79.             while(true)
  80.             {
  81.                     x+=20;
  82.                     try {
  83.                             Thread.sleep(100);
  84.                     } catch (InterruptedException e) {
  85.                             // TODO 自動產生的 catch 區塊
  86.                             e.printStackTrace();
  87.                     }
  88.                     lblNewLabel.setLocation(x, 40);
  89.                     sleep=(int)(Math.random()*3000);
  90.                     try {
  91.                             Thread.sleep(sleep);
  92.                     } catch (InterruptedException e) {
  93.                             // TODO 自動產生的 catch 區塊
  94.                             e.printStackTrace();
  95.                     }
  96.             }
  97. }
  98. }

  99. class turtleTH extends Thread{
  100.     JLabel lblNewLabel_1 = null;
  101.     public turtleTH(JLabel lblNewLabel_1) {
  102.             this.lblNewLabel_1 = lblNewLabel_1;
  103.     }

  104.     public void run()
  105. {
  106.             int x = lblNewLabel_1.getLocation().x;
  107.             while(true)
  108.             {
  109.                     x+=2;
  110.                     try {
  111.                             Thread.sleep(100);
  112.                     } catch (InterruptedException e) {
  113.                             // TODO 自動產生的 catch 區塊
  114.                             e.printStackTrace();
  115.                     }
  116.                     lblNewLabel_1.setLocation(x, 132);
  117.             }
  118. }
  119. }
複製代碼

TOP

返回列表