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接口的方式,实现线程。
简简单单, 无脑的线程操作实例...
按讚

發佈留言

電子郵件地址不會被公開。必填項已用 * 標註