Java学习之路-Thread-简易时钟显示-implements

用多线程编程, 实现时间显示的实例
TimeThread.java

package GUI.Thread;

import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeThread implements Runnable{

    public JPanel jp;
    public JFrame jf;
    public JLabel jLabel_Time;

    public TimeThread(){
        jf = new JFrame("动态时间");
        jf.setSize(250, 100);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.setLocationRelativeTo(null);
        jf.setResizable(false);

        jp = new JPanel(new FlowLayout());
        jLabel_Time = new JLabel("null");
        jLabel_Time.setFont(new Font(null, Font.PLAIN, 26));

        jp.add(jLabel_Time);
        jf.setContentPane(jp);
        jf.setVisible(true);

        Thread th = new Thread(this);
        th.start();
    }

    @Override
    public void run() {

        while (true){
            Date nowDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SSS");
            jLabel_Time.setText(sdf.format(nowDate.getTime()));
        }

    }

    public static void main (String[] args){
        new TimeThread();
    }
}
用实现Runnable接口的方式,实现线程。
简简单单, 无脑的线程操作实例...
点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注