J2ME HELLOWORLD 小试牛刀

类别:Java 点击:0 评论:0 推荐:

T628降价了,买了一个玩玩,这个“鸡鸡”支持JAVA扩展,所以第一时间去http://developer.sonyericsson.com/site/global/home/p_home.jsp下载了最新的SDK(FILE SIZE:42M),下载的时候要注意,SDK说明中应该指出支持T628,eclipse和eclipseme是早就下好的,下面就该安装了:

1、安装j2se 1.4.2,并配置path和classpath
2、安装semc_j2me_sdk_2_1_5.exe,装完后我没有做任何配置(包括环境变量配置)
3、解压缩eclipse,然后把eclipseme解压缩到eclipse的plugins目录中
4、然后就配置eclipseme(如图1)
5、创建一个jeme工程,File/New/Project.../J2ME/J2ME Midlet Suite
6、在工程中添加Midlet,File/New/Other../J2ME/J2ME Midlet
7、编写代码
8、在Navigator的工程上点右键,执行右键菜单里的J2ME/Create Package
9、用模拟器(图2)或通过蓝牙传到手机上执行
 
(图1)

(图2)


代码如下:
----------------------------------------------------------
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;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/*
 * Created on 2005-1-10
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author libo
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Helloworld extends MIDlet implements CommandListener{
 private Display display;
 private Form mainForm;
 private StringItem stringItem;
 private Command exitCommand=new Command("Exit",Command.EXIT ,1);
 /**
  *
  */
 public Helloworld() {
  super();
  // TODO Auto-generated constructor stub
 }

 /* (non-Javadoc)
  * @see javax.microedition.midlet.MIDlet#startApp()
  */
 protected void startApp() throws MIDletStateChangeException {
  // TODO Auto-generated method stub
  initMIDlet();
  display.setCurrent(mainForm);
 }
 
 private void initMIDlet(){
  String text;
  display=Display.getDisplay(this);
  mainForm=new Form("关于");
  stringItem=new StringItem(null,null);
  text="这是我的第一个J2ME程序!";  
  stringItem.setText(text);
  mainForm.append(stringItem);
  mainForm.addCommand(exitCommand);
  mainForm.setCommandListener(this);
 } 

 /* (non-Javadoc)
  * @see javax.microedition.midlet.MIDlet#pauseApp()
  */
 protected void pauseApp() {
  // TODO Auto-generated method stub

 }

 /* (non-Javadoc)
  * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
  */
 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  // TODO Auto-generated method stub
  System.out.println("exit the application");
 }
 
 public void commandAction(Command cmd,Displayable display){
  if(cmd==exitCommand)
  {
   try{
    destroyApp(false);
    notifyDestroyed();
   }catch(MIDletStateChangeException e){
    e.printStackTrace();
   }
  }
 } 

}


-----------------------------------------
代码结束


提示:T628仅支持CLDC 1.0 和MIDP 1.0 ,在新建设工程时,平台选择Sony Ericsson J2ME SDK(WTK 1.0.4) Platform ,如果选择Sony Ericsson J2ME SDK(WTK 2.1) Platform 则,在模拟器上可以运行,但传到T628上会提示无效的应用程序。

本文地址:http://com.8s8s.com/it/it11931.htm