Time Dynamic Display
1. method 1 use timertask
Java. util. timer and Java. util. timertask are used for dynamic updates. After all, each update can be regarded as a one-second timer.
The Code is as follows:
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 * 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_secondd = 1000; Public timeframe () { Timepanel = new jpanel (); Timelabel = new jlabel ("currenttime :"); Displayarea = new jlabel ();
Configtimearea ();
Timepanel. Add (timelabel ); Timepanel. Add (displayarea ); This. Add (timepanel ); This. setdefaclocloseoperation (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 ); } } |
Inherit timertask to create a custom task, get the current time, and update displayarea.
Create a timer instance and execute timertask once every 1 second. Because schedule may produce time errors, scheduleatfixedrate with higher precision can be called directly.
2. Method 2: Use the thread
This is relatively simple.
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 * Display time dynamically on the jswing-based interface. * @ Author Edison * */ Public class dtimeframe2 extends jframe implements runnable { Private jframe; Private jpanel timepanel; Private jlabel timelabel; Private jlabel displayarea; Private string default_time_format = "HH: mm: SS "; Private int one_secondd = 1000; Public dtimeframe2 () { Timepanel = new jpanel (); Timelabel = new jlabel ("currenttime :"); Displayarea = new jlabel ();
Timepanel. Add (timelabel ); Timepanel. Add (displayarea ); This. Add (timepanel ); This. setdefaclocloseoperation (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 (); } } |
Comparison:
I personally prefer method 1, because timer can be shared by multiple timertasks and a thread will increase the maintenance complexity of multithreading.
TIPS:
Jframe. setdefaclocloseoperation (); // Add a specific action to the close button
Jframe. setlocationrelativeto (null); // Let the frame appear in the middle of the screen rather than in the upper left corner.
You can change the preceding method to display the time of multiple countries. The Code is as follows:
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 = 4734786524987801209l; Private string time; Private jpanel timepanel; Private timezone; Private jcombobox zonebox; Private jlabel displayarea; Private int one_secondd = 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 actionreceivmed (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. setdefaclocloseoperation (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 (); } } |
TBD:
You must update displayarea in updatetimezone (timezone newzone. However, considering that timertask is executed for a short period of time, it takes only one second. It is basically the same as immediate update. If timertask is executed for a long time, you need to update displayarea immediately.
TIPS:
A. Pack () is used to automatically calculate the screen size;
B. timezone. getavailableids () is used to obtain all timezones.