欢迎光临
我们一直在努力

J2ME学习笔记(1)–实现手机屏幕的切换-JSP教程,J2ME开发

建站超值云服务器,限时71元/月

在编写手机程序时,经常要进行各个屏幕间的切换。一开始不知道怎么样去做,这两天看了一个例子,很简单,但很能说明问题。总结一下。如有不妥之外,请高手纠正!

功能:在主程序中有多个按钮,每一个按钮对应一个功能,每一个功能要不同的屏幕(元素)表现出来。

实现:

一、主程序中必然定义了一个display对像,如private display display,它表示当前的屏幕。还有一些displayable对像。如form,textfield等都是displayable的子类。在主程序中通过dipslay.sercurrent(displayable实例名); 即可将当displayable实例加入当前的屏幕。以下程序:

private display display;

private form form = new form(“一个form“);

public void startapp()

{

display.setcurrent(form);

}

作用是将form添加到当前的屏幕当中。

二、要想进行屏幕间的切换,只要将你想显示的东东放到到主程序的display对象中即可。主程序中定义了一个display,则要在另一个屏幕(我姑且把它称之为目标屏幕)中引用到主程序的display。

开发环境:win2000 server+eclipse+wtk2.1

用以下代码说明:

mainmidlet.java:主程序,一个标准的midlet。

import javax.microedition.midlet.midlet;

import javax.microedition.lcdui.*;

public class mainmidlet extends midlet implements commandlistener {

private display display;

private form form = new form("wellcome!!");

private command okcommand = new command("ok",command.ok,1);//选择ok,换到下一个屏幕

private form ns ;

private stringitem si = new stringitem("first screen","~_~");

public mainmidlet()

{

form.addcommand(okcommand);

form.append(si);

}

public void startapp() {

display = display.getdisplay(this);

display.setcurrent(form);

form.setcommandlistener(this);//对form加入commandlistener

}

public void pauseapp() {

}

public void destroyapp(boolean b){

}

public void commandaction(command c,displayable s)

{

if(c==okcommand)

{

ns = new nextscreen(display,form);//最关键的地方在这里:)

display.setcurrent(ns);

}

}

}

在这个midlet中,定义了一个display对像display。

以及一个两个displayable对象form form及stringitem si。运行后显示在屏幕当中。

还有个一command okcommand,其作用是触发下一个屏幕。

在public void commandlistener中可以看到,当当前按下的按钮是okcommand时,初始化一个nextscreen对象

ns = new nextscreen(display,form);

将display和form传入,作用就是进行屏幕的切换。

下面是nextscreen的实现:

nextscreen.java 第二个屏幕的代码

import javax.microedition.lcdui.*;

public class nextscreen extends form implements commandlistener {

private display display;

private displayable parent;

private command backcommand = new command("back",command.back,1);

private stringitem si = new stringitem("secondscrean","~_~");

public nextscreen(display d,displayable p)

{

super("nextscreen");

display = d;

parent = p;

append(si);

addcommand(backcommand);

setcommandlistener(this);

}

public void commandaction(command c,displayable s)

{

//返回上一个屏幕

if(c==backcommand)

{

display.setcurrent(parent);

}

}

}

它继承自form类。

在nextscreen中又定义了一个display display,将用它来标识当前的元素显示在哪一个屏幕中。

一个form form,一个stringitem si。这是当前屏幕中要显示的东东:)

构造函数中的super("secondscreen");的作用是使得nextscreen可以直接调中其父类form中的函数。

backcommand的作用是返回上一个屏幕。

将form,si及backcommand加入nextscreen中,一个nextscreen的实例就完成了。在主程序(mainmidlet)中,就是ns。

接下来,最关键的地方,就是在mainmidlet中的这一句:display.setcurrent(ns);

就是把ns在当前的屏幕中显示出来!这样就可以看到nextscreen中定义的各个元素(form,si)了!

然后想返回原屏幕,怎么办呢?这时nextscreen中的backcommand就起作用了。

仔细看这两句:

在mainmidlet.java中:

ns = new nextscreen(display,form);

它将form也传了进去。它有什么用呢?

在nextscreen的构造函数中:

dispaly =d;

这一句其实等于:nextscreen.display = mainmidlet.display;

这样,nextscreen就得到了当前的屏幕,它就随意的在上面放东东了。

parent = p;

这一句其实等于:nextscreen.parent = mainmidlet.form;

从字面意思不难理解,原来是把主程序的form当成parent(父母),这样就得到当前屏幕的前一个屏幕中所显示的内容!!

然后在commandaction中,如果backcommand按下了,则执行display.sercurrent(parent);这样,又把原来的屏幕给show出来了:)

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » J2ME学习笔记(1)–实现手机屏幕的切换-JSP教程,J2ME开发
分享到: 更多 (0)

相关推荐

  • 暂无文章