當前位置:才華齋>計算機>java語言>

Java中如何實現顯示動態的時間

java語言 閱讀(3.99K)

本文所述例項可以實現Java在介面上動態的顯示時間。具體實現方法彙總如下:

Java中如何實現顯示動態的時間

1.方法一 用TimerTask:

利用r和rTask來做動態更新,畢竟每次更新可以看作是計時1秒發生一次。

程式碼如下:

import nsion;import leDateFormat;import ndar;import ;import r;import rTask;import me;import el;import el;/** * 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(); (timeLabel); (displayArea); (timePanel); efaultCloseOperation(EXIT_ON_CLOSE); ize(new Dimension(200,70)); ocationRelativeTo(null); } /** * This method creates a timer task to the time per second */ private void configTimeArea() { Timer tmr = new Timer(); duleAtFixedRate(new JLabelTimerTask(),new Date(), ONE_SECOND); } /** * Timer task to the time display area * */ protected class JLabelTimerTask extends TimerTask{ SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT); @Override public void run() { time = at(nstance()ime()); ext(time); } } public static void main(String arg[]) { TimeFrame timeFrame=new TimeFrame(); isible(true); } }

繼承TimerTask來建立一個自定義的task,獲取當前時間,更新displayArea.

然後建立一個timer的例項,每1秒執行一次timertask。由於用schedule可能會有時間誤差產生,所以直接呼叫精度更高的scheduleAtFixedRate的。

  2. 方法二:利用執行緒:

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

import nsion;import leDateFormat;import ndar;import me;import el;import el;/** * 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(); (timeLabel); (displayArea); (timePanel); efaultCloseOperation(EXIT_ON_CLOSE); ize(new Dimension(200,70)); ocationRelativeTo(null); } public void run() { while(true) { SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT); ext(at( nstance()ime())); try { p(ONE_SECOND); } catch(Exception e) { ext("Error!!!"); } } } public static void main(String arg[]) { DTimeFrame2 df2=new DTimeFrame2(); isible(true); Thread thread1=new Thread(df2); t(); } }

比較:

個人傾向於方法一,因為Timer是可以被多個TimerTask共用,而產生一個執行緒,會增加多執行緒的維護複雜度。

注意如下程式碼:

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

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

import erLayout;import onEvent;import onListener;import leDateFormat;import ndar;import ;import le;import Zone;import r;import rTask;import ultComboBoxModel;import boBox;import me;import el;import el;/** * 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 = efault(); odel(new DefaultComboBoxModel(vailableIDs())); ctionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { updateTimeZone(imeZone((String) electedItem())); } }); configTimeArea(); (displayArea); ayout(new BorderLayout()); (zoneBox, H); (timePanel, ER); ocationRelativeTo(null); efaultCloseOperation(EXIT_ON_CLOSE); isible(true); pack(); } /** * This method creates a timer task to the time per second */ private void configTimeArea() { Timer tmr = new Timer(); duleAtFixedRate(new JLabelTimerTask(),new Date(), ONE_SECOND); } /** * Timer task to the time display area * */ public class JLabelTimerTask extends TimerTask{ SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_FORMAT, ISH); @Override public void run() { imeZone(timeZone); time = at(nstance()ime()); ext(time); } } /** * Update the timeZone * @param newZone */ public void updateTimeZone(TimeZone newZone) { Zone = newZone; } public static void main(String arg[]) { new WorldTimeFrame(); } }

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

補充:

①. pack() 用來自動計算螢幕大小;

②. vailableIDs() 用來獲取所有的TimeZone。