美文网首页
Java时钟桌面程序

Java时钟桌面程序

作者: 悬雨 | 来源:发表于2018-06-28 22:02 被阅读18次
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();
    }
}

相关文章

网友评论

      本文标题:Java时钟桌面程序

      本文链接:https://www.haomeiwen.com/subject/rfonyftx.html