六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 75|回复: 0

j2me选择菜单的综合小例子

[复制链接]

升级  50.67%

34

主题

34

主题

34

主题

秀才

Rank: 2

积分
126
 楼主| 发表于 2013-2-5 01:18:40 | 显示全部楼层 |阅读模式
选择菜单是j2me重要的板块,它给我们提供了动态交互信息的窗口,选择菜单有复选、单选、隐含式单选,下面的例子式综合三者的例子,有一定的代表性。。。

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Choice;

public class ComplexListExample extends MIDlet implements CommandListener {
    private static final Command exitCommand = new Command("退出", Command.EXIT, 1);
    private static final Command backCommand = new Command("返回", Command.BACK, 2);
    private Display display;
    private List mainList, exclusiveList, mutipleList, implicitList;
    private boolean bFirstTime;
    public ComplexListExample() {
        display = Display.getDisplay(this);
    }
    protected void startApp() {
        init();
        if(bFirstTime) {
            String[] stringArray = {
                                   "Exclusive",
                                   "Implicit",
                                   "Multiple"
                                  };
            mainList = new List("Choose type", Choice.IMPLICIT, stringArray, null);
            mainList.addCommand(exitCommand);
            mainList.setCommandListener(this);
            display.setCurrent(mainList);
            bFirstTime = false;
        }
    }
    protected void pauseApp() {
        
    }
    protected void destroyApp(boolean unconditional) {
        
    }
    public void commandAction(Command c, Displayable d) {
        if(d.equals(mainList)) {
            if (c == List.SELECT_COMMAND) {
                switch (((List) d).getSelectedIndex()) {
                case 0:
                    display.setCurrent(exclusiveList);
                    break;
                case 1:
                    display.setCurrent(implicitList);
                    break;
                case 2:
                    display.setCurrent(mutipleList);
                    break;
                }
             }
        } else {
            if(c == backCommand) {
                display.setCurrent(mainList);
            }
        }
        if(c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
    }
   
    public void init() {
        String[] elements = {"one", "two", "three", "four"};
        exclusiveList = new List("mainList", Choice.EXCLUSIVE, elements, null);
        exclusiveList.addCommand(exitCommand);
        exclusiveList.addCommand(backCommand);
        exclusiveList.setCommandListener(this);
        
        mutipleList = new List("mainList", Choice.MULTIPLE, elements, null);
        mutipleList.addCommand(exitCommand);
        mutipleList.addCommand(backCommand);
        mutipleList.setCommandListener(this);
        
        implicitList = new List("mainList", Choice.IMPLICIT, elements, null);
        implicitList.addCommand(exitCommand);
        implicitList.addCommand(backCommand);
        implicitList.setCommandListener(this);
        
        bFirstTime = true;
    }
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表