📄 j-j2me-10-5.shtml
字号:
menu12over.src="imagemaster/himenu12.jpg";
menu12out.src="imagemaster/menu12.jpg";
var mainblurb="主菜单";
var sectionblurb="章节菜单";
var feedbackblurb="给出此教程的反馈意见";
var previousblurb="上一屏";
var nextblurb="下一屏";
var nextsectionblurb="下一章";
topmainover.src="../i/h-main.gif";
topmainout.src="../i/main.gif";
bottommainover.src="../i/h-main.gif";
bottommainout.src="../i/main.gif";
topsectionover.src="../i/h-section.gif";
topsectionout.src="../i/section.gif";
bottomsectionover.src="../i/h-section.gif";
bottomsectionout.src="../i/section.gif";
topfeedbackover.src="../i/h-feedback.gif";
topfeedbackout.src="../i/feedback.gif";
bottomfeedbackover.src="../i/h-feedback.gif";
bottomfeedbackout.src="../i/feedback.gif";
toppreviousover.src="../i/h-previous.gif";
toppreviousout.src="../i/previous.gif";
bottompreviousover.src="../i/h-previous.gif";
bottompreviousout.src="../i/previous.gif";
topnextover.src="../i/h-next.gif";
topnextout.src="../i/next.gif";
bottomnextover.src="../i/h-next.gif";
bottomnextout.src="../i/next.gif";
topnextsectionout.src="../i/nextsection.gif";
topnextsectionover.src="../i/h-nextsection.gif";
bottomnextsectionout.src="../i/nextsection.gif";
bottomnextsectionover.src="../i/h-nextsection.gif";
}
function iOut(image)
{
if (browser=="N3")document[image].src=eval(image + "out.src");
}
function iOver(image)
{
if (browser=="N3")document[image].src=eval(image + "over.src");
}
// --></script><a href="#navskip"><img border="0" alt="跳至主目录" src="../i/c.gif" width="1" height="1"></a><!--#include virtual="/developerWorks/inc/tuto-inc/tuto-header.inc" --><!--#include virtual="/developerWorks/inc/tuto-inc/tuto-java-header.inc" --><br x="7"><img src="imagemaster/masthead.jpg" alt="J2ME:循序渐进" width="500" height="40"></td>
<td width="*" valign="bottom" align="right"><a border="0" href="../tutorial.zip"><img alt="下载教程 zip 文件" border="0" src="../i/icon-zip.gif" width="38" height="26"></a><a href=../tutorial_eng/index.shtml target=_blank><img alt="英文原文" border="0" src="../i/icon-source.gif" width="43" height="26"></a></td></tr><tr><td height="2" colspan="2"><img height="2" width="1" src="../i/c.gif"></td></tr></table><TABLE border="0" cellpadding="0" cellspacing="0" width="100%"><TR><TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD></TR><TR><TD background="../i/sw-gold.gif"><a border="0" href="index.shtml" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜单" border="0" src="../i/main.gif" name="topmain" width="77" height="15"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index10.shtml"><img alt="章节菜单" border="0" src="../i/section.gif" name="topsection" width="98" height="15"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-j2me-12-3.shtml"><img alt="给出此教程的反馈意见" border="0" src="../i/feedback.gif" name="topfeedback" width="80" height="15"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img src="../i/c.gif" width="1" height="1"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topprevious'); iOver('bottomprevious'); self.status=previousblurb; return true;" onMouseOut="iOut('topprevious'); iOut('bottomprevious'); self.status=''; return true;" href="j-j2me-10-4.shtml"><img alt="上一屏" border="0" src="../i/previous.gif" name="topprevious" width="77" height="15"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-j2me-10-6.shtml"><img alt="下一屏" border="0" src="../i/next.gif" name="topnext" width="60" height="15"></a></TD></TR></TABLE><table bgcolor="ffffff" cellspacing="0" cellpadding="2" border="0" height="400" width="100%"><tr valign="bottom"><a name="navskip"></a><td height="25" colspan="4"><img border="0" height="25" width="562" src="imagemaster/titlebar10.jpg" alt="第十章:MIDP API"></td></tr><tr><td bgcolor="ffffff" width="15"> </td><td bgcolor="ffffff" width="12"> </td><td valign="top" align="left" bgcolor="ffffff" width="*"><p><br x="7"><table cellspacing="0" cellpadding="0" border="0"><tr><td width="90%"><font size="4" face="Verdana, Arial, Helvetica"><b>HelloWorld MIDlet</b></font></td><td width="200" align="right"><nobr><font size="1" face="Verdana, Arial, Helvetica">第 5 页(共 6 页)</font></nobr></td></tr></table><br x="7"><br x="7"><font size="2" face="Verdana, Arial, Helvetica"><p>
与使用 KJava HelloWorld
应用程序一样,这个 MIDlet
也会在 MIDP 设备的显示屏上显示 "Hello World!" 和 Exit 按钮,按下该按钮会终止应用程序。
</p><p>HelloWorld.java
文件以下面的代码行开始,这些代码行导入稍后会在 <code style="font-family: Courier New, Courier, monospace; font-size: 12">HelloWorld</code> 类中使用的类:
</p><pre><code style="font-family: Courier New, Courier, monospace; font-size: 12">
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
</code></pre><p>由于 <code style="font-family: Courier New, Courier, monospace; font-size: 12">HelloWorld</code>
类是一个 MIDP
应用程序,它扩展了 MIDlet。它也实现
<code style="font-family: Courier New, Courier, monospace; font-size: 12">CommandListener</code> 接口来处理事件:
</p><pre><code style="font-family: Courier New, Courier, monospace; font-size: 12">
public class HelloWorld extends MIDlet implements CommandListener
</code></pre><p>下面的方法是一个缺省构造程序,它创建一个新表单,在上面初始化控件,然后显示出来:
</p><pre><code style="font-family: Courier New, Courier, monospace; font-size: 12">
private Form form;
public HelloWorld()
{
// Create a new form on which to display our text
form = new Form("Test App");
// Add the text "Hello World!" to the form
form.append("Hello World!");
// Add a command button labeled "Exit"
form.addCommand( new Command( "Exit", Command.EXIT, 1 ) );
// Register this object as a commandListener
form.setCommandListener( this );
}
</code></pre><p>调用 <code style="font-family: Courier New, Courier, monospace; font-size: 12">startApp()</code> 方法启动应用程序与小应用程序的启动方法很象。在 MIDle 的一次执行中它可能会被调用多次。如果 MIDlet 暂停,<code style="font-family: Courier New, Courier, monospace; font-size: 12">pauseApp()</code> 将会被调用。要重新启动 MIDlet,需调用 <code style="font-family: Courier New, Courier, monospace; font-size: 12">startApp()</code>。仅须执行一次的主初始化代码应该放置在构造程序中:
</p><pre><code style="font-family: Courier New, Courier, monospace; font-size: 12">
public void startApp()
{
// Get a reference to the display, and show the form
Display display = Display.getDisplay(this);
display.setCurrent( form );
}
</code></pre><p>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">pauseApp()</code> 被调用使得 MIDlet 处于暂停状态。在此应用程序中,当进入暂停状态时,我们没执行任何操作;但是我们仍然需要在 MIDlet 中实现 <code style="font-family: Courier New, Courier, monospace; font-size: 12">pauseApp</code> 方法,因为它是父 MIDlet 类中的抽象方法。
</p><pre><code style="font-family: Courier New, Courier, monospace; font-size: 12">
public void pauseApp() { }
</code></pre><p>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">destroyApp()</code> 被调用,破坏了 MIDlet 并使其处于销毁状态。在此应用程序中,我们通过将引用设为 <code style="font-family: Courier New, Courier, monospace; font-size: 12">null</code>,释放了对表单的引用。
</p><pre><code style="font-family: Courier New, Courier, monospace; font-size: 12">
public void destroyApp(boolean unconditional)
{
form = null;
}
</code></pre><p>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">commandAction()</code> 方法是事件处理程序,被请求实现 <code style="font-family: Courier New, Courier, monospace; font-size: 12">CommandListener</code> 接口。目前,它破坏了应用程序并通知应用程序管理软件 MIDlet 已经完成。
</p><pre><code style="font-family: Courier New, Courier, monospace; font-size: 12">
public void commandAction(Command c, Displayable d)
{
// Destroy this MIDlet
destroyApp(true);
// Notify the application management software that this MIDlet
// has entered the destroyed state
notifyDestroyed();
}
</code></pre><br x="7"></font></td></tr></table><TABLE border="0" cellpadding="0" cellspacing="0" width="100%"><TR><TD background="../i/sw-gold.gif"><a border="0" href="index.shtml" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜单" border="0" src="../i/main.gif" name="bottommain" width="77" height="15"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index10.shtml"><img alt="章节菜单" border="0" src="../i/section.gif" name="bottomsection" width="98" height="15"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-j2me-12-3.shtml"><img alt="给出此教程的反馈意见" border="0" src="../i/feedback.gif" name="bottomfeedback" width="80" height="15"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img src="../i/c.gif" width="1" height="1"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topprevious'); iOver('bottomprevious'); self.status=previousblurb; return true;" onMouseOut="iOut('topprevious'); iOut('bottomprevious'); self.status=''; return true;" href="j-j2me-10-4.shtml"><img alt="上一屏" border="0" src="../i/previous.gif" name="bottomprevious" width="77" height="15"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-j2me-10-6.shtml"><img alt="下一屏" border="0" src="../i/next.gif" name="bottomnext" width="60" height="15"></a></TD></TR><TR><TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD></TR></TABLE><!--#include virtual="/developerWorks/inc/tuto-inc/tuto-footer.inc" --></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -