1、1第 12 章 J2ME 程序设计基础【1】安装并配置 J2ME 的运行环境。解答:安装步骤如下:1、 安装 JDK1.4;2、 安装 J2ME Wireless Toolkit2.2(WTK22);测试运行环境:使用 WTK 创建第一个 MIDlet 程序,步骤如下:打开开发环境:开始 程序 J2ME Wireless Toolkit 2.2 KToolbar,如下:(2)新建项目。项目名称:合法的变量名称就行;MIDlet 类名称:该类为程序运行入口类,如下所示:(3)编写程序。使用任意的 IDE,编写类 InfoTest.java(MIDlet 类名称,与新建项目时的名称相同) ,同时必
2、须保存在 Java 源文件目录下。如果有图片等资源文件,必须保存在应用程序源文件目录下。如下所示:2源程序如下:import javax.microedition.lcdui.*;import javax.microedition.midlet.*;public class InfoTest extends MIDlet private Display display;public InfoTest() display=Display.getDisplay(this);protected void startApp()Alert alert =new Alert(“手机信息测试“);alert
3、.setTimeout(Alert.FOREVER);String icon=“/zsm.jpg“; /从资源目录开始寻找tryImage image=Image.createImage(icon);alert.setImage(image);catch(java.io.IOException x)System.out.println(“出错了“);display.setCurrent(alert);protected void pauseApp()protected void destroyApp(boolean unconditional)(4)编译与运行3【2】设计一个公告显示程序。解答
4、:代码如下:import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;import javax.microedition.lcdui.Display;import javax.microedition.lcdui.TextBox;import javax.microedition.lcdui.TextField;import javax.microedition.lcdui.Ticker;public class ShowInfo extends MID
5、let private Display dp;private TextBox tb;public ShowInfo()super();tb = new TextBox(“信息公告“,“,200, TextField.ANY);protected void startApp() throws MIDletStateChangeException dp = Display.getDisplay(this);tb.setTicker(new Ticker(“今天全校停课,哈哈!“);tb.setString(“由于天气原因,全校停课一个月,回家种地去!“);dp.setCurrent(tb);pro
6、tected void pauseApp() protected void destroyApp(boolean b) throws MIDletStateChangeException 运行结果如下:4【3】应用记录管理系统 RMS 建立一个同学通讯录。解答:同学通讯录的要求如下:1、 每个联系人包括两项信息:姓名,电话号码;2、 能对联系人进行增加、删除、修改和查询操作;3、 通过一个 MIDlet 程序实现与用户的交互(实现界面) 。程序如下:TelBean.java:描述联系人实体,既是用于传值的 JavaBean,又提供该类对象与字节数组相互转换的方法,代码如下:import jav
7、a.io.*;public class TelBook private String name;private String tel;public String getName() return name;public void setName(String name) this.name = name;public String getTel() return tel;public void setTel(String tel) this.tel = tel;public byte toByteArray()byte data = null;ByteArrayOutputStream bou
8、t = new ByteArrayOutputStream();5DataOutputStream dout = new DataOutputStream(bout);try dout.writeUTF(this.name);dout.writeUTF(this.tel);data = bout.toByteArray();dout.close();bout.close(); catch (IOException e) e.printStackTrace();return data;public void initTelBean(byte rec)ByteArrayInputStream bi
9、n = new ByteArrayInputStream(rec);DataInputStream din = new DataInputStream(bin);try this.name = din.readUTF();this.tel = din.readUTF();din.close();bin.close(); catch (IOException e) e.printStackTrace();TelModel.java:操作 RecordStore,实现对电话本的增、删、查、改功能,代码如下:import javax.microedition.rms.RecordStore;impo
10、rt javax.microedition.rms.RecordStoreException;import javax.microedition.rms.RecordStoreNotOpenException;import java.io.*;public class TelModel private RecordStore rs = null;public TelModel() try rs = RecordStore.openRecordStore(“Pref“,true); catch (RecordStoreException e) e.printStackTrace();public
11、 int addRecord(TelBook mn)int re = -1;try re = rs.addRecord(mn.toByteArray(),0,mn.toByteArray().length); catch (RecordStoreException e) 6e.printStackTrace();return -1;return re;public TelBook getRecord(int recordID)TelBook note = new TelBook();try byte MyNoteBytes = rs.getRecord(recordID);note.initT
12、elBean(MyNoteBytes); catch (RecordStoreException e) e.printStackTrace();return note;public boolean setRecord(int recordID,TelBook mn)try byte temp = mn.toByteArray();rs.setRecord(recordID,temp,0,temp.length); catch (RecordStoreException e) e.printStackTrace();return false;return true;public boolean
13、deleteRecord(int recordID)try rs.deleteRecord(recordID); catch (RecordStoreException e) e.printStackTrace();return false;return true;public int getRecordCounts()try return rs.getNumRecords(); catch (RecordStoreNotOpenException e) e.printStackTrace();return -1;public void Close()try 7rs.closeRecordSt
14、ore(); catch (RecordStoreException e) e.printStackTrace(); TelView.java:用户界面,得到用户的输入并显示系统输出,代码如下:import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;import javax.microedition.lcdui.*;public class RecordMidlet extends MIDlet implements CommandListener p
15、rivate Form mForm;private Form note;private Display dp;private TelModel rs;private TextField mSubject,mContent,mID;private int currentID;private int status = -1;private Command CMD_ADD;private Command CMD_DEL;private Command CMD_EDIT;private Command CMD_EXIT;private Command CMD_INFO;private Command
16、CMD_OK;private Command CMD_CANCEL;public RecordMidlet()rs = new TelModel();note = new Form(“Store Book“);mSubject = new TextField(“姓名“,“,20,0);mContent = new TextField(“号码“,“,20,0);mID = new TextField(“记录号“,“,10,TextField.NUMERIC);CMD_ADD = new Command(“Add Record“,Command.ITEM,1);CMD_DEL = new Comm
17、and(“Delete Record“,Command.ITEM,1);CMD_EDIT = new Command(“Edit Record“,Command.ITEM,1);CMD_EXIT = new Command(“Exit“,Command.EXIT,1);CMD_INFO = new Command(“Recode Store Information“,Command.EXIT,1);CMD_OK = new Command(“Ok“,Command.OK,1);CMD_CANCEL = new Command(“Cancel“,Command.CANCEL,1);note.ad
18、dCommand(CMD_CANCEL);note.addCommand(CMD_OK);note.setCommandListener(this);8protected void startApp() throws MIDletStateChangeException dp = Display.getDisplay(this);if(mForm = null)mForm = new Form(“电话本“);mForm.addCommand(CMD_ADD);mForm.addCommand(CMD_DEL);mForm.addCommand(CMD_EDIT);mForm.addComman
19、d(CMD_EXIT);mForm.addCommand(CMD_INFO);mForm.setCommandListener(this);dp.setCurrent(mForm);protected void pauseApp() protected void destroyApp(boolean b) throws MIDletStateChangeException rs.Close();public void commandAction(Command command, Displayable displayable) if(command = CMD_EXIT)notifyDestr
20、oyed();else if(command = CMD_CANCEL)dp.setCurrent(mForm);else if(command = CMD_ADD)note.deleteAll();note.append(mSubject);mSubject.setString(“);note.append(mContent);mContent.setString(“);status = 0;dp.setCurrent(note);else if(command = CMD_EDIT)note.deleteAll();note.append(mID);status = 1;dp.setCur
21、rent(note);else if(command = CMD_DEL)note.deleteAll();note.append(mID);status = 3;dp.setCurrent(note);if(command = CMD_OK)Alert a = new Alert(“);9if(status = 0)TelBook my = new TelBook();my.setTel(mContent.getString();my.setName(mSubject.getString();int id = rs.addRecord(my);a.setString(“Add record
22、successfully!“ + id);a.setTimeout(3000);dp.setCurrent(a,mForm);else if(status = 1)currentID = Integer.parseInt(mID.getString();TelBook temp = rs.getRecord(currentID);mSubject.setString(temp.getName();mContent.setString(temp.getTel();note.deleteAll();note.append(mSubject);note.append(mContent);dp.set
23、Current(note);status = 2;return;else if(status = 2)TelBook temp = new TelBook();temp.setName(mSubject.getString();temp.setTel(mContent.getString();boolean re = rs.setRecord(currentID,temp);if(re)a.setString(“Edit record successfully“);elsea.setString(“Eidt record failure“);dp.setCurrent(a,mForm);sta
24、tus = -1;else if(status = 3)currentID = Integer.parseInt(mID.getString();boolean re = rs.deleteRecord(currentID);if(re)a.setString(“Delete record successfully“);elsea.setString(“Delete recode failure“);dp.setCurrent(a,mForm);if(command = CMD_INFO)String temp = “;10temp += “The current RecordStore have“ + rs.getRecordCounts() + “records“;StringItem ss = new StringItem(“RecordStore informationn“,temp);note.deleteAll();note.append(ss);dp.setCurrent(note);status = 4;运行结果如下:主界面: 新增界面:编辑界面: