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