- package bbs.istak.org.tw;
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JLabel;
- import javax.swing.ImageIcon;
- public class Rabbit_and_Turtle_Run extends JFrame {
- private JPanel contentPane;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Rabbit_and_Turtle_Run frame = new Rabbit_and_Turtle_Run();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public Rabbit_and_Turtle_Run() {
- setTitle("\u9F9C\u5154\u8CFD\u8DD1");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 898, 269);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
-
- JLabel lblNewLabel = new JLabel("");
- lblNewLabel.setIcon(new ImageIcon(Rabbit_and_Turtle_Run.class.getResource("/bbs/istak/org/tw/r.gif")));
- lblNewLabel.setBounds(22, 40, 75, 64);
- contentPane.add(lblNewLabel);
- rabbitTH t = new rabbitTH(lblNewLabel);
- t.start();
-
-
- JLabel lblNewLabel_1 = new JLabel("New label");
- lblNewLabel_1.setIcon(new ImageIcon(Rabbit_and_Turtle_Run.class.getResource("/bbs/istak/org/tw/t.gif")));
- lblNewLabel_1.setBounds(22, 132, 75, 64);
- contentPane.add(lblNewLabel_1);
- turtleTH t2 = new turtleTH(lblNewLabel_1);
-
- JLabel lblNewLabel_2 = new JLabel("New label");
- lblNewLabel_2.setIcon(new ImageIcon(Rabbit_and_Turtle_Run.class.getResource("/bbs/istak/org/tw/endline.jpg")));
- lblNewLabel_2.setBounds(738, 85, 92, 158);
- contentPane.add(lblNewLabel_2);
-
- JLabel label = new JLabel("New label");
- label.setIcon(new ImageIcon(Rabbit_and_Turtle_Run.class.getResource("/bbs/istak/org/tw/endline.jpg")));
- label.setBounds(738, 0, 92, 142);
- contentPane.add(label);
-
- JLabel label_1 = new JLabel("New label");
- label_1.setIcon(new ImageIcon(Rabbit_and_Turtle_Run.class.getResource("/bbs/istak/org/tw/endline.jpg")));
- label_1.setBounds(738, 10, 92, 208);
- contentPane.add(label_1);
- t2.start();
-
- }
- }
- class rabbitTH extends Thread{
- JLabel lblNewLabel = null;
- public rabbitTH(JLabel lblNewLabel) {
- this.lblNewLabel = lblNewLabel;
- }
- public void run()
- {
- int x = lblNewLabel.getLocation().x;
- int sleep=0;
- while(true)
- {
- x+=20;
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- // TODO 自動產生的 catch 區塊
- e.printStackTrace();
- }
- lblNewLabel.setLocation(x, 40);
- sleep=(int)(Math.random()*3000);
- try {
- Thread.sleep(sleep);
- } catch (InterruptedException e) {
- // TODO 自動產生的 catch 區塊
- e.printStackTrace();
- }
- }
- }
- }
- class turtleTH extends Thread{
- JLabel lblNewLabel_1 = null;
- public turtleTH(JLabel lblNewLabel_1) {
- this.lblNewLabel_1 = lblNewLabel_1;
- }
- public void run()
- {
- int x = lblNewLabel_1.getLocation().x;
- while(true)
- {
- x+=2;
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- // TODO 自動產生的 catch 區塊
- e.printStackTrace();
- }
- lblNewLabel_1.setLocation(x, 132);
- }
- }
- }
複製代碼 |