// Import of API classes
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
//A first MIDlet with simple text and a few commands.
public class PhoneCalendar extends MIDlet
implements CommandListener, ItemStateListener {
//The commands
private Command exitCommand;
//The display for this MIDlet
private Display display;
// Display items e.g Form and DateField
Form displayForm;
DateField date;
public PhoneCalendar() {
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.SCREEN, 1);
date = new DateField("Select to date", DateField.DATE);
}
// Start the MIDlet by creating the Form and
// associating the exit command and listener.
public void startApp() {
displayForm = new Form("Quick Calendar");
displayForm.append(date);
displayForm.addCommand(exitCommand);
displayForm.setCommandListener(this);
displayForm.setItemStateListener(this);
display.setCurrent(displayForm);
}
public void itemStateChanged(Item item)
{
// Get the values from changed item
}
// Pause is a no-op when there is no background
// activities or record stores to be closed.
public void pauseApp() { }
// Destroy must cleanup everything not handled
// by the garbage collector.
public void destroyApp (boolean unconditional) { }
// Respond to commands. Here we are only implementing
// the exit command. In the exit command, cleanup and
// notify that the MIDlet has been destroyed.
public void commandAction (
Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
}
如上定义的 PhoneCalendar MIDlet 继承了 ItemListener 和 CommandListener。它使 MIDlet 具备了跟踪屏幕上的条目变化和对用户命令作出响应的功能。由此应用程序创建的用户界面从为电话屏幕定义一个显示并附加上一个 Form 开始。该 Form 充当容器使用,可以保持一些用户界面项。commandAction() 函数在 J2ME 中执行命令处理程序,并且定义某个命令应执行的动作。
部署 J2ME
您可以从 Sun 下载一个仿真器,该仿真器允许您在台式机系统上测试 J2ME 应用程序。如果您宁愿避免所有的图形开销,则您也可以在命令行上部署 J2ME。
在仿真环境中进行部署
在仿真环境中部署和运行 J2ME 应用程序,要涉及到仿真器的安装和配置。J2ME Wireless Toolkit 提供了一个仿真环境,该环境用于在资源受限的设备上对 Java 应用程序进行的开发和部署。这里教您如何自己运行它:
安装 J2ME Wireless Toolkit(请参阅参考资料)。安装程序中将会用必要的说明指导您进行安装。为运行这些示例,请选择独立(standalone)模式。如果您想将它集成到 IDE,请选择集成(integrated)模式。
通过 KToolbar 的用户界面创建一个新工程。指定一个类名。
将第 2 步指定的类名放到 C:/[J2ME Installation directory]/apps/[Project Name]/src 目录。
编译这个工程。
从 J2ME Wireless Toolkit -> Default Device Selection 选择 DefaultGrayPhone 作为缺省设备。
运行这个工程。
该工具箱还提供有一个选项,用于把工程打包成一个 jar 文件和一个 jad 文件。双击 jad 文件将会部署 jar 文件所指定的应用程序。
在命令行上进行部署
这里也有一些可用的命令行选项。
1. 创建类文件:
C:/J2ME/apps/PhoneCalendar>
javac _ tmpclasses _ootclasspath
C:/J2ME/lib/midpapi.zip -classpath tmpclasses;
classes src/*.java
2. 创建清单文件 manifest.mf:
MIDlet-1: PhoneCalendar,
PhoneCalendar.png,
PhoneCalendar
MIDlet-Name: Phone Calendar
MIDlet-Vendor: Sun Microsystems
MIDlet-Version: 1.0
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-1.0
3. 创建 jar 文件:
C:/J2ME/apps/PhoneCalendar>jar cfm ./bin/
PhoneCalendar.jar
manifest.mf -C classes . _ res .
4. 创建 jad 文件:
MIDlet-1: PhoneCalendar,
PhoneCalendar.png,
PhoneCalendar
MIDlet-Jar-Size: 4490
MIDlet-Jar-URL:
F:/J2ME/apps/PhoneCalendar/bin/
PhoneCalendar.jar
MIDlet-Name: PhoneCalendar
MIDlet-Vendor: Sun Microsystems
MIDlet-Version: 1.0
5. 运行 jad 文件:
C:/J2ME/bin> emulator -Xdescriptor:C:/J2ME/apps/PhoneCalendar/bin/PhoneCalendar.jad