1 package xiancheng_3v;
 2 
 3 import java.awt.Container;
 4 
 5 import javax.swing.*;
 6 public class Swing_and_Thread extends JFrame implements Runnable{
 7     JLabel jl = new JLabel();//声明JLabel对象
 8     Container container = getContentPane();//声明容器
 9     public Swing_and_Thread(){
10         jl.setText("大家好!我是练习时长两年半的个人练习生cxk,喜欢唱,跳,Rap,java");
11         container.add(jl);//将标签添加到容器中
12         setBounds(500,500,550,500);//绝对定位窗体大小与位置
13         setVisible(true);//使窗体可视
14         setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
15     }
16     public void run() {//重写run()方法
17         for (int i = 0; i <= 1001; i++) {//设置循环条件
18             jl.setBounds(i,10,1000,50);//将标签中的横坐标用变量表示
19             try {
20                 Thread.sleep(100);//使线程休眠1000毫秒
21             } catch (Exception e) {
22                 e.printStackTrace();
23             }
24             if (i == 200) {//当图标到达标签的最右边,使其回到标签的最左边
25                 i = 10;
26             }
27         }
28     }
29     public static void main(String[] args) {
30         Swing_and_Thread sw = new Swing_and_Thread();//实例化一个对象
31         Thread t1 = new Thread(sw);//把目标对象做为参数创建线程对象
32         t1.start();//启动线程
33     }
34 
35 }

 

posted on 2019-06-13 10:02  Anonym_白熊  阅读(143)  评论(0编辑  收藏  举报