最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實現(xiàn)時間動態(tài)顯示方法匯總

 更新時間:2014年08月12日 10:07:41   投稿:shichen2014  
這篇文章主要介紹了Java實現(xiàn)時間動態(tài)顯示方法匯總,很實用的功能,需要的朋友可以參考下

本文所述實例可以實現(xiàn)Java在界面上動態(tài)的顯示時間。具體實現(xiàn)方法匯總如下:

1.方法一 用TimerTask:

利用java.util.Timer和java.util.TimerTask來做動態(tài)更新,畢竟每次更新可以看作是計時1秒發(fā)生一次。
代碼如下:

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;
/**
 * This class is a simple JFrame implementation to explain how to
 * display time dynamically on the JSwing-based interface.
 * @author Edison
 *
 */
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);
 @Override
 public void run() {
  time = dateFormatter.format(Calendar.getInstance().getTime());
  displayArea.setText(time);
 }
 }
 
 public static void main(String arg[])
 {
 TimeFrame timeFrame=new TimeFrame();
 timeFrame.setVisible(true); 
 } 
}
 


繼承TimerTask來創(chuàng)建一個自定義的task,獲取當前時間,更新displayArea.
然后創(chuàng)建一個timer的實例,每1秒執(zhí)行一次timertask。由于用schedule可能會有時間誤差產(chǎn)生,所以直接調用精度更高的scheduleAtFixedRate的。
 
2. 方法二:利用線程:

這個就比較簡單了。具體代碼如下:

import java.awt.Dimension;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
 * This class is a simple JFrame implementation to explain how to
 * display time dynamically on the JSwing-based interface.
 * @author Edison
 *
 */
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(); 
 } 
}

比較:

個人傾向于方法一,因為Timer是可以被多個TimerTask共用,而產(chǎn)生一個線程,會增加多線程的維護復雜度。

注意如下代碼:

jFrame.setDefaultCloseOperation(); // 給關閉按鈕增加特定行為
jFrame.setLocationRelativeTo(null); // 讓Frame一出來就在屏幕中間,而不是左上方。

將上面方法一稍微一修改,就可以顯示多國時間。代碼如下:

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;
/**
 * A simple world clock
 * @author Edison
 *
 */
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(){
  @Override
  public 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);
 @Override
 public 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();
 } 
}

本來需要在updateTimeZone(TimeZone newZone)中,更新displayArea的。但是考慮到TimerTask執(zhí)行的時間太短,才1秒鐘,以肉眼觀察,基本上是和立刻更新沒區(qū)別。如果TimerTask執(zhí)行時間長的話,這里就要立刻重新用心的時間更新一下displayArea。

補充:

①. pack() 用來自動計算屏幕大??;
②. TimeZone.getAvailableIDs() 用來獲取所有的TimeZone。

相關文章

  • springboot接口服務,防刷、防止請求攻擊,AOP實現(xiàn)方式

    springboot接口服務,防刷、防止請求攻擊,AOP實現(xiàn)方式

    本文介紹了如何使用AOP防止Spring?Boot接口服務被網(wǎng)絡攻擊,通過在pom.xml中加入AOP依賴,創(chuàng)建自定義注解類和AOP切面,以及在業(yè)務類中使用這些注解,可以有效地對接口進行保護,測試表明,這種方法有效地防止了網(wǎng)絡攻擊
    2024-11-11
  • SpringMVC上傳圖片代碼實例

    SpringMVC上傳圖片代碼實例

    這篇文章主要介紹了SpringMVC上傳圖片代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-08-08
  • java使用jacob.jar將word轉pdf

    java使用jacob.jar將word轉pdf

    這篇文章主要為大家詳細介紹了java利用jacob.jar將word轉pdf,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • SpringCloud OpenFeign基本介紹與實現(xiàn)示例

    SpringCloud OpenFeign基本介紹與實現(xiàn)示例

    OpenFeign源于Netflix的Feign,是http通信的客戶端。屏蔽了網(wǎng)絡通信的細節(jié),直接面向接口的方式開發(fā),讓開發(fā)者感知不到網(wǎng)絡通信細節(jié)。所有遠程調用,都像調用本地方法一樣完成
    2023-02-02
  • Java?CompletableFuture實現(xiàn)多線程異步編排

    Java?CompletableFuture實現(xiàn)多線程異步編排

    這篇文章主要為大家介紹了Java?CompletableFuture實現(xiàn)多線程異步編排,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • Mybatis-plus插入數(shù)據(jù)遇到主鍵沒有默認值的情況

    Mybatis-plus插入數(shù)據(jù)遇到主鍵沒有默認值的情況

    這篇文章主要介紹了Mybatis-plus插入數(shù)據(jù)遇到主鍵沒有默認值的情況,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • SpringBoot集成ShedLock實現(xiàn)分布式定時任務

    SpringBoot集成ShedLock實現(xiàn)分布式定時任務

    ShedLock 是一個 Java 庫,通常用于分布式系統(tǒng)中,確保定時任務(Scheduled Tasks)在集群環(huán)境下只被某一個實例執(zhí)行一次,它通過在共享資源中添加鎖的方式,本文給大家介紹了SpringBoot集成ShedLock實現(xiàn)分布式定時任務,需要的朋友可以參考下
    2024-11-11
  • Spring注解 TX聲明式事務實現(xiàn)過程解析

    Spring注解 TX聲明式事務實現(xiàn)過程解析

    這篇文章主要介紹了Spring注解 - TX 聲明式事務實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • Java ServletContext對象用法解析

    Java ServletContext對象用法解析

    這篇文章主要介紹了Java ServletContext對象用法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • 一天時間用Java寫了個飛機大戰(zhàn)游戲,朋友直呼高手

    一天時間用Java寫了個飛機大戰(zhàn)游戲,朋友直呼高手

    前兩天我發(fā)現(xiàn)論壇有兩篇飛機大戰(zhàn)的文章異?;鸨?但都是python寫的,竟然不是我大Java,說實話作為老java選手,我心里是有那么一些失落的,今天特地整理了這篇文章,需要的朋友可以參考下
    2021-05-05

最新評論

平江县| 莲花县| 巴塘县| 九龙城区| 麻江县| 普宁市| 秦安县| 京山县| 璧山县| 全椒县| 伊金霍洛旗| 双辽市| 呈贡县| 任丘市| 吴旗县| 金秀| 金华市| 通城县| 陇川县| 马公市| 宜宾市| 弥勒县| 三原县| 新邵县| 定日县| 望城县| 余姚市| 山西省| 双城市| 南溪县| 衢州市| 威信县| 康乐县| 长治市| 阜阳市| 社旗县| 漾濞| 张掖市| 遂平县| 肇州县| 股票|