import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.*;
public class Clock extends JFrame implements Runnable {
public static JLabel jLable;
public static Clock clock;
public void run() {
for (; ; ) {
Date date = new Date();
SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
jLable.setText(time.format(date));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
public static void main(String[] agrs) {
clock = new Clock();
clock.setLayout(new BorderLayout());
jLable = new JLabel();
jLable.setFont(new Font("WenQuanYi Micro Hei Mono", 0, 25));
jLable.setSize(300,100);
clock.add(jLable,BorderLayout.CENTER);
jLable.setHorizontalAlignment(SwingConstants.CENTER);
clock.setLocationRelativeTo(null);//窗体居中显示
clock.setSize(jLable.getWidth(),jLable.getHeight());
clock.setResizable(false);
clock.setDefaultCloseOperation(EXIT_ON_CLOSE);
clock.setAlwaysOnTop(true);
clock.setVisible(true);
Thread t = new Thread(clock);
t.start();
}
}
网友评论