Java Implementation Clock Method summary

Source: Internet
Author: User

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;//First Comparison recommendation: public class Timeframe extends jframe{*/* Variables * private JPanel timepanel; private J Label 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 = NE W Timer (); Tmr.scheduleatfixedrate (New Jlabeltimertask (), NewDate (), One_second); }/** * Timer task to update the time display area * */protected class Jlabeltimertask extends timertask{simpledatefor Mat 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); } }

Import Java.awt.dimension;import java.text.simpledateformat;import java.util.calendar;import javax.swing.JFrame; Import Javax.swing.jlabel;import javax.swing.jpanel;//Second 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;// Third: Multi-country clock implementation public class Worldtimeframe extends jframe{/** * */private static final long Serialversionuid = 4782486524987   801209L; Private String time; Private JPanel Timepanel; Private TimeZone timezone;//time zone 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 = time   Zone.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 = NE W 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 ();}}

  

Java Implementation Clock Method summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.