자바 스 레 드 구현 스톱워치

3732 단어
package ch2;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.*;


public class Timer extends JFrame implements Runnable{
private JButton start,stop,pause;    //    
private JLabel minisec,minute,second;   //     
private JLabel fen,miao;//   
private Font font=new Font("Consolas",Font.BOLD,50);//  
private Font font2=new Font("  ",Font.BOLD,35);
Container c;
private Thread thread=null;
Timer(){
init();//   
setVisible(true);//    
setResizable(false);//false      
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//     
}
public void init() {
this.setTitle("  ");//    
this.setBounds(400, 200, 500, 300);//    
c = getContentPane();
c.setLayout(null);//     
/*    */
start=new JButton("  ");
start.setBounds(new Rectangle(50,180,80,30));

c.add(start);

start.addActionListener(new ActionListener() {/*       */
public void actionPerformed(ActionEvent evt) {
start.setEnabled(false);
stop.setEnabled(true);
pause.setEnabled(true);
Start();
}
});
/*    */
stop=new JButton("  ");
stop.setBounds(new Rectangle(210,180,80,30));
c.add(stop);
stop.setEnabled(false);//false        
stop.addActionListener(new ActionListener() {//  
public void actionPerformed(ActionEvent evt) {
start.setEnabled(true);
stop.setEnabled(false);
pause.setEnabled(false);
Stop();

}
});
/*    */
pause=new JButton("  ");
pause.setBounds(new Rectangle(370,180,80,30));
pause.setEnabled(false);
c.add(pause);
pause.addActionListener(new ActionListener() {//  
public void actionPerformed(ActionEvent evt) {
start.setEnabled(true);
stop.setEnabled(true);
pause.setEnabled(false);
Pause();

}
});
/*       */
minute=new JLabel("00");
minute.setBounds(new Rectangle(60,48,80,100));
minute.setFont(font);
c.add(minute);

fen=new JLabel(" ");
fen.setBounds(new Rectangle(150,53,80,80));
fen.setFont(font2);
c.add(fen);

second=new JLabel("00");
second.setBounds(new Rectangle(220,48,80,100));
second.setFont(font);
c.add(second);

miao=new JLabel(" ");
miao.setBounds(new Rectangle(310,53,80,80));
miao.setFont(font2);
c.add(miao);

minisec=new JLabel("00");
minisec.setBounds(new Rectangle(380,48,80,100));
minisec.setFont(font);
c.add(minisec);
}
public void Start() {
if(thread==null)//          
{
thread=new Thread(this);
thread.start();
}
else {//       ,  
thread.resume();
}
}
public void Stop() {
if(thread!=null) {
thread.stop();
thread=null;
}
minute.setText("00");
second.setText("00");
minisec.setText("00");
}
public void Pause() {
if(thread!=null) {
thread.suspend();//    ,  ,        
}


}

/*       ,     */

public void run()
{
int m=0,s=0,ms=0;
while(true) {
ms++;
if(ms>=10) {
ms=0;
s++;
if(s>=60) {
s=0;
m++;

if(m>=60) {

/*   60     ,           ,         。        ,   if(m>=60) {m=0;break;}*/

m=1;
s=0;
ms=0;
fen.setText(" ");
miao.setText(" ");
while(true) {
ms++;
if(ms>=60) {
ms=0;
s++;
if(s>=60) {
s=0;
m++;
if(m>=24) {
m=0;
break;
}
}
}
minisec.setText(String.format("%02d", ms));
second.setText(String.format("%02d", s));
minute.setText(String.format("%02d", m));
try {
           Thread.sleep(1000);     
       } catch (InterruptedException e) {
           break;
       }
}
break;
}
}
}
minisec.setText(String.format("%02d", ms));
second.setText(String.format("%02d", s));
minute.setText(String.format("%02d", m));
try {
            Thread.sleep(100);     
        } catch (InterruptedException e) {
            break;//        
        }
}

}

/*   */

public static void main(String[] args) {
Timer timer=new Timer();
}
}

좋은 웹페이지 즐겨찾기