以下是引用片段:
public void dismiss(){
timer.cancel(); //取消定時器
next = new TextBox("welcome","Welcome to come here!",40,TextFIEld.ANY);
display.setCurrent(next);
}
在用戶按下任意鍵後進入主界面,因此keyPressed()方法直接調用了dimiss()方法。
以下是引用片段:
protected void keyPressed(int key) {
dismiss();
}
SplashScreen的完整代碼如下:
SplashScreen.Java
以下是引用片段:
package splash.welcome.vIEw;
import Java.io.IOException;
import Java.util.Timer;
import Javax.microedition.lcdui.Canvas;
import Javax.microedition.lcdui.Display;
import Javax.microedition.lcdui.Graphics;
import Javax.microedition.lcdui.Image;
import Javax.microedition.lcdui.TextBox;
import Javax.microedition.lcdui.TextFIEld;
/**
* @author Administrator
*
* 歡迎界面的類
*/
public class SplashScreen extends Canvas {
private Display display;
/**
* 程序的主界面
*/
private TextBox next;
/**
* 定時器,一定時間後自動跳到主界面
*/
private Timer timer;
/**
* 最長的停留時間
*/
private final int MAX_TIME = 3000;
private Image image;
private String file = "/res/splash.png";
/**
* 完成初始化工作
* @param display
*/
public SplashScreen(Display display){
super();
this.display = display;
timer = new Timer();
loadImage();
display.setCurrent(this);
/**
* 設置定時器
*/
timer.schedule(new ShowMainScreen(this),MAX_TIME);
}
/**
* 取消定時器,顯示下一個頁面
*
*/
public void dismiss(){
timer.cancel();
next = new TextBox("welcome","Welcome to come here!",40,TextFIEld.ANY);
display.setCurrent(next);
}
protected void paint(Graphics g) {
g.drawImage(image,0,0,Graphics.LEFT|Graphics.TOP);
}
/**
* 用戶按下任意鍵後即進入程序的主界面
*/
protected void keyPressed(int key) {
dismiss();
}
/**
* 載入圖像
*
*/
private void loadImage(){
try {
image = Image.createImage(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
接下來看一下完成進入主界面任務的類:ShowMainScreen。它是從TimerTask繼承的。一個SplashScreen的對象作為其構造函數的參數傳遞進來。由於僅僅是取消定時器,顯示主界面,因此在run()方法中只是簡單的調用了SplashScreen的dismiss()方法。
以下是引用片段:
public void run(){
splash.dismiss();
}
ShowMainScreen的完整代碼如下:
ShowMainScreen.Java
以下是引用片段:
package splash.welcome.vIEw;
import Java.util.TimerTask;
/**
* @author Administrator
*
* 跳過歡迎界面,進入主界面的類
*/
public class ShowMainScreen extends TimerTask {
private SplashScreen splash;
public ShowMainScreen(SplashScreen splash){
this.splash = splash;
}
public void run(){
splash.dismiss();
}
}
另外,有一個Midlet來運行這個程序:
SplashMidlet.Java
以下是引用片段:
package splash.welcome.midlet;
import Javax.microedition.lcdui.Canvas;
import Javax.microedition.lcdui.Display;
import Javax.microedition.midlet.MIDlet;
import Javax.microedition.midlet.MIDletStateChangeException;
import splash.welcome.vIEw.SplashScreen;
/**
* @author Administrator
*
* 進入程序的Midlet
*/
public class SplashMidlet extends MIDlet {
private Display display;
/**
* 歡迎界面
*/
private Canvas splash;
public SplashMidlet() {
super();
display = Display.getDisplay(this);
}
protected void startApp() throws MIDletStateChangeException {
splash = new SplashScreen(display);
}
protected void pauseApp() {
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
}