Java完成時光靜態顯示辦法匯總。本站提示廣大學習愛好者:(Java完成時光靜態顯示辦法匯總)文章只能為提供參考,不一定能成為您想要的結果。以下是Java完成時光靜態顯示辦法匯總正文
本文所述實例可以完成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.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來創立一個自界說的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 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共用,而發生一個線程,會增長多線程的保護龐雜度。
留意以下代碼:
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履行的時光太短,才1秒鐘,以肉眼不雅察,根本上是和連忙更新沒差別。假如TimerTask履行時光長的話,這裡就要連忙從新居心的時光更新一下displayArea。
彌補:
①. pack() 用來主動盤算屏幕年夜小;
②. TimeZone.getAvailableIDs() 用來獲得一切的TimeZone。