本文所述實例可以實現Java在介面上動態的顯示時間。具體實作方法總結如下:
1.方法一用TimerTask:
利用java.util.Timer和java.util.TimerTask來做動態更新,畢竟每次更新可以看作是計時1秒發生一次。
程式碼如下:
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.JFrameimport java.util.TimerTask;import javax.swing.JFrameimportswing.JFrameimport; 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 times privel times; 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("CurrentPanel = new JPanel(); timeLabel = new JLabel("CurrentTime: "); configTimeArea(); timePanel.add(timeLabel); timePanel.add(displayArea); this.add(timePanel); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setSize(new Dimension(200,70)); this.setLocationRelative /n); ** * 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); } / * */ protected class JLabelTimerTask extends TimerTask{ SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT); @Override public void run() { time = dateFormatter.format(Calendar.getInstance().getTime()); displayArea.set time); String arg[]) { TimeFrame timeFrame=new TimeFrame(); timeFrame.setVisible(true); } }
繼承TimerTask來建立一個自訂的task,取得目前時間,更新displayArea.
然後建立一個timer的實例,每1秒執行一次timertask。由於用schedule可能會有時間誤差產生,所以直接呼叫精度更高的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 * 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 ONE_SECOND = 1000; public DTimeFrame2() { timePanel = new JPanel(); timeLabel = new JLabel("CurrentTime: "); displayArea = new JLabel(); timePanel.add(timeLabel); timePanel.this(timeArea); add(timePanel); this.setDefaultCloseOperation(EXIT_ON_CLOSE); 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共用,而產生一個線程,會增加多線程的維護複雜度。
注意如下程式碼:
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;importat;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;/** * Alock simple worldJLabel;import javax.swing.JPanel;/** * Alock simple worldJLabel;import javax.swing.JPanel;/** * Alock simple worldJLabel;import javax.swing.JPanel;/** * Alock simple worldJLabel;import javax.swing.JPanel;/** * Alock simple worldJLabel; @author Edison * */public class WorldTimeFrame extends JFrame{ /** * */ private static final long serialVersionUID = 4782486524987801209L; private String time; private JPanel timePanel; private TimeZone timeZone; 1000; private String DEFAULT_FORMAT = "EEE d MMM, HH:mm:ss"; public WorldTimeFrame() { zoneBox = new JComboBox(); timePanel = new JPanel(); displayArea = new JLabel(); ); 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); LOS.setDefaultCloseOperation(thisIT_ON_CLOSy); true); pack(); } /** * This method creates a timer task to update the time per second */ private void configTimeArea() { Timer tmr = new Timer(); .scheduleAtFixedRate(new JLabelTimerTask(),new Datetmr(), ONE_SECOND); 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 =mFormar); displayArea.setText(time); } } /** * Update the timeZone * @param newZone */ public void updateTimeZone(TimeZone newZone) { this.timeZone = newZone; } public static void main(String arg[ (); } }
本來需要在updateTimeZone(TimeZone newZone)中,更新displayArea的。但考慮到TimerTask執行的時間太短,才1秒鐘,以肉眼觀察,基本上是和立刻更新沒區別。如果TimerTask執行時間長的話,這裡就要立刻重新用心的時間更新一下displayArea。
補充:
①. pack() 用來自動計算螢幕大小;
②. TimeZone.getAvailableIDs() 用來取得所有的TimeZone。