Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
J2ME中建立Splash启动界面

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

很显然,最简单的建立Splash启动界面的方法是用Alert ,在应用程序启动的时候用Alert 显示启动信息和图片一段时间直到应用程序启动完成.因此用Alert 做启动界面是非常简单的,具体实现方法可参考如下代码:

 

public void showSplashScreen( 
                     Display d, Displayable next ){
    Image logo = null;
    
    try {
      logo = Image.createImage(
                              "/images/logo.png" );
    }
    catch( IOException e ){
    }
    
    Alert a = new Alert( "Time Tracker", 
           "Copyright 2001 by Nobody, Inc.",
            logo, null ); 
    a.setTimeout( Alert.FOREVER );
    display.setCurrent( a, next );
}

 

 

不过使用Alert 也许不能满足您灵活的需要,比如需要用任何按钮取消Splash , 或者显示一个简单的动画 .这个时候,我们可以用Canvas 来代替Alert .请参见如下代码:

 

import java.util.*;
import javax.microedition.lcdui.*;
 
public class SplashScreen extends Canvas {
    private Display     display;
    private Displayable next;
    private Timer       timer = new Timer();
 
    public SplashScreen( 
               Display display, Displayable next ){
        this.display = display;
        this.next    = next;
 
        display.setCurrent( this );
    }
 
    protected void keyPressed( int keyCode ){
        dismiss();
    }
 
    protected void paint( Graphics g ){
        // do your drawing here
    }
 
    protected void pointerPressed( int x, int y ){
        dismiss();
    }
 
    protected void showNotify(){
        timer.schedule( new CountDown(), 5000 );
    }
 
    private void dismiss(){
        timer.cancel();
        display.setCurrent( next );
    }
 
    private class CountDown extends TimerTask {
        public void run(){
            dismiss();
        }
    }
}

 

 

       为了显示这个Splash界面,为他建立一个实例 当你建立一个SplashScreen的实例,把它传递给MIDlet的显示类, 你也可以在Splash界面释放的时候把Display激活.

 

public void showSplashScreen( 
               Display display, Displayable next ){
    new SplashScreen( display, next );
}

 

这个例子,无论是用户按下了任意键或者点击了屏幕(如果设备支持触摸屏的的化),如果什么事件也没有发生,Splash屏幕将在第一次显示5秒后消失.

 

       我们在程序中什么时候显示Splash?你也许会想到在MIDletstartApp方法的最后一行显示Splash,但请记住,startApp方法在一次进程中有可能被调用多次,构造函数中也不是一个显示Splash的好地方,因为MIDP的规范中没有保证在构造的时候初始化Display 对象 .所以startApp 是可以显示Splash的地方,不过一定要注意,只能够显示一次. 同时startApp中也是可以安全获取到Display对象 , 所以结合以上两点,见如下代码:

 

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
 
public class MyMIDlet extends MIDlet
                      implements CommandListener {
 
    private Display display;
    private Command exitCommand = new Command( 
                         "Exit", Command.EXIT, 1 );
 
    public MyMIDlet(){
    }
 
    protected void destroyApp(
      boolean unconditional )
               throws MIDletStateChangeException {
        exitMIDlet();
    }
 
    protected void pauseApp(){
    }
 
    protected void startApp() 
                throws MIDletStateChangeException {
        if( display == null ){
                            // first time called...
            initMIDlet();
        }
    }
 
    private void initMIDlet(){
        display = Display.getDisplay( this );
        new SplashScreen(
                       display, new TrivialForm() );
    }
 
    public void exitMIDlet(){
        notifyDestroyed();
    }
 
    public void commandAction( 
                        Command c, Displayable d ){
        exitMIDlet();
    }
 
    // A trivial UI screen
 
    class TrivialForm extends Form {
        TrivialForm(){
            super( "MyMIDlet" );
            addCommand( exitCommand );
            setCommandListener( MyMIDlet.this );
        }
    }
}

 

 

MIDlet程序中,并非每次客户启动程序都要显示Splash , 你应该在用户第一次启动MIDlet的时候显示Splash , 下次用户再启动的时候就不要再显示了. 为此,你可以用记录管理系统(RMS) 存储程序是否已经被调用的标识, 只有当RMS中没有该标识的时候才显示Splash ,当显示完Splash 后你就把标识写入RMS,这样就不会重复调用了.




相关文章

相关软件