50506759
App Name: Java timer
Knowledge used: Java GUI programming
Development environment: win8+eclipse+jdk1.8
Function Description: Chronograph function, accurate to 1 milliseconds, can be paused.
:
Source:
Importjavax.swing.*; Importjava.awt.HeadlessException; Importjava.awt.BorderLayout; Importjava.awt.FlowLayout; ImportJava.awt.Font; ImportJava.awt.event.ActionListener; Importjava.awt.event.ActionEvent; /*** Timer*/ Public classTimerextendsJFrame {/** * */ Private Static Final LongSerialversionuid = 1L; Private Static FinalString initial_label_text = "00:00:00 000"; //Count Thread PrivateCountingthread thread =NewCountingthread (); //Log program start time Private LongProgramstart =System.currenttimemillis (); //The program was suspended at first. Private LongPausestart =Programstart; //Total time The program was paused Private LongPausecount = 0; PrivateJLabel label =NewJLabel (Initial_label_text); PrivateJButton Startpausebutton =NewJButton ("Start"); PrivateJButton Resetbutton =NewJButton ("Qing 0"); PrivateActionListener Startpausebuttonlistener =NewActionListener () { Public voidactionperformed (ActionEvent e) {if(thread.stopped) {Pausecount+ = (System.currenttimemillis ()-Pausestart); thread.stopped=false; Startpausebutton.settext (Pause); } Else{Pausestart=System.currenttimemillis (); thread.stopped=true; Startpausebutton.settext (Continue to); } } }; PrivateActionListener Resetbuttonlistener =NewActionListener () { Public voidactionperformed (ActionEvent e) {Pausestart=Programstart; Pausecount= 0; thread.stopped=true; Label.settext (Initial_label_text); Startpausebutton.settext (Start); } }; PublicTimer (String title)throwsheadlessexception {Super(title); Setdefaultcloseoperation (Exit_on_close); SetLocation (300, 300); Setresizable (false); Setupborder (); Setuplabel (); Setupbuttonspanel (); Startpausebutton.addactionlistener (Startpausebuttonlistener); Resetbutton.addactionlistener (Resetbuttonlistener); Thread.Start (); //The Count thread has been running } //add a border to the form panel Private voidSetupborder () {JPanel ContentPane=NewJPanel (NewBorderLayout ()); Contentpane.setborder (Borderfactory.createemptyborder (5, 5, 5, 5)); This. Setcontentpane (ContentPane); } //Configure button Private voidSetupbuttonspanel () {JPanel panel=NewJPanel (NewFlowLayout ()); Panel.add (Startpausebutton); Panel.add (Resetbutton); Add (Panel, Borderlayout.south); } //Configure Labels Private voidSetuplabel () {label.sethorizontalalignment (swingconstants.center); Label.setfont (NewFont (Label.getfont (). GetName (), Label.getfont (). GetStyle (), 40)); This. Add (label, Borderlayout.center); } //Program Entry Public Static voidMain (string[] args) {Try{Uimanager.setlookandfeel (Uimanager.getsystemlookandfeelclassname ()); } Catch(Exception e) {e.printstacktrace (); } Timer Frame=NewTimer ("Timer"); Frame.pack (); Frame.setvisible (true); } Private classCountingthreadextendsThread { Public Booleanstopped =true; PrivateCountingthread () {Setdaemon (true); } @Override Public voidrun () { while(true) { if(!stopped) { Longelapsed = System.currenttimemillis ()-Programstart-Pausecount; Label.settext (Format (elapsed)); } Try{sleep (1);//1 ms Update display}Catch(interruptedexception e) {e.printstacktrace (); System.exit (1); } } } //format the number of milliseconds PrivateString Format (Longelapsed) { inthour, minute, second, Milli; Milli= (int) (Elapsed% 1000); Elapsed= elapsed/1000; Second= (int) (Elapsed% 60); Elapsed= ELAPSED/60; Minute= (int) (Elapsed% 60); Elapsed= ELAPSED/60; Hour= (int) (Elapsed% 60); returnString.Format ("%02d:%02d:%02d%03d", hour, minute, second, Milli); } } }
Java Java Timer (stopwatch)