Java Multithreaded Production timer

Source: Internet
Author: User

Basic ideas:

Creates a thread in the class for the button listener to invoke.

Interface design:

Code:

1  Packagethread;2 3 Importjava.awt.*;4 Importjava.awt.event.*;5 6 Importjavax.swing.*;7 8  Public classThreadTestextendsJFrameImplementsrunnable{9JFrame frm=NewJFrame ("Watch");TenJPanel bottom=NewJPanel (); OneJPanel mid=NewJPanel (); AJLabel h=NewJLabel ("0"); -JLabel biao=NewJLabel (":"); -JLabel s=NewJLabel ("00"); theJButton hajime=NewJButton ("Start timing"); -JButton pause=NewJButton ("Pause"); -JButton tozero=NewJButton ("Qing 0"); -Thread t=NewThread ( This);  +     Private intI=0,j=0; -      Public voidrun () { +         Try A         { at              while(true){ -Thread.Sleep (100); -i++; -             if(i>=1&&i<10) -             { -S.settext ("0" +i); in             } -             Else if(i>=10&&i<60) toS.settext ("" +i); +             if(i==60) -             { theI=0; *J + +; $S.settext ("00");Panax NotoginsengH.settext ("" +j); -             } the             } +}Catch(Exception e) { ASystem.out.println (""); the         } +     } -      Publicthreadtest () $     { $Frm.setlayout (NewBorderLayout ()); -Frm.setbounds (200, 200, 300, 150); -Frm.setvisible (true); theFrm.add ("South", bottom); -Frm.add ("Center", mid);Wuyi          the Mid.add (h); - Mid.add (Biao); Wu Mid.add (s); -H.setfont (NewFont ("Consulas", font.plain,36)); AboutBiao.setfont (NewFont ("Consulas", font.plain,36)); $S.setfont (NewFont ("Consulas", font.plain,36)); - Bottom.add (Hajime); - bottom.add (pause); - Bottom.add (Tozero); A          +          theHajime.addactionlistener (NewActionListener () { -              Public voidactionperformed (ActionEvent e) { $                 if(T.isalive ()) the T.resume (); the                 ElseT.start (); the             }}); thePause.addactionlistener (NewActionListener () { -              Public voidactionperformed (ActionEvent e) { in t.suspend (); the             }}); theTozero.addactionlistener (NewActionListener () { About              Public voidactionperformed (ActionEvent e) { theH.settext ("0"); theS.settext ("00"); theI=0; +J=0; -             } the         });Bayi     } the      the      Public Static voidMain (string[] args) { -         Newthreadtest (); -     } the}
View Code

Java Multithreaded Production timer

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.