By zieckey ()
本文在 Nokia 6120C 手机上测试成功.该手机是S60第三版操作系统.
1、新建J2ME项目工程:
New->Project->J2ME->J2ME Midlet Suite->Next
输入工程名字:FirstJ2ME
点击Finish.
输入以下代码:
/* * @(#)AlertsMIDlet.java */
package zieckey.j2me.study;
import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; 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.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException;
/** * 该程序用来学习Alert控件的用法。 不同的Alert类型会发出不同的声音。 * * @author Zieckey * @version 07/01/19 */ public class AlertsMIDlet extends MIDlet implements CommandListener { private Display display; private TextBox textbox;
private Command exitCommand; private Command alarmCommand; private Command confirmCommand; private Command errorCommand; private Command infoCommand; private Command warningCommand;
private Alert alarmAlert; private Alert confirmAlert; private Alert errorAlert; private Alert infoAlert; private Alert warningAlert;
/** * 构造函数 */ public AlertsMIDlet( ) { display = Display.getDisplay( this );// 提取系统设备的Display对象
exitCommand = new Command( "Exit", Command.EXIT, 2 );
alarmCommand = new Command( "Alarm", Command.SCREEN, 2 );// 对该菜单的相应应用到当前整个屏幕
confirmCommand = new Command( "Confirm", Command.SCREEN, 2 ); errorCommand = new Command( "Error", Command.SCREEN, 2 ); infoCommand = new Command( "Information", Command.SCREEN, 2 ); warningCommand = new Command( "Warning", Command.SCREEN, 2 );
textbox = new TextBox( "Main UI", "Please click the soft keys to choose a Alert to run....", 256, 0 ); textbox.addCommand( alarmCommand ); textbox.addCommand( confirmCommand ); textbox.addCommand( errorCommand ); textbox.addCommand( infoCommand ); textbox.addCommand( warningCommand ); textbox.addCommand( exitCommand );
textbox.setCommandListener( this );
alarmAlert = new Alert( "Alarm", "This is an \"ALARM\" alert! ", null, AlertType.ALARM ); confirmAlert = new Alert( "Confirmation", "This is an \"CONFIRMATION\" alert! ", null, AlertType.CONFIRMATION ); errorAlert = new Alert( "Error", "This is an \"ERROR\" alert! ", null, AlertType.ERROR ); infoAlert = new Alert( "Info", "This is an \"INFORMATION\" alert! ", null, AlertType.INFO ); warningAlert = new Alert( "Warning", "This is an \"WARNING\" alert! ", null, AlertType.WARNING );
alarmAlert.setTimeout( 3000 ); confirmAlert.setTimeout( 3000 ); errorAlert.setTimeout( 3000 ); infoAlert.setTimeout( 3000 ); warningAlert.setTimeout( 3000 );
display.setCurrent( textbox ); }
protected void startApp() throws MIDletStateChangeException {
}
protected void destroyApp( boolean arg0 ) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
public void commandAction( Command cmd, Displayable arg1 ) { if ( cmd == exitCommand ) { try { destroyApp( true ); } catch ( MIDletStateChangeException e ) { e.printStackTrace( ); } notifyDestroyed( ); } else if ( cmd == infoCommand ) { display.setCurrent( infoAlert ); } else if ( cmd == errorCommand ) { display.setCurrent( errorAlert ); } else if ( cmd == alarmCommand ) { display.setCurrent( alarmAlert ); } else if ( cmd == confirmCommand ) { display.setCurrent( confirmAlert ); } else if ( cmd == warningCommand ) { display.setCurrent( warningAlert ); } } }
|
可以先在电脑上模拟测试下,程序的正确性。
2、打包
在工程名“FirstJ2ME”上右击->J2ME->Create Package
此时会在工程目录下的“deployed”文件夹下生成两个文件:FirstJ2ME.jad,FirstJ2ME.jar
这两个文件就是我们下面下载到手机上要用的文件。
在下载前要对FirstJ2ME.jad文件修改下,Eclipse打包后的FirstJ2ME.jad文件内容如下:
MIDlet-Jar-Size: 2152 MIDlet-Jar-URL: FirstJ2ME.jar MIDlet-Name: FirstJ2ME Midlet Suite MIDlet-Vendor: Midlet Suite Vendor MIDlet-Version: 1.0.0 MicroEdition-Configuration: CLDC-1.1 MicroEdition-Profile: MIDP-2.0
|
在第一行插入如下一行内容:
MIDlet-1: FirstJ2ME, , zieckey.j2me.study.AlertsMIDlet
|
最后FirstJ2ME.jad文件内容如下:
MIDlet-1: FirstJ2ME, , zieckey.j2me.study.AlertsMIDlet MIDlet-Jar-Size: 2152 MIDlet-Jar-URL: FirstJ2ME.jar MIDlet-Name: FirstJ2ME Midlet Suite MIDlet-Vendor: Midlet Suite Vendor MIDlet-Version: 1.0.0 MicroEdition-Configuration: CLDC-1.1 MicroEdition-Profile: MIDP-2.0
|
插入的那一行内容解释如下:
MIDlet-1: 程序在手机上显示的名字, 手机上显示的程序图标, 类名(注意包括包名)
3、下载
下载可以有很多种方式,最简单的就是通过数据线把手机和电脑连接起来。另外还有红外、蓝牙、OTA等方式。把上述两个文件FirstJ2ME.jad,FirstJ2ME.jar下载到手机上。
4、安装和运行
在手机上安装刚下载的程序,注意安装时选择 jad 文件也就是FirstJ2ME.jad文件安装,然后运行。
看看效果如何?
阅读(3055) | 评论(0) | 转发(0) |