51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

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

赞(0)
未经允许不得转载:工具盒子 » Java学习之路-Thread-简易时钟显示-implements