菜单项如何实现

来源:互联网 发布:硬盘阵列数据恢复 编辑:程序博客网 时间:2024/06/05 18:20

看这个例子:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class midpLiftcycle extends MIDlet implements CommandListener
{
   Display display;
   Form form;
   Command pauseCommand;
   Command exitCommand;
   Command okCommand;
   boolean isPause;
   boolean isOver;

   public midpLiftcycle()
   {
      display=Display.getDisplay(this);
      form=new Form("程序生命周期");
      okCommand=new Command("确认",Command.OK,2);
      pauseCommand = new Command("暂停", Command.ITEM, 2);
      exitCommand = new Command("离开", Command.EXIT, 1);
      form.append("第一次先运行构造函数初始设定值"); 
      form.addCommand(pauseCommand);
      form.addCommand(exitCommand);
      
form.addCommand(okCommand);
      form.setCommandListener(this);
   }

   public void startApp()
   {
      if(!isPause)
        form.append("第二次运行startApp区域程序代码");
      else if(isOver)
        destroyApp(false);
      else
        pauseApp();
       
 
      display.setCurrent(form);        
   }

   public void pauseApp()
   {
      form.append("第三次运行pauseApp区域程序代码");
   }

   public void destroyApp(boolean unconditional)
   {
      form.append("第四次运行destroyApp离开MIDlet程序");
   }

   public void commandAction(Command cmd, Displayable disp)
   {
      if (cmd == pauseCommand)
      {
        isPause=true;
         startApp();
      }
      else if (cmd == exitCommand){
         isOver=true;
         destroyApp(false);
      }
   }
}

若将上面红色的代码去掉,则既有两个Command对象,显示如下:

 

就是多加了一条Command对象,也就是当有三个或三个以上Command对象时,就会自动出现“菜单”项且在右边,而“离开”项为默认的左菜单项。

 

 

原创粉丝点击