J2ME学习笔记(三)-----学习MIDlets

王朝java/jsp·作者佚名  2006-01-30
宽屏版  字体: |||超大  

处理MIDlets

1. MIDlet是使用MIDP特征和CLDC配置的MIDlet应用

1).MIDlet是打包成JAD(JAVA描述符)文件的Java类文件

2).MIDlet运行在已安装于MIDP设备上的Application Management Software(应用管理软件AMS).AMS提供KVM和MIDlets的环境

3).MIDlet是在支持CLDC和MIDP的手持设备中使用.

2. MIDlet的生命周期(图)

3. 开发MIDlets实例

1).任务陈述-----SaveMyMoney移动银行应用的第一个屏幕上要显示的消息为”Welcome to SaveMyMoney Bank!”,屏幕顶部有一个显示消息"Welcome to the World of Mobile Banking!"的滚动文本;

第二个屏幕上要显示的消息为"Dear Customer, , You can view your personal account information by entering your PIN number and sending it to the number 9002. If you have not received the PIN number, please contact us at our Head Office."屏幕顶部有一个显示消息"Note: Your PIN number has been sent to you at your mailing address."的滚动文本

2).代码如下-----

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

//需要实现lcdui类中的CommandListener接口

public class MB extends MIDlet implements CommandListener

{

//用Display类管理显示和用户的输入

Display display;

Form form1;

Form form2;

//定义两个滚动条ticker1,ticker2

Ticker ticker1;

Ticker ticker2;

static final Command okCommand = new Command("Info",Command.OK,1);

static final Command backCommand = new Command("Back",Command.BACK,0);

static final Command exitCommand = new Command("Exit", Command.STOP, 2);

public MB()

{

}

public void startApp() throws MIDletStateChangeException

{

ticker1 = new Ticker("Welcome to the World of Mobile Banking!");

ticker2 = new Ticker("Note: Your PIN number has been sent to you at your mailing address.");

display = Display.getDisplay(this);

form1 = new Form("SaveMyMoney");

form2 = new Form("CUSTOMER CARE");

StringItem strItem = new StringItem("Welcome to SaveMyMoney Bank!", "");

StringItem strItem1 = new StringItem("Dear Customer, ", "You can view your personal account information by entering your PIN number and sending it to the number 9002. If you have not received the PIN number, please contact us at our Head Office.");

form1.append(strItem);

form2.append(strItem1);

//把命令加入到屏幕,第一个屏幕的左软键是Exit,右软键是OK

form1.addCommand(exitCommand);

form1.addCommand(okCommand);

//监听

form1.setCommandListener(this);

form1.setTicker(ticker1);

//设置主屏幕的当前显示为Form1

display.setCurrent(form1);

}

public void pauseApp()

{

}

public void destroyApp(boolean unconditional)

{

notifyDestroyed();

}

public void showForm1()

{

form1.addCommand(exitCommand);

form1.addCommand(okCommand);

form1.setCommandListener(this);

display.setCurrent(form1);

}

public void showForm2()

{

form2.addCommand(exitCommand);

form2.addCommand(backCommand);

form2.setCommandListener(this);

form2.setTicker(ticker2);

display.setCurrent(form2);

}

public void commandAction(Command cmd, Displayable displayable)

{

String label = cmd.getLabel();

if (label.equals("Exit"))

{

//调用撤消MIDlet的动作并退出此应用

destroyApp(true);

}

else if (label.equals("Back"))

{

// go back to Form1

showForm1();

}

else

{

showForm2();

}

}

}

测试效果如下:

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有