Java動態(tài)顯示當前日期和時間
更新時間:2019年12月20日 16:21:01 作者:Bit0_1
這篇文章主要為大家詳細介紹了Java動態(tài)顯示當前日期和時間,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
Java 動態(tài)顯示當前系統(tǒng)的日期、時間;如圖所示:

package com.xin.test;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.JFrame;
public class NowTime extends JFrame {
private static final long serialVersionUID = 4306803332677233920L;
// 添加 顯示時間的JLabel
public NowTime() {
JLabel time = new JLabel();
time.setForeground(Color.BLUE);
time.setBounds(30, 0, 900, 130);
time.setFont(new Font("微軟雅黑", Font.BOLD, 80));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setTitle("now time");
this.setBounds(500, 200, 930, 200);
this.setVisible(true);
this.add(time);
this.setTimer(time);
}
// 設置Timer 1000ms實現(xiàn)一次動作 實際是一個線程
private void setTimer(JLabel time) {
final JLabel varTime = time;
Timer timeAction = new Timer(100, new ActionListener() {
public void actionPerformed(ActionEvent e) {
long timemillis = System.currentTimeMillis();
// 轉換日期顯示格式
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
varTime.setText(df.format(new Date(timemillis)));
}
});
timeAction.start();
}
// 運行方法
public static void main(String[] args) {
new NowTime();
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringBoot集成Redis—使用RedisRepositories詳解
這篇文章主要介紹了SpringBoot集成Redis—使用RedisRepositories詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
springboot項目整合mybatis并配置mybatis中間件的實現(xiàn)
這篇文章主要介紹了springboot項目整合mybatis并配置mybatis中間件的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04
Java中ArrayList和LinkedList之間的區(qū)別_動力節(jié)點Java學院整理
這篇文章主要為大家詳細介紹了Java中ArrayList和LinkedList之間的區(qū)別,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
SpringBoot之通過BeanPostProcessor動態(tài)注入ID生成器案例詳解
這篇文章主要介紹了SpringBoot之通過BeanPostProcessor動態(tài)注入ID生成器案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-09-09
使用mybatis-plus的insert方法遇到的問題及解決方法(添加時id值不存在異常)
這篇文章主要介紹了使用mybatis-plus的insert方法遇到的問題及解決方法(添加時id值不存在異常),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
Springboot+Mybatis中typeAliasesPackage正則掃描實現(xiàn)方式
這篇文章主要介紹了Springboot+Mybatis中typeAliasesPackage正則掃描實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07

