java實現時鐘方法匯總

import java.awt.Dimension;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
//第一種比較推薦:
public class TimeFrame extends JFrame
{/** Variables */private JPanel timePanel;private JLabel timeLabel;private JLabel displayArea;private String DEFAULT_TIME_FORMAT = "HH:mm:ss";private String time;private int ONE_SECOND = 1000;public TimeFrame(){timePanel = new JPanel();timeLabel = new JLabel("CurrentTime: ");displayArea = new JLabel();configTimeArea();timePanel.add(timeLabel);timePanel.add(displayArea);this.add(timePanel);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setSize(new Dimension(200,70));this.setLocationRelativeTo(null);}/*** This method creates a timer task to update the time per second*/private void configTimeArea() {Timer tmr = new Timer();tmr.scheduleAtFixedRate(new JLabelTimerTask(),new Date(), ONE_SECOND);}/*** Timer task to update the time display area**/protected class JLabelTimerTask extends TimerTask{SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT);@Overridepublic void run() {time = dateFormatter.format(Calendar.getInstance().getTime());displayArea.setText(time);}}public static void main(String arg[]){TimeFrame timeFrame=new TimeFrame();timeFrame.setVisible(true); } 
}

  

import java.awt.Dimension;
import java.text.SimpleDateFormat;
import java.util.Calendar;import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
//第二種 
public class DTimeFrame2 extends JFrame implements Runnable{private JFrame frame;private JPanel timePanel;private JLabel timeLabel;private JLabel displayArea;private String DEFAULT_TIME_FORMAT = "HH:mm:ss";private int ONE_SECOND = 1000;public DTimeFrame2(){timePanel = new JPanel();timeLabel = new JLabel("CurrentTime: ");displayArea = new JLabel();timePanel.add(timeLabel);timePanel.add(displayArea);this.add(timePanel);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setSize(new Dimension(200,70));this.setLocationRelativeTo(null);}public void run(){while(true){SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT);displayArea.setText(dateFormatter.format(Calendar.getInstance().getTime()));try{Thread.sleep(ONE_SECOND); }catch(Exception e){displayArea.setText("Error!!!");}} } public static void main(String arg[]){DTimeFrame2 df2=new DTimeFrame2();df2.setVisible(true);Thread thread1=new Thread(df2);thread1.start(); } }

  

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
//第三種:多國時鐘實現
public class WorldTimeFrame extends JFrame
{/*** */private static final long serialVersionUID = 4782486524987801209L;private String time;private JPanel timePanel;private TimeZone timeZone;//時區private JComboBox zoneBox;private JLabel displayArea;private int ONE_SECOND = 1000;private String DEFAULT_FORMAT = "EEE d MMM, HH:mm:ss";public WorldTimeFrame(){zoneBox = new JComboBox();timePanel = new JPanel();displayArea = new JLabel();timeZone = TimeZone.getDefault();zoneBox.setModel(new DefaultComboBoxModel(TimeZone.getAvailableIDs()));zoneBox.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent e) {updateTimeZone(TimeZone.getTimeZone((String) zoneBox.getSelectedItem()));}});configTimeArea();timePanel.add(displayArea);this.setLayout(new BorderLayout());this.add(zoneBox, BorderLayout.NORTH);this.add(timePanel, BorderLayout.CENTER);this.setLocationRelativeTo(null);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);pack();}/*** This method creates a timer task to update the time per second*/private void configTimeArea() {Timer tmr = new Timer();tmr.scheduleAtFixedRate(new JLabelTimerTask(),new Date(), ONE_SECOND);}/*** Timer task to update the time display area**/public class JLabelTimerTask extends TimerTask{SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_FORMAT, Locale.ENGLISH);@Overridepublic void run() {dateFormatter.setTimeZone(timeZone);time = dateFormatter.format(Calendar.getInstance().getTime());displayArea.setText(time);}}/*** Update the timeZone* @param newZone*/public void updateTimeZone(TimeZone newZone){this.timeZone = newZone;}public static void main(String arg[]){new WorldTimeFrame();} 
}

  

轉載于:https://www.cnblogs.com/ipetergo/p/6862148.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392788.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392788.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392788.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

js中注冊標識符流程

注冊分為三個階段:分別是注冊階段,函數處理階段,變量處理階段;這三個階段有先后順序的。(注:這三個階段的名字沒有權威性,是作者為了方便記憶自己起的名字) 注冊階段的特征 1.此時不…

jsp論壇網站模版_網站關鍵詞優化怎么做

說到網站關鍵詞優化,大多企業都很陌生,建站公司說的關鍵詞優化頭頭是道。跟聽天書似的,51商務網小編為大家總結的網站優化方法希望可以幫到大家,首先要說的是做網站優化第一點就是要有耐心,如果很長時間沒有收錄的話&a…

feature功能_利用feature-u V1釋放基于功能的JS開發的強大功能

feature功能This article is an introduction to a new JS library called feature-u, that facilitates feature-based development in your React project.本文是對新的JS庫(稱為feature-u )的介紹,該庫促進了React項目中基于功能的開發 。 Note: On 8/14/2018 f…

虛擬實驗工場大學計算機實驗報告答案,虛擬實驗實驗報告 - 實驗報告 - 書業網.doc...

虛擬實驗實驗報告 - 實驗報告 - 書業網虛擬實驗實驗報告 - 實驗報告 - 書業網篇一:虛擬實驗報告第一章 文獻綜述1.1 丙酮酸脫氫酶概述丙酮酸脫氫酶復合體(Pyruvate Dehydrogenase Complex)催化丙酮酸不可逆的氧化脫羧轉化成乙酰輔酶A。該復合體是糖酵解的關鍵限速酶…

【筆記】一些linux實用函數技巧【原創】

函數返回的是函數的地址 kallsyms_lookup_name() 本文轉自張昺華-sky博客園博客,原文鏈接:http://www.cnblogs.com/sky-heaven/p/5191491.html,如需轉載請自行聯系原作者

leetcode 845. 數組中的最長山脈

我們把數組 A 中符合下列屬性的任意連續子數組 B 稱為 “山脈”&#xff1a; B.length > 3 存在 0 < i < B.length - 1 使得 B[0] < B[1] < … B[i-1] < B[i] > B[i1] > … > B[B.length - 1] &#xff08;注意&#xff1a;B 可以是 A 的任意子數組…

【Lintcode】018.Subsets II

題目&#xff1a; Given a list of numbers that may has duplicate numbers, return all possible subsets Notice Each element in a subset must be in non-descending order.The ordering between two subsets is free.The solution set must not contain duplicate subset…

多線程1

1-1 進程 程序是靜止的&#xff0c;運行中的程序就是進程。進程的三個特征&#xff1a; 動態性 &#xff1a; 進程是運行中的程序&#xff0c;要動態的占用內存&#xff0c;CPU和網絡等資源。獨立性 &#xff1a; 進程與進程之間是相關獨立的&#xff0c;彼此有自己的獨立內存區…

go 列出已經安裝的包_Go 安裝教程

一、在 Windows 上安裝 Go 環境首先在 Go 官網 下載 Windows 系統下的一鍵安裝包。然后雙擊打開該文件&#xff0c;一直點 Next 就行。注意這里默認是安裝到 C 盤&#xff0c;建議不要修改&#xff0c;因為環境變量會自動設置&#xff0c;如果安裝到其他盤&#xff0c;那么可能…

【轉】spin_lock、spin_lock_irq、spin_lock_irqsave區別

為什么80%的碼農都做不了架構師&#xff1f;>>> 轉自&#xff1a;http://blog.csdn.net/luckywang1103/article/details/42083613 void spin_lock(spinlock_t *lock);void spin_lock_irq(spinlock_t *lock);void spin_lock_irqsave(spinlock_t *lock, unsigned lon…

七年級計算機上教學計劃,初一教學計劃模板錦集5篇

初一教學計劃模板錦集5篇時光在流逝&#xff0c;從不停歇&#xff0c;我們又將迎來新的教學工作&#xff0c;我們要好好計劃今后的教育教學方法。那么一份同事都拍手稱贊的教學計劃是什么樣的呢&#xff1f;以下是小編為大家整理的初一教學計劃5篇&#xff0c;僅供參考&#xf…

程序員實際情況_程序員實際上是做什么的?

程序員實際情況What do programmers actually do? What can they be working on?程序員實際上是做什么的&#xff1f; 他們可以做什么&#xff1f; In this video from an Airbnb software engineer, you will learn about what programmers do on a day-to-day basis. She …

leetcode 1365. 有多少小于當前數字的數字(排序)

給你一個數組 nums&#xff0c;對于其中每個元素 nums[i]&#xff0c;請你統計數組中比它小的所有數字的數目。 換而言之&#xff0c;對于每個 nums[i] 你必須計算出有效的 j 的數量&#xff0c;其中 j 滿足 j ! i 且 nums[j] < nums[i] 。 以數組形式返回答案。 示例 1&…

spring整合springmvc案例

面試遇到過上機操作&#xff0c;不知道小伙伴們遇到過沒。 案例。 1、新建web項目&#xff0c;找到相關的jar包。 轉載于:https://www.cnblogs.com/sjzxs/p/11158116.html

我的世界服務器玩家在線時間,將公布上線時間?我的世界中國版網易520前瞻

【17173專稿&#xff0c;轉載請注明出處】《我的世界》中國版最近一段時間動作不斷。網易CEO丁磊在財報電話會議上公布了《我的世界》手游版會在7月份推出&#xff0c;結合《我的世界》中國版的公告提及&#xff1a;”《我的世界》中國版即將在暑期上線“。如此看來手游版和PC版…

ftpwebrequest 無法加載或初始化請求的服務提供程序_jvm之類加載機制

什么是類加載每個編寫的".java"拓展名類文件都存儲著需要執行的程序邏輯&#xff0c;這些".java"文件經過Java編譯器編譯成拓展名為".class"的文件&#xff0c;".class"文件中保存著Java代碼經轉換后的虛擬機指令&#xff0c;當需要使…

【284天】我愛刷題系列(43)

叨叨兩句 身體是靈魂的載體&#xff0c;靈魂是身體的指引&#xff0c;用心維護、馴化你的身體&#xff0c;構建通道&#xff0c;指引它將力量與情緒宣泄在你想做出成績的領域&#xff0c;神奇的事情就會發生&#xff0c;哈哈。牛客網——java專項練習023 1 SuppressWarnings(“…

基于python滲透測試_Python中基于屬性的測試簡介

基于python滲透測試by Shashi Kumar Raja由Shashi Kumar Raja Python中基于屬性的測試簡介 (Intro to property-based testing in Python) In this article we will learn a unique and effective approach to testing called property-based testing. We will use Python , p…

leetcode144. 二叉樹的前序遍歷(迭代)

給定一個二叉樹&#xff0c;返回它的 前序 遍歷。示例:輸入: [1,null,2,3] 1\2/3 輸出: [1,2,3]代碼 /*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val…

矩陣的理解經典博客

矩陣理解一&#xff1a;https://blog.csdn.net/myan/article/details/647511 矩陣理解二&#xff1a;https://blog.csdn.net/myan/article/details/649018 矩陣理解三&#xff1a;https://blog.csdn.net/myan/article/details/1865397 關鍵結論&#xff1a; 1. 首先有空間&…