j2me课程设计说明书.docx
- 文档编号:28818286
- 上传时间:2023-07-19
- 格式:DOCX
- 页数:30
- 大小:434.86KB
j2me课程设计说明书.docx
《j2me课程设计说明书.docx》由会员分享,可在线阅读,更多相关《j2me课程设计说明书.docx(30页珍藏版)》请在冰豆网上搜索。
j2me课程设计说明书
课程设计说明书
名称_手持移动设备应用开发课程设计_
2011年12月27日至2011年12月29日共1周
院系______计算机工程系________
班级______07计算机应用(对口)______
姓名_______________
系主任____________________
教研室主任____________________
指导教师__
一、课程设计目的与要求
1.1设计目的
本课程设计课题是为配合《手持设备移动应用开发》课程教学所开设的实践性环节课程,旨在要求学生综合掌握Eclipse集成开发环境中基于J2ME技术的手持移动设备应用开发的完整过程,特别是掌握用户界面设计、基于Socket网络连接的程序设计方法,进一步熟悉WTK文档的使用,强化学生面向职业应用的综合程序设计能力。
1.2设计要求
(一)掌握Eclipse集成开发环境中基于J2ME技术的手持移动设备应用开发的完整开发流程;
(二)掌握课程设计课题的工作原理,完成系统分析及相应的程序设计工作,包括:
(1)指定服务器端、客户端及通信端口号功能的主界面程序设计;
(2)服务器端界面程序设计、服务器端读取客户发送信息程线程设计;
(3)客户端界面程序设计、客户端发送/接收信息线程设计。
(三)完成课程设计说明书编写。
1.3设计内容简介
本课程设计参照WTK文档实现一个基于Socket网络编程协议的智能手机J2ME网络通信应用程序,该系统同时包含服务器和客户端功能,可以根据通信需要启用服务器端功能或客户端功能,可以指定通信端口号,客户端与服务器之间的通信由独立的线程完成。
二、开发环境与系统流程
2.1.开发平台构建
2.1.1环境简介
Sun公司为了使Java语言进入嵌入式系统和消费类电子产品领域,在Java2中推出了J2ME,并提供了一套完整的J2ME开发工具,可以在PC机上完成J2ME应用程序开发、调试、预校验和运行等工作。
使用J2ME的CLDC/MIDP编写的Java程序被称为MIDlet。
为了开发MIDlet应用程序,选用Windows操作系统平台。
在此平台上安装相关软件:
JDK、SunWTK、Eclipse、EclipseME插件、WTK插件。
2.1.2安装步骤
EclipseME作为Eclipse一个插件,致力于帮助开发者开发J2ME应用程序。
EclipseME并不为开发者提供无线设备模拟器,而将各手机厂商的实用模拟器紧密连接到Eclipse开发环境中,为开发者提供一种无缝统一的集成开发环境。
在http:
//www.eclipse.org/downloads/index.php上下载Eclipse。
在Toolkit。
安装步骤:
●安装jdk-7u1-windows-i586.exe(设置好系统环境变量)
●安装WTK2.5正式版sun_java_wireless_toolkit-2.5.2_01-win.exe
●解压安装eclipse-SDK-3.7.1-win32.zip
●加载eclipseme.feature_1.7.9_site.zip插件和,具体步骤如下:
通过菜单Window->Preferences的J2ME选项中addwirelesstoolkit整合eclipse和WTK。
●加载WTK插件
●安装Eclipse简体中文语言包
2.2.系统流程图
服务器端客户端
三、详细设计与分析
3.1主界面程序设计
3.1.1SocketMIDlet功能简介
主界面定义了相关端口号,类型名与显示功能,指定服务器端、客户端及通信端口号的设计。
3.1.2界面截图
3.1.3界面关键代码和描述
该程序段实现了主界面显示效果的功能,主要程序段如下:
publicSocketMIDlet(){
display=Display.getDisplay(this);//获得系统屏幕
f=newForm("SocketDemo");//创建界面的标题
cg=newChoiceGroup("Pleaseselectpeer",Choice.EXCLUSIVE,names,null);//创建两个单选按钮
f.append(cg);
portField=newTextField("Portnumber:
",String.valueOf(DEFAULT_PORT),6,TextField.NUMERIC);//创建一个不超过6个字符的文本框对象
f.append(portField);
f.addCommand(exitCommand);
f.addCommand(startCommand);//把命令按钮添加到Form窗口中
f.setCommandListener(this);//为当前窗口设置监听器
display.setCurrent(f);
}
3.2服务器端界面程序设计
3.2.1Server功能简介
服务器端界面程序设计、服务器端读取客户发送信息程线程设计。
3.2.2界面截图
3.2.3界面关键代码和描述
该程序段实现了服务器端界面显示的功能,主要程序段如下:
publicServer(SocketMIDletm,intp){
parent=m;
port=p;
display=Display.getDisplay(parent);
f=newForm("SocketServer");
si=newStringItem("Status:
","");
tf=newTextField("Send:
","",30,TextField.ANY);
f.append(si);
f.append(tf);
f.addCommand(exitCommand);
f.setCommandListener(this);
display.setCurrent(f);
}
3.3服务器端接收信息程序设计
3.3.1功能简介
打开两个模拟器,另外一个当作Server端,一个当作Client端,Client端向Server端发送“1231AD”。
3.3.2界面截图
3.3.3界面关键代码和描述
该程序段实现了服务器端发送/接收信息的功能,主要程序段如下:
publicvoidstart(){
Threadt=newThread(this);
t.start();
}//启动线程,防止网络堵塞
publicvoidrun(){
StringportString=String.valueOf(port);
try{
si.setText("Waitingforconnectiononport"+portString);
scn=(ServerSocketConnection)Connector.open("socket:
//:
"
+portString);
//等待连接
sc=(SocketConnection)scn.acceptAndOpen();
si.setText("Connectionaccepted");
is=sc.openInputStream();//打开输入流
os=sc.openOutputStream();//打开输出流
sender=newSender(os);
//AllowsendingofmessagesonlyafterSenderiscreated
f.addCommand(sendCommand);
while(true){
StringBuffersb=newStringBuffer();
intc=0;
while(((c=is.read())!
='\n')&&(c!
=-1)){//判断读取的字符
sb.append((char)c);
}
if(c==-1){
break;
}
si.setText("Messagereceived-"+sb.toString());
}
stop();
si.setText("Connectionisclosed");
f.removeCommand(sendCommand);
}catch(IOExceptionioe){
if(ioe.getMessage().equals("ServerSocketOpen")){
Alerta=newAlert("Server","Port"+portString
+"isalreadytaken.",null,AlertType.ERROR);
a.setTimeout(Alert.FOREVER);
a.setCommandListener(this);
display.setCurrent(a);
}else{
if(!
stop){
ioe.printStackTrace();
}
}
}catch(Exceptione){
e.printStackTrace();
}
}
publicvoidcommandAction(Commandc,Displayables){
if((c==sendCommand)&&!
parent.isPaused()){
sender.send(tf.getString());//发送数据
}
if((c==Alert.DISMISS_COMMAND)||(c==exitCommand)){
parent.notifyDestroyed();
parent.destroyApp(true);//退出
}
}
3.4客户端主界面设计
3.4.1功能简介
客户端界面程序设计、客户端发送/接收信息线程设计
3.4.2界面截图
3.4.3界面关键代码和描述
该程序段实现了客户端面界面显示的功能,主要程序段如下:
publicClient(SocketMIDletm,intp){
parent=m;
port=p;
display=Display.getDisplay(parent);
f=newForm("SocketClient");
si=newStringItem("Status:
","");
tf=newTextField("Send:
","",30,TextField.ANY);
f.append(si);
f.append(tf);
f.addCommand(exitCommand);
f.addCommand(sendCommand);
f.setCommandListener(this);
display.setCurrent(f);
}
3.5客户端发送/接收信息设计
3.5.1功能简介
服务器端接受到Client端发送的消息:
“1231AD”,并发送一个消息给Client端:
“GJ1JMAMJM”
3.5.2界面截图
3.5.3界面关键代码和描述
该程序段实现了客户端发送/接收信息的功能,主要程序段如下:
publicvoidstart(){
Threadt=newThread(this);
t.start();
}
publicvoidrun(){
StringportString=String.valueOf(port);
try{
sc=(SocketConnection)Connector.open("socket:
//localhost:
"
+portString);
si.setText("Connectedtoserveronport"+portString);
is=sc.openInputStream();
os=sc.openOutputStream();
//Startthethreadforsendingmessages-seeSender'smain
//commentforexplanation
sender=newSender(os);
//Loopforever,receivingdata
while(true){
StringBuffersb=newStringBuffer();
intc=0;
while(((c=is.read())!
='\n')&&(c!
=-1)){
sb.append((char)c);
}
if(c==-1){
break;
}
//显示发过来的信息
si.setText("Messagereceived-"+sb.toString());
}
stop();
si.setText("Connectionclosed");
f.removeCommand(sendCommand);
}catch(ConnectionNotFoundExceptioncnfe){
Alerta=newAlert("Client",
"PleaserunServerMIDletfirstonport"+portString,
null,AlertType.ERROR);
a.setTimeout(Alert.FOREVER);
a.setCommandListener(this);
display.setCurrent(a);
}catch(IOExceptionioe){
if(!
stop){
ioe.printStackTrace();
}
}catch(Exceptione){
e.printStackTrace();
}
}
四、课程设计总结
这次课程设计做的是客户端与服务器信息收发系统,系统分为练习和测试两部分,系统中采用的技术主要利用MyEclipse集成开发环境与WTK文档。
socket通信,基本原理就是创建socket,然后连接、监听、接受、发送、关闭等等。
在本次实践中,给我印象最为深刻的是通过运行继承于MIDlet的主程序看结果,再来分析程序的界面设计和其中的通信过程,遇到不懂的知识查阅WTK文档,或者从网上查查了很多资料,如ChoiceGroup类的getSelectedIndex()方法Choice.EXCLUSIVE的意义等。
对低级界面的移动应用软件的设计使我更好的掌握Java编程和J2ME程序调试在基本技能。
更让我掌握设计移动应用软件的基本思路和方法,提高运用J2ME解决实际问题的能力。
通过这次课程设计,我收获的不仅仅是课程上的知识得到实际应用,还有编程的基本习惯和开发系统时应注意的流程。
体现出自己单独设计的能力以及综合运用知识的能力,体会了学以致用、突出自己劳动成果的喜悦心情,从中发现自己平时学习的不足和薄弱环节,从而加以弥补。
在此感谢我们的贾老师,老师严谨细致、一丝不苟的作风一直是我工作、学习中的榜样;老师循循善诱的教导和不拘一格的思路给予我无尽的启迪;这次课程设计的每个细节和每个数据,都离不开老师您的细心指导。
而您开朗的个性和宽容的态度,帮助我能够很顺利的完成了这次课程设计。
五、参考文献
[1]张光兰.基于J2ME的手机日志工作系统的设计和实现.成都信息工程学院,2007.
[2]高明娟.基于J2ME手机考试系统的设计与实现.成都信息工程学院,2007.
附录(源程序代码)
SocketMIDlet
packagesocket;
importjavax.microedition.lcdui.Choice;
importjavax.microedition.lcdui.ChoiceGroup;
importjavax.microedition.lcdui.Command;
importjavax.microedition.lcdui.CommandListener;
importjavax.microedition.lcdui.Display;
importjavax.microedition.lcdui.Displayable;
importjavax.microedition.lcdui.Form;
importjavax.microedition.lcdui.TextField;
importjavax.microedition.midlet.MIDlet;
publicclassSocketMIDletextendsMIDletimplementsCommandListener{
privatestaticfinalintDEFAULT_PORT=5000;
privatestaticfinalStringSERVER="Server";
privatestaticfinalStringCLIENT="Client";
privatestaticfinalString[]names={SERVER,CLIENT};
privatestaticDisplaydisplay;
privateFormf;
privateChoiceGroupcg;
privatebooleanisPaused;
privateTextFieldportField;
privateServerserver;
privateClientclient;
privateCommandexitCommand=newCommand("Exit",Command.EXIT,1);
privateCommandstartCommand=newCommand("Start",Command.ITEM,1);
publicSocketMIDlet(){
display=Display.getDisplay(this);
f=newForm("SocketDemo");
cg=newChoiceGroup("Pleaseselectpeer",Choice.EXCLUSIVE,names,
null);
f.append(cg);
portField=newTextField("Portnumber:
",String.valueOf(DEFAULT_PORT),
6,TextField.NUMERIC);
f.append(portField);
f.addCommand(exitCommand);
f.addCommand(startCommand);
f.setCommandListener(this);
display.setCurrent(f);
}
publicbooleanisPaused(){
returnisPaused;
}
publicvoidstartApp(){
isPaused=false;
}
publicvoidpauseApp(){
isPaused=true;
}
publicvoiddestroyApp(booleanunconditional){
if(server!
=null){
server.stop();
}
if(client!
=null){
client.stop();
}
}
publicvoidcommandAction(Commandc,Displayables){
if(c==exitCommand){
destroyApp(true);
notifyDestroyed();
}elseif(c==startCommand){
Stringname=cg.getString(cg.getSelectedIndex());
intport=Integer.parseInt(portField.getString());
if(name.equals(SERVER)){
server=newServer(this,port);
server.start();
}else{
client=newClient(this,port);
client.start();
}
}
}
}
Client
packagesocket;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importjavax.microedition.io.ConnectionNotFoundException;
importjavax.microedition.io.Connector;
importjavax.microedition.io.SocketConnection;
importjavax.microedition.lcdui.Alert;
importjavax.microedition.lcdui.AlertType;
importjavax.microedition.lcdui.Command;
importjavax.microedition.lcdui.CommandListener;
importjavax.microedition.lcdui.Display;
importjavax.microedition.lcdui.Displayable;
importjavax.microedition.lcdui.Form;
importjavax.microedition.lcdui.StringItem;
importjavax.microedition.lcdui.TextField;
publicclassClientimplementsRunnable,CommandListener{
privateSocketMIDletparent;
privateDisplaydisplay;
privat
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- j2me 课程设计 说明书
![提示](https://static.bdocx.com/images/bang_tan.gif)