Chinaunix首页 | 论坛 | 博客
  • 博客访问: 251794
  • 博文数量: 47
  • 博客积分: 7052
  • 博客等级: 少将
  • 技术积分: 1155
  • 用 户 组: 普通用户
  • 注册时间: 2005-02-19 23:32
文章分类

全部博文(47)

文章存档

2011年(2)

2010年(10)

2009年(21)

2008年(14)

我的朋友

分类: Java

2010-01-24 17:14:39

在midlet开发中,屏幕只有一个。如果需要显示不同的内容,可以在后台先准备好要显示的内容,然后通过Display.setcurrent(displayable d)函数来解决这个问题。

但是如何控制显示不同的内容呢?如果是程序里自动控制,那么就不存在这个问题;如果需要用户干预,进行屏幕的切换,又是如何实现的呢?

其实思路也很简单,为每个屏幕设置相应的menu,然后这些menu的控制,统一由一个类来处理,那么就可以实现不同屏幕之间的切换了。


package hello;

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

public class HelloMIDlet extends MIDlet implements CommandListener {

    private Command exitCommand; // The exit command

    private Command view;
    private Display display; // The display for this MIDlet

    private TextBox t;

    private MyCanvas m;

    public HelloMIDlet() {
        display = Display.getDisplay(this);
        exitCommand = new Command("Exit", Command.EXIT, 0);
        view = new Command("View", Command.ITEM, 1);
    }

    public void startApp() {
        t = new TextBox("Hello", "Hello, World!", 256, 0);

        t.addCommand(exitCommand);
        t.addCommand(view);
        t.setCommandListener(this);
        
        MyCanvas m=new MyCanvas();
        m.addCommand(exitCommand);
        m.addCommand(view);
        m.setCommandListener(this);
        if (System.getProperty(
      "microedition.io.file.FileConnection.version") != null)
                t.setTicker(new Ticker(System.getProperty(
      "microedition.io.file.FileConnection.version")));
            else
                t.setTicker(new Ticker("no"));
        display.setCurrent(m);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
        if (c==view && s = m)
        {
            display.setCurrent(t);
        }
    }

}


以上代码中,主类实现了Commandlistener接口,所有屏幕的command都由这个类负责监听和处理。那么相当于由这个主类来决定如何切换屏幕.
就这么简单。
阅读(469) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~