日期:2014-05-20 浏览次数:20835 次
package midp.wallimn.com; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.Item; import javax.microedition.lcdui.Spacer; import javax.microedition.lcdui.StringItem; import javax.microedition.lcdui.TextBox; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; /** *//** * 这是个秒表程序,以我的L6g为运行平台,是我自己编写的第一个手机程序, 很快就调试运行成功,很有成就感。 <br/> * * @version : V1.0<br/> * @author : (Email: wallimn@sohu.com QQ: 54871876)<br/> * @date : 2008-1-14 下午01:37:28<br/> */ public class StopWatchMIDlet extends MIDlet implements CommandListener ...{ //private TextBox tBox;// 显示 //原来使用TextBox做显示用,用户敲键盘会出现输入窗口。 private Form mainForm; private StringItem stringItem; private Timer m_timer;// 定时器 private Display display; private Command EXIT_CMD = new Command("退出", Command.EXIT, 1); private Command STOP_CMD = new Command("停止", Command.STOP, 1); private Command START_CMD = new Command("开始", Command.STOP, 1); class MyTimerTask extends TimerTask ...{ int s = 0;//秒 int m = 0;//分 int h = 0;//时 int ms = 0;//1/100秒 StringBuffer sb = null; public MyTimerTask()...{ sb = new StringBuffer(); } public void init() ...{ s = 0; m = 0; h = 0; ms = 0; } public String getString() ...{ //StringBuffer sb = new StringBuffer(); sb.delete(0, sb.length()); if (h <= 9) sb.append("0"); sb.append(h); sb.append(":"); if (m <= 9) sb.append("0"); sb.append(m); sb.append(":"); if (s <= 9) sb.append("0"); sb.append(s); sb.append("."); sb.append(ms); return sb.toString(); } public void run() ...{ if (ms == 9) ...{ ms = 0; if (s == 59) ...{ s = 0; if (m == 59) ...{ m = 0; h++; } else ...{ m++; } } else ...{ s++; } } else ...{ ms++; } stringItem.setText(getString()); } } public StopWatchMIDlet() ...{ display = Display.getDisplay(this); mainForm = new Form("=秒表程序="); Spacer spacer = new Spacer(20,mainForm.getHeight()/2-18); mainForm.append(spacer); stringItem = new StringItem("","00:00:00.0"); stringItem.setLayout(Item.LAYOUT_CENTER); mainForm.append(stringItem); mainForm.addCommand(EXIT_CMD);//添加命令显示 mainForm.addCommand(START_CMD);//添加命令显示 mainForm.setCommandListener(this);//添加事件监听 } protected void destroyApp(boolean arg0) ...{ stringItem = null; } protected void pauseApp() ...{ } protected void startApp() throws MIDletStateChangeException ...{ display.setCurrent(mainForm); } public void commandAction(Command c, Displayable s) ...{ if (c == EXIT_CMD) ...{ if(m_timer!=null)...{//停止计时器,释放资源 m_timer.cancel(); m_timer=null; } try ...{ destroyApp(false); } catch (Exception e) ...{ } notifyDestroyed(); } else if (c == STOP_CMD) ...{ this.m_timer.cancel(); m_timer = null; mainForm.removeCommand(STOP_CMD); mainForm.addCommand(START_CMD); } else if(c==START_CMD)...{ m_timer = new Timer(); m_timer.schedule(new MyTimerTask(), new Date(),100); mainForm.removeCommand(START_CMD); mainForm.addCommand(STOP_CMD); } } }