**********SwitchDisplayMID
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.*;
/**
* @author Administrator
*/
//类继承了接口CommandListener
public class SwitchDisplayMID extends MIDlet implements CommandListener {
Display ds; //声明Display对象ds
TextBox text1,text2; //声明界面对象
Command cmdExit,cmdSwitch; //声明命令
//构造函数
public SwitchDisplayMID (){
ds = Display.getDisplay(this); //获取当前Display对象
//创建界面对象
text1=new TextBox("第一屏","这是第1个屏幕",30,TextField.ANY);
text2=new TextBox("第二屏","这是第2个屏幕",30,TextField.ANY);
//创建命令
cmdExit=new Command("退出",Command.EXIT,2);
cmdSwitch=new Command("切换",Command.SCREEN,2);
//将命令添加到界面对象
text1.addCommand(cmdExit);
text1.addCommand(cmdSwitch);
text2.addCommand(cmdSwitch);
//让界面对象能侦听到命令
text1.setCommandListener(this);
text2.setCommandListener(this);
//显示界面
ds.setCurrent(text1);
}
public void startApp() {
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
//实现CommandListener接口中的抽象方法commandAction
public void commandAction(Command c, Displayable d) {
if(c==cmdExit){ //检查是否为cmdExit命令
destroyApp(true);
notifyDestroyed(); //结束程序
}
else if(c==cmdSwitch ){ //检查是否为cmdSwitch命令
if(d==text1){ //当前界面对象是否为text1
ds.setCurrent(text2); //显示text2
}
else if(d==text2){ //当前界面对象是否为text2
ds.setCurrent(text1); //显示text1
}
}
}
}
本文来源:https://www.2haoxitong.net/k/doc/d71709fa770bf78a65295431.html
文档为doc格式