用多线程编程, 实现时间显示的实例
TimeThread1.java
package GUI.Thread;
import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeThread1 extends Thread {
public JPanel jp;
public JFrame jf;
public JLabel jLabel_Time;
public TimeThread1() {
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);
}
@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()));
}
}
}
TimeThread1Test.java
package GUI.Thread;
public class TimeThread1Test {
public static void main(String[] args) {
Thread th = new TimeThread1();
th.start();
}
}
用继承Thread类的方式,实现线程。