循序渐进学J2Me,第四步

原来只有一个Form,现在增加一个,选Next命令就可以进入下一画面了。不解释了,注意代码的变化。这回的Form里面加StringItem我就不套getStringItem了。


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

/**
 * @author netscaner
 */
public class Midlet extends MIDlet implements CommandListener {
    private Form showForm;
    private StringItem stringItem;
    private Command exitCommand;
    private Command NextCommand;
    private Form NextScreen;

    public StringItem getStringItem() {
        if (stringItem == null) {
            stringItem = new StringItem("Hello", "Hello, World!");
        }
        return stringItem;
    }

  public Form getForm() {
        if (showForm == null) {
            showForm = new Form("Welcome", new Item[] { getStringItem() });
            showForm.addCommand(getExitCommand());
            showForm.addCommand(getNextCommand());
            showForm.setCommandListener(this);
        }
        return showForm;
    }

    public void startMIDlet() {
        switchDisplayable(null, getForm());
    }


    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
        Display display = getDisplay();
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }
    }

        public Display getDisplay () {
        return Display.getDisplay(this);
    }

    public Command getExitCommand() {
        if (exitCommand == null) {
            exitCommand = new Command("Exit", Command.EXIT, 0);
        }
        return exitCommand;
    }

    public Command getNextCommand() {
        if (NextCommand == null) {
            NextCommand = new Command("Next", Command.SCREEN, 1);
        }
        return NextCommand;
    }
    public void commandAction(Command command, Displayable displayable) {
                                         
        if (command == exitCommand) {
                exitMIDlet();                                          
         }

        if (command == NextCommand) {
            Form NextScreen = new Form("Next",null);
            NextScreen.append(new StringItem("Next", "Next Screen"));
            NextScreen.addCommand(getExitCommand());
            NextScreen.setCommandListener(this);
            Display display = getDisplay();
            display.setCurrent(NextScreen);

            }
    }

    public void startApp() throws MIDletStateChangeException  {
            startMIDlet ();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值