[Java] 超级简单的java时钟,仅30多行代码

每隔一秒钟就会自动更新时间,这就形成了一个简单的时钟。

 

 

 代码如下:

import java.awt.*;
import java.util.*;
import javax.swing.*;
public class HyClock extends JFrame implements Runnable {
    public static JLabel tm;
    public void run() {
        while (true) {
            Calendar c = Calendar.getInstance();
            String nowtime = c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND);
            tm.setText(nowtime);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
    }
 
    public static void main(String[] args) {
        HyClock  hytime = new HyClock();
        hytime.setLayout(new FlowLayout());
        tm = new JLabel();
        tm.setFont(new Font("宋体", 1, 40));
        tm.setForeground(Color.RED);
        hytime.add(tm);
        hytime.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        hytime.setSize(300, 100);
        hytime.setVisible(true);
        hytime.setLocation(400, 400);
        Thread t = new Thread(hytime);
        t.start();
    }
}
  • 5
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值