ImageVerifierCode 换一换
格式:DOC , 页数:10 ,大小:262KB ,
资源ID:1211350      下载积分:10 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.docduoduo.com/d-1211350.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录   微博登录 

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(第12章j2me程序设计基础.doc)为本站会员(cjc2202537)主动上传,道客多多仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知道客多多(发送邮件至docduoduo@163.com或直接QQ联系客服),我们立即给予删除!

第12章j2me程序设计基础.doc

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;运行结果如下:主界面: 新增界面:编辑界面:

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报