J2ME手机屏幕的切换

在编写手机程序时,经常要进行各个屏幕间的切换

功能:在主程序中有多个按钮,每一个按钮对应一个功能,每一个功能要不同的屏幕(元素)表现出来。
实现:
一、主程序中必然定义了一个display对像,如private Display display,它表示当前的屏幕。还有一些Displayable对像。如form,textfield等都是displayable的子类。在主程序中通过dipslay.sercurrent(displayable实例名); 即可将当displayable实例加入当前的屏幕。以下程序:

  1. private Display display=Display.getDisplay(this); 
  2. private Form form = new Form(“New Form“); 
  3. public void startapp() 
  4. display.setcurrent(form); 
  5. }

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

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

用以下代码说明:

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

  1. import javax.microedition.midlet.midlet; 
  2. import javax.microedition.lcdui.*;
  3. public class mainmidlet extends midlet implements commandlistener {
  4. private display display; 
  5. private form form = new form("wellcome!!"); 
  6. private command okcommand = new command("ok",command.ok,1);
  7. //选择ok,换到下一个屏幕 
  8. private form ns ; 
  9. private stringitem si = new stringitem("first screen","~_~"); 
  10. public mainmidlet() 
  11.     form.addcommand(okcommand); 
  12.     form.append(si); 
  13. public void startapp() { 
  14.     display = display.getdisplay(this);
  15.     display.setcurrent(form); 
  16.     form.setcommandlistener(this);//对form加入commandlistener
  17. public void pauseapp() {
  18. public void destroyapp(boolean b){
  19. public void commandaction(command c,displayable s) 
  20.     if(c==okcommand) 
  21.     { 
  22.         ns = new nextscreen(display,form);//最关键的地方在这里:form:将父屏幕对象传下去,方便后退时返回父屏幕) 
  23.         display.setcurrent(ns); 
  24.     }
  25. }

在这个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 第二个屏幕的代码

  1. import javax.microedition.lcdui.*; 
  2. public class nextscreen extends form implements commandlistener {
  3. private display display; 
  4. private displayable parent; 
  5. private command backcommand = new command("back",command.back,1); 
  6. private stringitem si = new stringitem("secondscrean","~_~"); 
  7. public nextscreen(display d,displayable p) 
  8.     super("nextscreen"); 
  9.     display = d; 
  10.     parent = p; 
  11.     append(si); 
  12.     addcommand(backcommand); 
  13.     setcommandlistener(this);
  14. public void commandaction(command c,displayable s) 
  15. //返回上一个屏幕 
  16.     if(c==backcommand) 
  17.     { 
  18.     display.setcurrent(parent); 
  19.     } 
  20. }

它继承自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
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 25
    评论
评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值