J2ME学习笔记(3)—初次接触MIDlets
最近很多朋友喜欢上设计,但是大家却不知道如何去做,别担心有图老师给你解答,史上最全最棒的详细解说让你一看就懂。
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();
}
}
}