Java實(shí)現(xiàn)的動(dòng)態(tài)數(shù)字時(shí)鐘功能示例【顯示世界時(shí)間】
本文實(shí)例講述了Java實(shí)現(xiàn)的動(dòng)態(tài)數(shù)字時(shí)鐘功能。分享給大家供大家參考,具體如下:
構(gòu)建:
1. Clock繼承 JFrame 為運(yùn)行頁(yè)面
2. ClockText 測(cè)試類 創(chuàng)建 Clock 對(duì)象
運(yùn)行效果:
具體實(shí)現(xiàn):
一、Clock類
- 四個(gè)JPnal 三個(gè)放時(shí)間 最后一個(gè)放日期
- 放時(shí)間的三個(gè)JPnal 分別加入 地點(diǎn) 時(shí)間 按鈕
- 最后一個(gè)按鈕添加日期
具體實(shí)現(xiàn)如下:
public class Clock extends JFrame { private JPanel jPanelBeijing; private JPanel jPanelNewYork; private JPanel jPanelLondom; private JPanel jPanelDate; private boolean BeijingThreadFlag_IsStart = true; private boolean NewYorkThreadFlag_IsStart = true; private boolean LondonThreadFlag_IsStart = true; public Clock() { // TODO Auto-generated constructor stub jPanelBeijing = new JPanel(); jPanelNewYork = new JPanel(); jPanelLondom = new JPanel(); jPanelDate = new JPanel(); iniRelations(); iniLayout(); jFrameClick(); setVisible(true); setSize(480, 225); setLocationRelativeTo(null); } private void iniLayout() { jPanelBeijing.setLayout(new GridLayout(3, 1)); jPanelNewYork.setLayout(new GridLayout(3, 1)); jPanelLondom.setLayout(new GridLayout(3, 1)); } // 關(guān)系 private void iniRelations() { this.add(BorderLayout.WEST, jPanelBeijing); this.add(BorderLayout.CENTER, jPanelNewYork); this.add(BorderLayout.EAST, jPanelLondom); this.add(BorderLayout.SOUTH, jPanelDate); Font placeFont = new Font("楷體", Font.BOLD, 36); JLabel jLabelBeijing = new JLabel("北京時(shí)間"); jLabelBeijing.setFont(placeFont); jPanelBeijing.add(jLabelBeijing); setWestPanel(); JLabel jLabelNewYork = new JLabel("紐約時(shí)間"); jLabelNewYork.setFont(placeFont); jPanelNewYork.add(jLabelNewYork); setCenterPanel(); JLabel jLabelLondon = new JLabel("倫敦時(shí)間"); jLabelLondon.setFont(placeFont); jPanelLondom.add(jLabelLondon); setEastPanel(); setDatePanel(); } private void setWestPanel() { // add time for SouthPanel JLabel jLabelTime = new JLabel("加載中."); jLabelTime.setFont(new Font("宋體", Font.BOLD, 30)); Timer timeAction = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { long timemillis = System.currentTimeMillis(); // 轉(zhuǎn)換日期顯示格式 SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss "); jLabelTime.setText(time.format(new Date(timemillis))); } }); timeAction.start(); jPanelBeijing.add(jLabelTime); Button button = new Button("北京暫停"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (BeijingThreadFlag_IsStart) { timeAction.stop(); button.setLabel("北京繼續(xù)"); BeijingThreadFlag_IsStart = false; } else { timeAction.start(); button.setLabel("北京暫停"); BeijingThreadFlag_IsStart = true ; } } }); jPanelBeijing.add(button); } private void setCenterPanel() { // add time for SouthPanel JLabel jLabelTime = new JLabel("加載中."); jLabelTime.setFont(new Font("宋體", Font.BOLD, 30)); Timer timeAction = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { long timemillis = System.currentTimeMillis(); // 轉(zhuǎn)換日期顯示格式 SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss "); jLabelTime.setText(time.format(new Date(timemillis - 13 * 60 * 60 * 1000))); } }); timeAction.start(); jPanelNewYork.add(jLabelTime); Button button = new Button("紐約暫停"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (NewYorkThreadFlag_IsStart) { timeAction.stop(); button.setLabel("紐約繼續(xù)"); NewYorkThreadFlag_IsStart = false; } else { timeAction.start(); button.setLabel("紐約暫停"); NewYorkThreadFlag_IsStart = true ; } } }); jPanelNewYork.add(button); } private void setEastPanel() { // add time for SouthPanel // JLabel jLabelDate = new JLabel("Date"); JLabel jLabelTime = new JLabel("加載中."); jLabelTime.setFont(new Font("宋體", Font.BOLD, 30)); Timer timeAction = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { long timemillis = System.currentTimeMillis(); // SimpleDateFormat date = new SimpleDateFormat("yyyy 年 MM 月 dd // 日 "); // jLabelDate.setText(" 當(dāng)前日期: " + date.format(new // Date(timemillis))); SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss "); jLabelTime.setText(time.format(new Time(timemillis - 8 * 60 * 60 * 1000))); } }); timeAction.start(); jPanelLondom.add(jLabelTime); Button button = new Button("倫敦暫停"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (LondonThreadFlag_IsStart) { timeAction.stop(); button.setLabel("倫敦繼續(xù)"); LondonThreadFlag_IsStart = false; } else { timeAction.start(); button.setLabel("倫敦暫停"); LondonThreadFlag_IsStart = true ; } } }); jPanelLondom.add(button); // jPanelLondom.add(jLabelDate); } private void setDatePanel() { // add time for SouthPanel JLabel jLabelDate = new JLabel("加載中."); Timer timeAction = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { long timemillis = System.currentTimeMillis(); SimpleDateFormat date = new SimpleDateFormat("yyyy 年 MM 月 dd 日 "); jLabelDate.setText(" 當(dāng)前日期: " + date.format(new Date(timemillis))); } }); timeAction.start(); jPanelDate.add(jLabelDate); } private void jFrameClick(){ setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//設(shè)置不默認(rèn)關(guān)閉 addWindowListener(new WindowListener() { @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub int x = JOptionPane.showConfirmDialog(null, "確認(rèn)退出么?", "友情提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); if (x == 0) { System.exit(0); } } @Override public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method stub } }); } }
二、創(chuàng)建ClockText類用于測(cè)試
public class ClockText{ public static void main(String[] args) { new Clock(); } }
PS:這里再為大家推薦幾款時(shí)間及日期相關(guān)工具供大家參考使用:
在線世界各地時(shí)間查詢:
http://tools.jb51.net/zhuanhuanqi/worldtime
Unix時(shí)間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime
在線日期/天數(shù)計(jì)算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《java日期與時(shí)間操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- java實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘并設(shè)置鬧鐘功能
- Java實(shí)現(xiàn)的簡(jiǎn)單數(shù)字時(shí)鐘功能示例
- java多線程編程制作電子時(shí)鐘
- java實(shí)現(xiàn)的小時(shí)鐘示例分享
- Java編程小實(shí)例—數(shù)字時(shí)鐘的實(shí)現(xiàn)代碼示例
- java實(shí)現(xiàn)時(shí)鐘效果
- Java實(shí)現(xiàn)動(dòng)態(tài)模擬時(shí)鐘
- Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
- JavaFX實(shí)現(xiàn)簡(jiǎn)易時(shí)鐘效果(一)
- java實(shí)現(xiàn)時(shí)鐘表盤
相關(guān)文章
java本機(jī)內(nèi)存分配Native?memory?allocation?mmap失敗問(wèn)題解決
這篇文章主要介紹了java本機(jī)內(nèi)存分配Native?memory?allocation?mmap失敗問(wèn)題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11SpringMVC九大組件之HandlerMapping詳解
這篇文章主要介紹了SpringMVC九大組件之HandlerMapping詳解,HandlerMapping 叫做處理器映射器,它的作用就是根據(jù)當(dāng)前 request 找到對(duì)應(yīng)的 Handler 和 Interceptor,然后封裝成一個(gè) HandlerExecutionChain 對(duì)象返回,需要的朋友可以參考下2023-09-09MyBatis中一對(duì)多的xml配置方式(嵌套查詢/嵌套結(jié)果)
這篇文章主要介紹了MyBatis中一對(duì)多的xml配置方式(嵌套查詢/嵌套結(jié)果),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Java對(duì)數(shù)組實(shí)現(xiàn)選擇排序算法的實(shí)例詳解
這篇文章主要介紹了Java對(duì)數(shù)組實(shí)現(xiàn)選擇排序算法的實(shí)例,選擇排序的比較次數(shù)為 O(N^2)而交換數(shù)為O(N),需要的朋友可以參考下2016-04-04spring boot的健康檢查HealthIndicators實(shí)戰(zhàn)
這篇文章主要介紹了spring boot的健康檢查HealthIndicators實(shí)戰(zhàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10mybatis 查詢sql中in條件用法詳解(foreach)
這篇文章主要介紹了mybatis 查詢sql中in條件用法詳解(foreach),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02解決idea的debug模式突然變卡,項(xiàng)目啟動(dòng)變慢的狀況
這篇文章主要介紹了解決idea的debug模式突然變卡,項(xiàng)目啟動(dòng)變慢的狀況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02SpringMVC中的@ControllerAdvice使用場(chǎng)景詳解
這篇文章主要介紹了SpringMVC中的@ControllerAdvice使用場(chǎng)景詳解,在Spring?MVC進(jìn)行調(diào)用的過(guò)程中,會(huì)有很多的特殊的需求,比如全局異常,分頁(yè)信息和分頁(yè)搜索條件,請(qǐng)求時(shí)帶來(lái)返回時(shí)還得回顯頁(yè)面,需要的朋友可以參考下2024-01-01IntelliJ IDEA使用教程從入門到上癮(2019圖文版)
這篇文章主要介紹了IntelliJ IDEA使用教程從入門到上癮(2019圖文版),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12