获得当前时间并且停止或开始时间

时间:2011-12-26 18:00:05
【文件属性】:

文件名称:获得当前时间并且停止或开始时间

文件大小:2KB

文件格式:JAVA

更新时间:2011-12-26 18:00:05

time

import java.awt.*; import java.awt.event.*; import java.util.*; public class Time extends Frame{ Label label; Button stopButton,startButton; Panel pa; Thread th; public Time(String title){ super(title); setSize(200,200); setLocation(100,100); label=new Label(); stopButton=new Button("stop"); startButton=new Button("start"); pa=new Panel(); pa.add(stopButton); pa.add(startButton); add(label,BorderLayout.NORTH); add(pa,BorderLayout.SOUTH); th=new Thread(new MyTime()); th.start(); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); stopButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String str=e.getActionCommand(); if(str.equals("stop")){ th.stop(); } } }); startButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String str=e.getActionCommand(); if(str.equals("start")){ th=new Thread(new MyTime()); th.start(); } } }); setVisible(true); } class MyTime extends Thread{ public void run(){ while(true){ Date d=new Date(); String tt=d.toString(); System.out.println(tt); label.setText(tt); try{ Thread.sleep(1000); }catch(InterruptedException e){ System.out.println(e.getMessage()); } } } } public static void main(String[] args){ Time t=new Time("time"); } }


网友评论