上海电力学院J2me实验报告Word格式.docx
《上海电力学院J2me实验报告Word格式.docx》由会员分享,可在线阅读,更多相关《上海电力学院J2me实验报告Word格式.docx(14页珍藏版)》请在冰豆网上搜索。
![上海电力学院J2me实验报告Word格式.docx](https://file1.bdocx.com/fileroot1/2022-11/16/719cbe17-7da5-4eba-ab96-22bf2e021d46/719cbe17-7da5-4eba-ab96-22bf2e021d461.gif)
1【实验目的】
实验和了解eclipse下j2me用户界面实现方法。
2【实验内容】
●使用Textbox类创建程序,在屏幕左上角显示一组文字“一个普通的Textbox”,最大字符容量是40.点击右下角菜单项,可以弹出从上到下五个菜单选项,分别是“查看最大空间”,“查看当前位置”,“删除最末字符”,“插入”和“替换”。
查看最大空间指的是获取Textbox字符容量,查看当前位置是获取光标当前位置,每次点击“插入”后在Textbox第四个字符位置中插入“插入实验”字符,每次点击“替换”后将Textbox中全部字符替换成“替换实验”。
每次点击菜单项操作均在控制台要打印出对应的程序结果。
●使用List类创建程序。
List的标题是“你最爱吃的水果”,屏幕中List列表中有“苹果”,“桔子”和“香蕉”。
屏幕左下角有“退出”按键,右下角有“菜单”项,菜单项中有两个菜单选项,从上到下是“确定”和“删除该选项”。
用户可以在“苹果”,“桔子”和“香蕉”中选择,点击“确定”后控制台打印出选择的结果,点击“删除该选项”则删除对应项。
3【实验步骤】
(含源码及其必要的说明文字)
importjavax.microedition.lcdui.Command;
importjavax.microedition.lcdui.CommandListener;
importjavax.microedition.lcdui.Display;
importjavax.microedition.lcdui.Displayable;
importjavax.microedition.lcdui.TextBox;
importjavax.microedition.midlet.MIDlet;
importjavax.microedition.midlet.MIDletStateChangeException;
publicclassertextendsMIDletimplementsCommandListener{
TextBoxtextBox,cur;
Commandcheckmax,checknow,deletelast,sub,add;
publicert(){
//TODOAuto-generatedconstructorstub
}
protectedvoiddestroyApp(booleanarg0)throwsMIDletStateChangeException{
//TODOAuto-generatedmethodstub
protectedvoidpauseApp(){
protectedvoidstartApp()throwsMIDletStateChangeException{
checkmax=newCommand("
查看最大空间"
Command.SCREEN,0);
checknow=newCommand("
查看当前位置"
deletelast=newCommand("
删除最末字符"
sub=newCommand("
替换"
add=newCommand("
插入"
textBox=newTextBox("
一个普通的textbox"
"
插入实验"
20,0);
cur=newTextBox("
"
cur.addCommand(checkmax);
cur.addCommand(checknow);
cur.addCommand(deletelast);
cur.addCommand(sub);
cur.addCommand(add);
textBox.addCommand(checkmax);
textBox.addCommand(checknow);
textBox.addCommand(deletelast);
textBox.addCommand(sub);
textBox.addCommand(add);
textBox.setCommandListener((CommandListener)this);
Display.getDisplay(this).setCurrent(textBox);
publicvoidcommandAction(Commandcommand,Displayablescreen){
if(command==checkmax){
Stringr="
最大容量为40,当前容量为:
;
Stringe=r+textBox.getString().toString();
cur=newTextBox("
e,20,0);
cur.setCommandListener((CommandListener)this);
Display.getDisplay(this).setCurrent(cur);
}
if(command==checknow){
inti=textBox.getCaretPosition();
textBox.setString(i+"
);
if(command==deletelast){
Stringi=textBox.getString().substring(0,textBox.getString().length()-1);
i,20,0);
cur.setCommandListener((CommandListener)this);
Display.getDisplay(this).setCurrent(cur);
if(command==sub){
Stringg="
替换实验"
g,20,0);
if(command==add){
textBox.getString()+g,20,0);
}
4【实验环境】
Eclipse3.3
5【实验结果】
(含界面截图及其必要的说明文字)
importjavax.microedition.lcdui.Alert;
importjavax.microedition.lcdui.List;
publicclassListDemoextendsMIDlet{
privateDisplaydisplay;
privateintmode=List.IMPLICIT;
/*privateCommandexitCommand=newCommand("
确定"
Command.SCREEN,2);
privateCommanddeleteCommand=newCommand("
删除该选项"
Command.OK,1);
privateCommandnextCommand=newCommand("
Next"
*/
privateCommandokCommand=newCommand("
删除选中项"
publicListDemo(){
exitMIDlet();
if(display==null){
initMIDlet();
privatevoidinitMIDlet(){
display=Display.getDisplay(this);
display.setCurrent(newSampleList(mode));
publicvoidexitMIDlet(){
notifyDestroyed();
publicstaticfinalString[]items={
"
苹果"
橘子"
香蕉"
};
classSampleListextendsListimplementsCommandListener{
privateintmode;
SampleList(intmode){
super("
mode,items,null);
/*addCommand(exitCommand);
addCommand(selectCo