fixedrateschedulemidlet.java

来自「j2me实例代码」· Java 代码 · 共 56 行

JAVA
56
字号
package timeimpl;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.*;
public class FixedRateScheduleMIDlet extends MIDlet{
	private Timer aTimer;
	private ClockTimerTask aTimerTask;
	Displayable d;
	private Date currentTime;
	private Calendar now;
	private String nowString="";
	protected void startApp()  {
		d=new ClockCanvas();
		d.addCommand(new Command("Exit",Command.EXIT,0));
		d.setCommandListener(new CommandListener(){
			public void commandAction(Command c,Displayable s){
				notifyDestroyed();
			}
		});
		currentTime=new Date();
		now=Calendar.getInstance();
		now.setTime(currentTime);
		nowString=now.get(Calendar.HOUR)+"Hours"+now.get(Calendar.MINUTE)+"Minute"+now.get(Calendar.SECOND)+"Second";
		aTimer=new Timer();
		aTimerTask=new ClockTimerTask();
		aTimer.scheduleAtFixedRate(aTimerTask,10,1000);
		Display.getDisplay(this).setCurrent(d);
	}
	protected void pauseApp() {
	}
	protected void destroyApp(boolean arg0) {
	}
	class ClockCanvas extends Canvas{
		public void paint(Graphics g){
			int width=getWidth();
			int height=getHeight();
			g.setGrayScale(255);
			g.fillRect(0,0,width-1,height-1);
			g.setGrayScale(0);
			g.drawRect(0,0,width-1,height-1);
			g.drawString(nowString,10,10,Graphics.TOP|Graphics.LEFT);
		}
	}
	class ClockTimerTask extends TimerTask{
		public final void run(){
			currentTime=new Date();
			now=Calendar.getInstance();
			now.setTime(currentTime);
			nowString=now.get(Calendar.HOUR)+"Hours"+now.get(Calendar.MINUTE)+"Minute"+now.get(Calendar.SECOND)+"Second";
			((ClockCanvas)d).repaint();
		}
	}	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?