⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timermidlet.java

📁 j2me实例代码
💻 JAVA
字号:
package timeimpl;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import java.lang.*;
import java.io.*;
import javax.microedition.lcdui.*;
import java.util.*;

public class TimerMIDlet extends MIDlet implements CommandListener{

	final private static int GAUGE_MAX=10;
	private boolean timerRunning;
	private Display display;
	
	private Gauge gaugeOne;
	private Gauge gaugeTwo;
	
	private Form myScreen;
	private Command cmdOK;
	private Command cmdExit;
	
	private Timer timer;
	private MyTimerTask timerTaskOne;
	private MyTimerTask timerTaskTwo;
	
	private class MyTimerTask extends TimerTask{
		private Gauge myGauge;
		private boolean goUp;
		private int num;
		
		public MyTimerTask(Gauge g,boolean up){
			myGauge=g;
			goUp=up;
		}
		public void run() {
			num++;
			myGauge.setValue(goUp?GAUGE_MAX-(num%GAUGE_MAX):num%GAUGE_MAX);		
		}
		
	}
	public TimerMIDlet() {
		super();
		display=Display.getDisplay(this);
		myScreen=new Form("TimerMIDlet");
		gaugeOne=new Gauge("Up Gauge",false,GAUGE_MAX,0);
		myScreen.append(gaugeOne);
		gaugeTwo=new Gauge("Down Gauge",false,GAUGE_MAX,GAUGE_MAX);
		myScreen.append(gaugeTwo);
		
		cmdOK=new Command("OK",Command.OK,1);
		cmdExit=new Command("Exit",Command.EXIT,1);
		myScreen.addCommand(cmdOK);
		myScreen.addCommand(cmdExit);
		myScreen.setCommandListener(this);
	}
	
	private void flipFlop(){
		if(timerRunning){
			timerTaskOne.cancel();
			timerTaskTwo.cancel();
			timer.cancel();
			timerRunning=false;
		}else{
			timer=new Timer();
			timerTaskOne=new MyTimerTask(gaugeOne,false);
			timerTaskTwo=new MyTimerTask(gaugeTwo,true);
			timer.schedule(timerTaskOne,0,1000);
			timer.schedule(timerTaskTwo,0,1000);
			timerRunning=true;
		}
	}
	protected void startApp() throws MIDletStateChangeException {
		display.setCurrent(myScreen);
		flipFlop();

	}

	protected void pauseApp() {
		

	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		

	}

	public void commandAction(Command c, Displayable g) {
		if(c==cmdOK){
			flipFlop();
		}else if(c==cmdExit){
			try{
				destroyApp(false);
				notifyDestroyed();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
	}

}

⌨️ 快捷键说明

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