Java stopwatch, can be timed to seconds!

Source: Internet
Author: User
Tags gettext

Java stopwatch, the realization of the second time and in the lap of timing!

Words do not say much, on the source!

Package Timerdemo;import Java.awt.borderlayout;public Class Timercountdemo extends JPanel implements Runnable, Actionlistener{private ScrollPane scrollpane;private TextArea resulttimetext;private JLabel currenttimelabel;private Calendar currentcalendar;private Date currentdate;private JButton startjbutton;private JButton quanjbutton;private JButton secondbutton;private JButton stopjbutton;private JButton showtimejbutton;private Thread t;private static int I=1                          ; Lap number private static int hour,min,sec,msec,totaltime;private static Boolean flag=true;public Timercountdemo () {//TODO Auto-generated Constructor stubstartjbutton=new JButton ("start"); Showtimejbutton=new JButton ("00-00-00"); Showtimejbutton.setbackground (systemcolor.activecaption); Showtimejbutton.setfont (New Font ("叶根友 pen running script Upgrade",        Font.plain) quanjbutton=new JButton ("lap Time"); quanjbutton.setenabled (false);       Set to Falsesecondbutton=new JButton at initialization ("in seconds"), secondbutton.setenabled (false); Set to Falsestop when initializedJbutton=new JButton ("Stop"); Stopjbutton.setbackground (color.red); Set the background color to red scrollpane=new ScrollPane (); resulttimetext=new TextArea (20,20); resulttimetext.seteditable (false); Scrollpane.setenabled (True); Currentcalendar=calendar.getinstance (); Scrollpane.setbackground (Color.WHITE); Currenttimelabel=new JLabel (); currentdate=new Date (); Currenttimelabel.settext ("Current Time:"); This.setlayout (new BorderLayout ()); JPanel pnorth=new JPanel (New GridLayout (2,1));p North.add (Currenttimelabel);p north.add (Showtimejbutton); This.add (             Pnorth,borderlayout.north);                   Add current time Labelthis.add (Scrollpane,borderlayout.center); Add JPanel panel=new JPanel (New GridLayout (2,2));p Anel.add (Quanjbutton);p anel.add (Secondbutton);p Anel.add ( Startjbutton);p Anel.add (Stopjbutton); This.add (Panel,borderlayout.south); t=new Thread (this); T.start (); Startjbutton.addactionlistener (This), Quanjbutton.addactionlistener (This), Secondbutton.addactionlistener (this); Stopjbutton.addactionlistener (this);} @Overridepublic void RUn () {//TODO auto-generated method Stubtry {while (true) {int mhour=new date (). getHours (); int mmin=new date (). getminutes ( ); int msec=new Date (). getseconds (); int mtotaltime=mhour*3600+mmin*60+msec;int diffhour= (mtotaltime-totaltime)/3600 ; int diffmin= (mtotaltime-totaltime)%3600/60;int diffsec= (mtotaltime-totaltime)%3600%60; Thread.Sleep, Currenttimelabel.settext ("Current Time:" +mhour+ ":" +mmin+ ":" +msec "), if (!flag) {Showtimejbutton.settext (" "+diffhour+"-"+diffmin+"-"+diffsec";} if (!flag)}//while} catch (Exception e) {//Todo:handle exceptione.printstacktrace ();}} @SuppressWarnings ("deprecation") @Overridepublic void actionperformed (ActionEvent e) {//TODO auto-generated method    Stubif (E.getsource () ==startjbutton) {hour= (new Date ()). GetHours (); min= (new Date ()). getminutes ();    sec= (New Date ()). Getseconds ();    Totaltime=hour*3600+min*60+sec;    Startjbutton.setenabled (FALSE);    Quanjbutton.setenabled (TRUE);    Secondbutton.setenabled (TRUE); Flag=false;} if (!flag) {int xhour=new Date (). GetHours ();          Get the moment hour int xmin=new Date (). getminutes ();          Get the minute int xsec=new Date (). getseconds ();    Get the moment second int xtotaltime=xhour*3600+xmin*60+xsec; Get total time now int bhour= (xtotaltime-totaltime)/3600;int bmin= (xtotaltime-totaltime)%3600/60;int bsec= ( Xtotaltime-totaltime)%3600%60;//quanjbuttonif (E.getsource () ==quanjbutton) {resulttimetext.append ("+ (i++) +" Circle: " +bhour+ ":" +bmin+ ":" +bsec+ "\ n"); Scrollpane.add (Resulttimetext); secondbutton.setenabled (false);} Quanjbutton//secondbuttonif (E.getsource () ==secondbutton) {Resulttimetext.append (bhour+ ":" +bMin+ ":" +bSec+ "\ n") ; Scrollpane.add (Resulttimetext); quanjbutton.setenabled (false);} Secondbutton//stopjbuttonif (E.getsource () ==stopjbutton) {if (stopjbutton.gettext () = = "Stop") {//system.out.println ("Test Stop"); Hour=0;sec=0;min=0;stopjbutton.settext ("clear"); quanjbutton.setenabled (false); Secondbutton.setenabled ( false); flag=true;} Stopjbutton.gettext () = = "Stop" Else if (stopjbutton.gettext () = = "Clear") {I=1;stopjbutton.settext ("Stop"); Resulttimetext.settext (""); STARtjbutton.setenabled (True); Showtimejbutton.settext ("00-00-00");} Stopjbutton.settext ("clear");} stopjbutton}//if (!flag)//flag=false;//}//if (!flag) public static void Main (String args[]) {JFrame jf=new JFrame (" Demo "); Try{uimanager.setlookandfeel (" Com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel ");} catch (Exception e) {e.printstacktrace ();} Timercountdemo xcountdemo=new Timercountdemo (); Jf.setbounds (200,200,250,300); Jf.getcontentpane (). Add (XCountDemo) ; Jf.setdefaultcloseoperation (Jframe.exit_on_close); jf.setvisible (True);}}

Initializing the interface


Begin


Lap timing


Lap timing


Timed in seconds


Source Address

Java stopwatch, can be timed to seconds!

Related Article

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.