MyException - 我的异常网
当前位置:我的异常网» J2ME » 界面跳转解决方案

界面跳转解决方案(2)

www.MyException.Cn  网友分享于:2013-12-27  浏览:18次


------解决方案--------------------
J2ME不允许一个MIDlet调用另一个MIDlet
所以改后只留下一个MIDlet类。

Main.java
Java code

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

public class Main extends MIDlet implements CommandListener {
    public Display display;
    private Form form;
    private Command exit;
    private Command process;
    private ChoiceGroup characters;
    private ChoiceGroup cg;
    private int characterIndex;
    private TestCanvas canvas;
    public static Main instance;

    public Main() {
        instance = this;
        display = Display.getDisplay(this);
        characters = new ChoiceGroup("武汉特色介绍", Choice.EXCLUSIVE);
        characters.append("景点", null);
        characters.append("小吃/小吃街", null);
        characters.append("购物街", null);
        characters.append("大型购物商场", null);
        characters.append("大型购物超市", null);
        characters.append("大学院校", null);
        exit = new Command("Exit", Command.EXIT, 1);
        process = new Command("Process", Command.SCREEN, 2);
        form = new Form("");
        characterIndex = form.append(characters);
        form.addCommand(exit);
        form.addCommand(process);
        form.setCommandListener(this);
    }

    public void startApp() {
        display.setCurrent(form);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean arg0) {
    }

    public static void exit() {
        instance.destroyApp(true);
        instance.notifyDestroyed();
        instance = null;
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command == exit) {
            exit();
        } else if (command == process) {
            int x = 0;
            boolean picks[] = new boolean[characters.size()];
            characters.getSelectedFlags(picks);
            for (; x < picks.length; x++) {
                if (picks[x]) {
                    cg = new ChoiceGroup("", Choice.EXCLUSIVE);
                    switch (x) {
                    case 0:
                        cg.append("黄鹤楼", null);
                        cg.append("中山公园", null);
                        cg.append("江滩", null);
                        break;
                    case 1:
                        cg.append("热干面", null);
                        cg.append("豆皮", null);
                        break;
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                        break;
                    default:
                        break;
                    }
                    break;
                }
            }
            form.delete(characterIndex);
            characterIndex = form.append(cg);
            boolean picks2[] = new boolean[cg.size()];
            cg.getSelectedFlags(picks2);
            if (x == 0) {
                for (int y = 0; y < picks2.length; y++) {
                    if (picks2[y]) {
                        switch (y) {
                        case 0:
                            canvas = new TestCanvas();
                            display.setCurrent(canvas);
                            break;
                        default:
                            break;
                        }
                    }
                }
            }
            form.delete(characterIndex);
        }
    }
}

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有