当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015计算机二级考试指导:如何实现在不同screen的切换
发布时间:2010/3/10 16:13:35 来源:城市学习网 编辑:MOON
  在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 (cview && s = m)
  {
  display.setCurrent(t);
  }
  }
  }
  以上代码中,主类实现了Commandlistener接口,所有屏幕的command都由这个类负责监听和处理。那么相当于由这个主类来决定如何切换屏幕.
  就这么简单。
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved