日期:2014-05-20 浏览次数:20728 次
public class Testtime extends JFrame implements ActionListener,Runnable{ JButton btn1; JButton btn2; JLabel time; int minute,seconds; Thread begin; boolean flag; public Testtime(){ btn1=new JButton("开始计时 "); btn2=new JButton(" 停止计时"); time=new JLabel(0+":"+0); minute=0;seconds=0; add(btn1); add(time); add(btn2); setLayout(new FlowLayout()); setSize(400,400); setVisible(true); btn1.addActionListener(this); btn2.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource()==btn1){ begin=new Thread(Testtime.this); minute=0;seconds=0; flag=true; begin.start(); } else{ flag=false; } } public void run(){ while(flag){ seconds++; if(seconds%60==0){ seconds=0; minute++; } time.setText(minute+" : "+seconds); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String args[]){ new Testtime(); } }