NotePad完整源代码

J2ME版本的记事本,类似于西门子C65自带的便签.

// ---------------------------------------------------
// ContentDataEntity.java
// ---------------------------------------------------
package src;
import java.io. * ;
public class ContentDataEntity
... {
publicStringtitle;
publicStringdate;
publicStringtext;


publicContentDataEntity(Stringtitle,Stringdate,Stringtext)
...{
this.title=title;
this.text=text;
this.date=date;

}


publicContentDataEntity()
...{}

publicbyte[]encode()
...{
byte[]bytData=null;
try
...{
ByteArrayOutputStreambos
=newByteArrayOutputStream();
DataOutputStreamdos
=newDataOutputStream(bos);
dos.writeUTF(title);
dos.writeUTF(text);
dos.writeUTF(date);
bytData
=bos.toByteArray();
dos.close();
bos.close();
}

catch(Exceptionerro)
...{}
returnbytData;
}


publicvoiddecode(byte[]data)
...{
try
...{
ByteArrayInputStreambis
=newByteArrayInputStream(data);
DataInputStreamdis
=newDataInputStream(bis);
title
=dis.readUTF();
text
=dis.readUTF();
date
=dis.readUTF();
bis.close();
dis.close();
}

catch(Exceptionerro)
...{}
}

}

// ------------------------------------------------------
// CreatNewTextScreen.java
// ------------------------------------------------------
package src;
import javax.microedition.lcdui. * ;
import javax.microedition.midlet. * ;
import java.util. * ;
import javax.microedition.rms. * ;
public class CreatNewTextScreen extends Form implements CommandListener
... {
privateDisplaydisplay;
privateNotePadMIDletMIDlet;
privateTextFieldtitleField;
privateTextFieldtextField;
privateDateFielddateField;
privateintrecordID;
privatebooleanfirstSave=true;

publicCreatNewTextScreen(Displaydisplay,NotePadMIDletMIDlet)
...{
super("新建记事");
this.display=display;
this.MIDlet=MIDlet;
titleField
=newTextField("标题:","",20,TextField.ANY);
dateField
=newDateField("日期",DateField.DATE);
dateField.setDate(
newDate());//设定时间为当前时间
textField=newTextField("正文","",140,TextField.ANY);
this.append(titleField);
this.append(dateField);
this.append(textField);
this.addCommand(newCommand("保存",Command.OK,0));
this.addCommand(newCommand("返回",Command.OK,0));
this.setCommandListener(this);
}


publicvoidcommandAction(Commandc,Displayables)
...{
Stringcmd
=c.getLabel();
if(cmd.equals("保存"))
...{
saveData();
}

elseif(cmd.equals("返回"))
...{
ListScreenlistScreen
=newListScreen(display,MIDlet);
display.setCurrent(listScreen);
}

}


publicvoidsaveData()
...{
Stringdbname
="NotePadDataBase";
RecordStorers
=RMSUtil.openRSAnyway(dbname);
try
...{
if(rs==null)
...{
Alertal
=newAlert("");
al.setType(AlertType.INFO);
al.setTimeout(
5000);
al.setString(
"打开数据库出错,请与我联系:norains@163.com");
this.addCommand(newCommand("返回",Command.BACK,0));
display.setCurrent(al);
}

else
...{
Calendarcal
=Calendar.getInstance();
cal.setTime(dateField.getDate());
StringstrDate
=Integer.toString(cal.get(cal.YEAR))+""+Integer.toString(cal.get(cal.MONTH))+""+Integer.toString(cal.get(cal.DAY_OF_MONTH))+"";
//StringstrTime=Integer.toString(cal.get(cal.HOUR_OF_DAY))+":"+Integer.toString(cal.get(cal.MILLISECOND))+":"+Integer.toString(cal.get(cal.SECOND));
ContentDataEntitywriteData=newContentDataEntity(titleField.getString(),strDate,textField.getString());
byte[]bytTemp=writeData.encode();
if(firstSave==true)
...{
recordID
=rs.addRecord(bytTemp,0,bytTemp.length);
firstSave
=false;
}

else
...{
rs.setRecord(recordID,bytTemp,
0,bytTemp.length);
}

//如果加入数据出错,则会抛出异常,下面这段代码不执行
Alertal=newAlert("");
al.setString(
"已经成功保存!");
al.setType(AlertType.INFO);
al.setTimeout(Alert.FOREVER);
display.setCurrent(al,
this);
}

rs.closeRecordStore();
}

catch(Exceptionerro)
...{
Alertal
=newAlert("");
al.setString(
"出现错误,无法保存!请与我联系:norains@163.com");
al.setType(AlertType.ERROR);
al.setTimeout(Alert.FOREVER);
display.setCurrent(al,
this);
}

}




}

// -----------------------------------------------------
// EditContentScreen.java
// -----------------------------------------------------
package src;

import java.util.Calendar;
import java.util.Date;

import javax.microedition.lcdui. * ;
import javax.microedition.rms. * ;

public class EditContentScreen extends Form implements CommandListener
... {
privateDisplaydisplay;
privateNotePadMIDletMIDlet;
privateTextFieldtitleField;
privateTextFieldtextField;
privateDateFielddateField;
privateStringtitle;
privateStringdate;
privateStringtext;
privateintrecordID;

publicEditContentScreen(Displaydisplay,NotePadMIDletMIDlet,intID)
...{
super("新建记事");
this.display=display;
this.MIDlet=MIDlet;
this.recordID=ID;
readRecordData();
titleField
=newTextField("标题",title,20,TextField.ANY);
dateField
=newDateField("修改日期",DateField.DATE);
dateField.setDate(
newDate());//设定时间为当前时间
textField=newTextField("正文",text,140,TextField.ANY);
this.append(titleField);
this.append(dateField);
this.append(textField);
this.addCommand(newCommand("保存",Command.OK,0));
this.addCommand(newCommand("返回",Command.OK,0));
this.setCommandListener(this);
}


publicvoidcommandAction(Commandc,Displayables)
...{
Stringcmd
=c.getLabel();
if(cmd.equals("保存"))
...{
saveData();
}

elseif(cmd.equals("返回"))
...{
ViewListScreenviewListScreen
=newViewListScreen(display,MIDlet);
display.setCurrent(viewListScreen);
}

}


privatevoidreadRecordData()
...{
Stringdbname
="NotePadDataBase";
RecordStorers
=RMSUtil.openRSExisted(dbname);
if(rs==null)
...{
this.append("打开数据库失败!可能尚未储存数据,请进行游戏后再尝试.如果依然出现错误,请与我联系:norains@163.com");
}

else
...{
try
...{
ContentDataEntityrecordData
=newContentDataEntity();
recordData.decode(rs.getRecord(recordID));
title
=recordData.title;
date
=recordData.date;
text
=recordData.text;
rs.closeRecordStore();
}

catch(Exceptionerro)
...{}
}

}


publicvoidsaveData()
...{
Stringdbname
="NotePadDataBase";
RecordStorers
=RMSUtil.openRSAnyway(dbname);
try
...{
if(rs==null)
...{
Alertal
=newAlert("");
al.setType(AlertType.INFO);
al.setTimeout(
5000);
al.setString(
"打开数据库出错,请与我联系:norains@163.com");
this.addCommand(newCommand("返回",Command.BACK,0));
display.setCurrent(al);
}

else
...{
Calendarcal
=Calendar.getInstance();
cal.setTime(dateField.getDate());
StringstrDate
=Integer.toString(cal.get(cal.YEAR))+""+Integer.toString(cal.get(cal.MONTH))+""+Integer.toString(cal.get(cal.DAY_OF_MONTH))+"";
//StringstrTime=Integer.toString(cal.get(cal.HOUR_OF_DAY))+":"+Integer.toString(cal.get(cal.MILLISECOND))+":"+Integer.toString(cal.get(cal.SECOND));
ContentDataEntitywriteData=newContentDataEntity(titleField.getString(),strDate,textField.getString());
byte[]bytTemp=writeData.encode();
rs.setRecord(recordID,bytTemp,
0,bytTemp.length);

//如果加入数据出错,则会抛出异常,下面这段代码不执行
Alertal=newAlert("");
al.setString(
"已经成功保存!");
al.setType(AlertType.INFO);
al.setTimeout(Alert.FOREVER);
display.setCurrent(al,
this);
}

rs.closeRecordStore();
}

catch(Exceptionerro)
...{
Alertal
=newAlert("");
al.setString(
"出现错误,无法保存!请与我联系:norains@163.com");
al.setType(AlertType.ERROR);
al.setTimeout(Alert.FOREVER);
display.setCurrent(al,
this);
}

}


}


// ---------------------------------------
// EditPasswordScreen.java
// ---------------------------------------
package src;

import javax.microedition.lcdui. * ;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;

public class EditPasswordScreen extends Form implements CommandListener
... {
privatebooleanpasswordExist=false;
privateDisplaydisplay;
privateStringoldPassword;
privateintrecordID;
privateTextFieldoldPasswordField;
privateTextFieldnewPasswordField1;
privateTextFieldnewPasswordField2;
privateDisplayablenextDisp;
publicEditPasswordScreen(Displaydisplay,DisplayablenextDisp)
...{
super("");
this.display=display;
this.nextDisp=nextDisp;
readPassword();
if(passwordExist==true)
...{
oldPasswordField
=newTextField("请输入原密码:","",20,TextField.PASSWORD);
newPasswordField1
=newTextField("请输入新密码:","",20,TextField.PASSWORD);;
newPasswordField2
=newTextField("请再次输入新密码:","",20,TextField.PASSWORD);;
this.append(oldPasswordField);
this.append(newPasswordField1);
this.append(newPasswordField2);
}

else
...{
newPasswordField1
=newTextField("请输入新密码:","",20,TextField.PASSWORD);;
newPasswordField2
=newTextField("请再次输入新密码:","",20,TextField.PASSWORD);;
this.append(newPasswordField1);
this.append(newPasswordField2);
}

this.addCommand(newCommand("提交",Command.OK,0));
this.addCommand(newCommand("返回",Command.OK,0));
this.setCommandListener(this);
}


publicvoidcommandAction(Commandc,Displayabledisp)
...{
Stringcmd
=c.getLabel();
if(cmd.equals("提交"))
...{
updataPassword();
}

elseif(cmd.equals("返回"))
...{
display.setCurrent(nextDisp);
}

}


//读取密码
publicvoidreadPassword()
...{
Stringdbname
="NotePadSettingDateBase";
RecordStorers
=RMSUtil.openRSAnyway(dbname);
try
...{
if(rs==null)
...{
this.append("打开数据库失败!请与我联系:norains@163.com");
}

else
...{
RecordEnumerationre
=rs.enumerateRecords(null,null,false);
if(re.numRecords()!=0)
...{
passwordExist
=true;
recordID
=re.nextRecordId();
SettingDataEntitysde
=newSettingDataEntity();
sde.decode(rs.getRecord(recordID));
oldPassword
=sde.password;
}

else
...{
passwordExist
=false;
}

}

rs.closeRecordStore();
}

catch(Exceptionerro)
...{}
}


//更新密码
publicvoidupdataPassword()
...{
Stringdbname
="NotePadSettingDateBase";
RecordStorers
=RMSUtil.openRSAnyway(dbname);
try
...{
if(rs==null)//无法打开数据库
...{
this.append("打开数据库失败!请与我联系:norains@163.com");
}

else//能够打开数据库
...{
Stringtmppasd1
=newPasswordField1.getString();
Stringtmppasd2
=newPasswordField2.getString();
SettingDataEntitysde
=newSettingDataEntity(true,newPasswordField1.getString());
byte[]outputData=sde.encode();
if(tmppasd1.equals(tmppasd2))//两次输入的新密码相同
...{
if(passwordExist==true)//原来存在密码
...{
if(oldPassword.equals(oldPasswordField.getString()))//输入的原密码和保存的密码相同
...{
rs.setRecord(recordID,outputData,
0,outputData.length);
Alertal
=newAlert("");
al.setString(
"成功更改密码!");
al.setTimeout(Alert.FOREVER);
display.setCurrent(al,nextDisp);
}

else//输入的密码和保存的密码不相同
...{
Alertal
=newAlert("");
al.setString(
"原密码不对!");
al.setTimeout(Alert.FOREVER);
al.setType(AlertType.ERROR);
display.setCurrent(al);
}

}

else//原来不存在密码
...{
recordID
=rs.addRecord(outputData,0,outputData.length);
Alertal
=newAlert("");
al.setString(
"成功设置密码!");
al.setTimeout(Alert.FOREVER);
display.setCurrent(al,nextDisp);
}

}

else//两次输入的新密码不同
...{
Alertal
=newAlert("");
al.setString(
"两次输入的新密码不相同!");
al.setTimeout(Alert.FOREVER);
al.setType(AlertType.ERROR);
display.setCurrent(al);
}

}

rs.closeRecordStore();
}

catch(Exceptionerro)
...{}
}

}

// --------------------------------------
// FlashScreen.java
// --------------------------------------
package src;
import javax.microedition.lcdui.game. * ;
import javax.microedition.lcdui. * ;
import java.util. * ;
public class FlashScreen extends GameCanvas implements Runnable
... {
privateLayerManagerlayerManager;
privateSpriteanimation;
privateDisplaydisplay;
privateNotePadMIDletMIDlet;
publicFlashScreen(Displaydisplay,NotePadMIDletm)
...{
super(false);
this.display=display;
MIDlet
=m;
this.setFullScreenMode(true);
layerManager
=newLayerManager();
animation
=createAnimation("/res/flash.png");
layerManager.append(animation);

start();
}


privateSpritecreateAnimation(Stringpic)
...{
Imageimg
=null;
try
...{
img
=Image.createImage(pic);
}

catch(Exceptionerro)
...{}
returnnewSprite(img,96,54);
}



booleanconti=true;
intrate=300;
publicvoidrun()
...{
longst=0;
longet=0;
Graphicsg
=getGraphics();
System.out.println(
"ret");
while(conti==true)
...{
st
=System.currentTimeMillis();
input();
render(g);
et
=System.currentTimeMillis();
if((et-st)<rate)
...{
try
...{
Thread.sleep(rate
-(et-st));
}

catch(Exceptionerro)
...{}
}

}

}


publicvoidinput()
...{
intkeyState=getKeyStates();
if(keyState!=0)
...{
stop();
TestPasswordScreenreadSettingScreen
=newTestPasswordScreen(display,MIDlet);
//display.setCurrent(readSettingScreen);
//在这里之所以不使用上面这句setCurrent是因为ReadSettingScreen内部构造函数有setCurrent语句;
//如果设定了反而令流程不正常.这应该是个BUG.不明白可以把上面注释划掉测试看看----在NotePadKeyDataBase没建立的时候

}

}


publicvoidrender(Graphicsg)
...{
g.setColor(
255,255,255);
g.fillRect(
0,0,getWidth(),getHeight());
animation.nextFrame();
layerManager.setViewWindow(
0,0,getWidth(),getHeight());
layerManager.paint(g,(getWidth()
-96)/2,(getHeight()-54)/2);
flushGraphics();
}


publicvoidstart()
...{
Threadt
=newThread(this);
t.start();
}


publicvoidstop()
...{
conti
=false;
}

}

//-----------------------------------------------------
//InfoScreen.java
//-----------------------------------------------------

package
src;
import javax.microedition.lcdui. * ;
public class InfoScreen extends Form implements CommandListener
... {
privateDisplaydisplay;
privateNotePadMIDletMIDlet;
publicInfoScreen(Displaydisplay,NotePadMIDletMIDlet)
...{
super("程序说明");
this.display=display;
this.MIDlet=MIDlet;
this.append("名称:掌上记事版本:0.1 联系:norains@163.com 说明:手机上编写记事的好帮手。兄弟们如果有什么好建议,欢迎和偶联系!");
this.addCommand(newCommand("返回",Command.BACK,0));
this.setCommandListener(this);
}

publicvoidcommandAction(Commandc,Displayabledisp)
...{
Stringcmd
=c.getLabel();
if(cmd.equals("返回"))
...{
ListScreenlistScreen
=newListScreen(display,MIDlet);
display.setCurrent(listScreen);
}

}

}

//-----------------------------------------------------
//ListScreen.java
//-----------------------------------------------------

packagesrc;
importjavax.microedition.lcdui.
* ;
public class ListScreenextendsListimplementsCommandListener
... {
privateDisplaydisplay;
privateNotePadMIDletMIDlet;
//传入TestLatencyMIDlet主要是为了能进行退出程序的操作.
publicListScreen(Displaydisplay,NotePadMIDletm)
...{
super(
"",Choice.IMPLICIT);
MIDlet
=m;
this.display=display;
Imageimg1,img2,img3,img4,img5;
img1
=img2=img3=img4=img5=null;
try
...{
img1
=Image.createImage("/res/1.PNG");
img2
=Image.createImage("/res/2.PNG");
img3
=Image.createImage("/res/3.PNG");
img4
=Image.createImage("/res/4.PNG");
img5
=Image.createImage("/res/5.PNG");
}

catch(Exceptionerro)
...{}
this.append("新建记事",img1);
this.append("浏览条目",img2);
this.append("系统设置",img3);
this.append("程序说明",img4);
this.append("退出程序",img5);
this.setCommandListener(this);
}


publicvoidcommandAction(Commandc,Displayables)
...{
if(c==List.SELECT_COMMAND)
...{
Listtmp
=(List)s;
intselected=tmp.getSelectedIndex();
Stringlist
=tmp.getString(selected);
if(list.equals("新建记事"))
...{
CreatNewTextScreennewTextScreen
=newCreatNewTextScreen(display,MIDlet);
display.setCurrent(newTextScreen);
}

elseif(list.equals("浏览条目"))
...{
ViewListScreenviewListScreen
=newViewListScreen(display,MIDlet);
display.setCurrent(viewListScreen);
}

elseif(list.equals("系统设置"))
...{
SettingScreensettingScreen
=newSettingScreen(display,MIDlet);
display.setCurrent(settingScreen);
}

elseif(list.equals("程序说明"))
...{
InfoScreeninfoScreen
=newInfoScreen(display,MIDlet);
display.setCurrent(infoScreen);
}

elseif(list.equals("退出程序"))
...{
MIDlet.notifyDestroyed();
}

}

}

}

//-----------------------------------------------------
// NotePadMIDlet.java
//-----------------------------------------------------

package
src;

import javax.microedition.midlet. * ;
import javax.microedition.lcdui. * ;

public class NotePadMIDlet extends MIDlet ... {

privateDisplaydisplay;
publicNotePadMIDlet()...{
super();
//TODO自动生成构造函数存根
display=Display.getDisplay(this);
FlashScreenflashScreen
=newFlashScreen(display,this);
display.setCurrent(flashScreen);
}


protectedvoidstartApp()throwsMIDletStateChangeException...{
//TODO自动生成方法存根

}


protectedvoidpauseApp()...{
//TODO自动生成方法存根

}


protectedvoiddestroyApp(booleanarg0)throwsMIDletStateChangeException...{
//TODO自动生成方法存根

}


}

//-----------------------------------------------------
// RMSUtil .java
//-----------------------------------------------------

package
src;
import javax.microedition.rms. * ;
// 数据库操作公共类
public class RMSUtil
... {
publicstaticRecordStoreopenRSAnyway(Stringrsname)
...{
RecordStorers
=null;
if(rsname.length()>32)
...{
returnnull;
}

else
...{
try
...{
rs
=RecordStore.openRecordStore(rsname,true);
returnrs;
}

catch(Exceptionerro)
...{
returnnull;
}

}

}



publicstaticRecordStoreopenRSExisted(Stringrsname)
...{
RecordStorers
=null;
if(rsname.length()>32)
...{
returnnull;
}

else
...{
try
...{
rs
=RecordStore.openRecordStore(rsname,false);
returnrs;
}

catch(Exceptionerro)
...{
returnnull;
}

}

}



publicstaticbooleandeletRS(Stringrsname)
...{
if(rsname.length()>32)
...{
returnfalse;
}

else
...{
try
...{
RecordStore.deleteRecordStore(rsname);
returntrue;
}

catch(Exceptionerro)
...{
returnfalse;
}

}

}



}


//----------------------------------------
// SettingDataEntity.H
//----------------------------------------
package src;
import java.io. * ;

public class SettingDataEntity
... {
publicbooleanusePassword;
publicStringpassword;

publicSettingDataEntity(booleanusePassword,Stringpassword)
...{
this.usePassword=usePassword;
this.password=password;
}


publicSettingDataEntity()
...{}

publicbyte[]encode()
...{
byte[]bytData=null;
try
...{
ByteArrayOutputStreambos
=newByteArrayOutputStream();
DataOutputStreamdos
=newDataOutputStream(bos);
dos.writeBoolean(usePassword);
dos.writeUTF(password);
bytData
=bos.toByteArray();
dos.close();
bos.close();
}

catch(Exceptionerro)
...{}
returnbytData;
}


publicvoiddecode(byte[]bytData)
...{
try
...{
ByteArrayInputStreambis
=newByteArrayInputStream(bytData);
DataInputStreamdis
=newDataInputStream(bis);
usePassword
=dis.readBoolean();
password
=dis.readUTF();
dis.close();
bis.close();
}

catch(Exceptionerro)
...{}
}


}

//-----------------------------------------------------
//SettingScreen .java
//-----------------------------------------------------

packagesrc;
importjavax.microedition.lcdui.
* ;
importjavax.microedition.rms.
* ;
public class SettingScreenextendsFormimplementsCommandListener
... {
privateDisplaydisplay;
privateNotePadMIDletMIDlet;
privateDisplayableme=this;//定义此变量是为了在程序中传值
privatebooleansettingDataExist;
privatebooleanrecordUsePassword;
privateChoiceGroupusePasswordCG;

publicSettingScreen(Displaydisplay,NotePadMIDletMIDlet)
...{
super(
"设置");
this.display=display;
this.MIDlet=MIDlet;
usePasswordCG
=newChoiceGroup("密码保护",ChoiceGroup.EXCLUSIVE);
usePasswordCG.append(
"",null);
usePasswordCG.append(
"",null);
readSettingData();
if(recordUsePassword==true)//
...{
usePasswordCG.setSelectedIndex(
0,true);
}

else
...{
usePasswordCG.setSelectedIndex(
1,true);
}

this.append(usePasswordCG);
PasswordItemsetPassword
=newPasswordItem();
this.append(setPassword);
this.addCommand(newCommand("保存",Command.OK,0));
this.addCommand(newCommand("返回",Command.BACK,0));
this.setCommandListener(this);

}


publicvoidcommandAction(Commandc,Displayabledisp)
...{
Stringcmd
=c.getLabel();
if(cmd.equals("保存"))
...{
saveSettingData();
}

elseif(cmd.equals("返回"))
...{
ListScreenls
=newListScreen(display,MIDlet);
display.setCurrent(ls);
}

}


privatevoidsaveSettingData()
...{
SettingDataEntitysde;
Stringdbname
="NotePadSettingDateBase";
RecordStorers
=RMSUtil.openRSAnyway(dbname);
if(rs==null)//不能打开数据库
...{
Alertal
=newAlert("");
al.setString(
"无法打开数据库!");
al.setTimeout(Alert.FOREVER);
display.setCurrent(al);
}

else//数据库顺利打开
...{
try
...{
RecordEnumerationre
=rs.enumerateRecords(null,null,false);
if(re.numRecords()!=0)//记录存在
...{
intid;
id
=re.nextRecordId();
sde
=newSettingDataEntity();
sde.decode(rs.getRecord(id));
//取出原来的数据,主要是因为密码在此画面没有,但要写进去
if(usePasswordCG.getSelectedIndex()==0)
...{
sde.usePassword
=true;
}

elseif(usePasswordCG.getSelectedIndex()==1)
...{
sde.usePassword
=false;
}

byte[]writeData=sde.encode();
rs.setRecord(id,writeData,
0,writeData.length);

Alertal
=newAlert("");
al.setString(
"设置已保存!");
al.setTimeout(Alert.FOREVER);
ListScreenls
=newListScreen(display,MIDlet);
display.setCurrent(al,ls);
}

else//记录不存在
...{
if(usePasswordCG.getSelectedIndex()==0)
...{
//密码不存在又要密码保护,则直接转到编辑密码画面
EditPasswordScreeneps=newEditPasswordScreen(display,me);

display.setCurrent(eps);
}

elseif(usePasswordCG.getSelectedIndex()==1)
...{
sde
=newSettingDataEntity();
sde.usePassword
=false;
sde.password
=null;
byte[]writeData=sde.encode();
rs.addRecord(writeData,
0,writeData.length);

Alertal
=newAlert("");
al.setString(
"设置已保存!");
al.setTimeout(Alert.FOREVER);
ListScreenls
=newListScreen(display,MIDlet);
display.setCurrent(al,ls);
}

}

rs.closeRecordStore();
}

catch(Exceptionerro)
...{}
}

}


privatevoidreadSettingData()
...{
SettingDataEntitysde;
Stringdbname
="NotePadSettingDateBase";
RecordStorers
=RMSUtil.openRSAnyway(dbname);
if(rs==null)//不能打开数据库
...{
Alertal
=newAlert("");
al.setString(
"无法打开数据库!");
al.setTimeout(Alert.FOREVER);
display.setCurrent(al);
}

else//数据库顺利打开
...{
try
...{
RecordEnumerationre
=rs.enumerateRecords(null,null,false);
if(re.numRecords()!=0)//记录存在
...{
intid;
id
=re.nextRecordId();
sde
=newSettingDataEntity();
sde.decode(rs.getRecord(id));
if(sde.usePassword=true)
...{
recordUsePassword
=true;
}

else
...{
recordUsePassword
=false;
}

}

else//记录不存在
...{
recordUsePassword
=false;
}

rs.closeRecordStore();
}

catch(Exceptionerro)
...{}
}

}


//定义这个类是为了方便进行按钮操作
publicclassPasswordItemextendsStringItemimplementsItemCommandListener
...{
publicPasswordItem()
...{
super(
"","设置或更改密码",Item.PLAIN);
this.setDefaultCommand(newCommand("更改密码",Command.OK,0));
this.setItemCommandListener(this);
}

publicvoidcommandAction(Commandc,Itemi)
...{
Stringcmd
=c.getLabel();
if(cmd.equals("更改密码"))
...{
//System.out.println("设置!");
EditPasswordScreeneps=newEditPasswordScreen(display,me);
display.setCurrent(eps);
}

}

}


}

//-----------------------------------------------------
//TestPasswordScreen .java
//-----------------------------------------------------

package
src;

import javax.microedition.rms. * ;
import javax.microedition.lcdui. * ;
public class TestPasswordScreen extends Form implements CommandListener
... {
privateDisplaydisplay;
privateNotePadMIDletMIDlet;
privateTextFieldpasswordField;
privateinttestNum=0;
privateStringrecordPassword;
privatebooleanusePassword;

publicTestPasswordScreen(Displaydisplay,NotePadMIDletMIDlet)
...{
super("");
this.display=display;
this.MIDlet=MIDlet;
readSetting();
if(usePassword==true)
...{
passwordField
=newTextField("请在下面方框输入密码:","",20,TextField.PASSWORD);
this.append(passwordField);
this.addCommand(newCommand("确定",Command.OK,0));
this.addCommand(newCommand("退出",Command.OK,0));
this.setCommandListener(this);
display.setCurrent(
this);
}

else
...{
ListScreenlistScreen
=newListScreen(display,MIDlet);
display.setCurrent(listScreen);
//System.out.println("KO");
}

}


publicvoidcommandAction(Commandc,Displayabledisp)
...{
Stringcmd
=c.getLabel();
if(cmd.equals("确定"))
...{
testpassword();
}

elseif(cmd.equals("退出"))
...{}
}


privatevoidreadSetting()
...{
Stringdbname
="NotePadSettingDateBase";
RecordStorers
=RMSUtil.openRSExisted(dbname);
try
...{
if(rs==null)
...{
usePassword
=false;
}

else
...{
intid;
SettingDataEntitysettingData
=newSettingDataEntity();
RecordEnumerationre
=rs.enumerateRecords(null,null,false);
id
=re.nextRecordId();
settingData.decode(rs.getRecord(id));
usePassword
=settingData.usePassword;
recordPassword
=settingData.password;
}

rs.closeRecordStore();
}

catch(Exceptionerro)
...{}
}


privatevoidtestpassword()
...{
if(recordPassword.equals(passwordField.getString()))
...{
ListScreenlistScreen
=newListScreen(display,MIDlet);
display.setCurrent(listScreen);
}

else
...{
testNum
++;
intnum=3-testNum;
if(testNum<3)
...{
Alertal
=newAlert("密码错误");
al.setString(
"您还有"+num+"次输入机会,超过此次数程序将自动关闭");
al.setTimeout(Alert.FOREVER);
passwordField.setString(
"");
display.setCurrent(al);
}

else
...{
MIDlet.notifyDestroyed();
}

}

}


}



//-----------------------------------------------------
//ViewContentScreen
.java
//-----------------------------------------------------

package
src;
importjavax.microedition.lcdui.*;
importjavax.microedition.rms.*;

publicclassViewContentScreenextendsFormimplementsCommandListener
...{
privateDisplaydisplay;
privateNotePadMIDletMIDlet;
privateintrecordID;
privateStringtitle;
privateStringdate;
privateStringtext;


publicViewContentScreen(Displaydisplay,NotePadMIDletMIDlet,intrecordID)
...{
super("");
this.display=display;
this.MIDlet=MIDlet;
this.recordID=recordID;
readRecordData();
this.append("标题:"+title+" 修改日期:"+date+" 内容:"+text);
this.addCommand(newCommand("返回",Command.BACK,0));
this.addCommand(newCommand("编辑",Command.OK,0));
this.setCommandListener(this);
}


privatevoidreadRecordData()
...{
Stringdbname
="NotePadDataBase";
RecordStorers
=RMSUtil.openRSExisted(dbname);
if(rs==null)
...{
this.append("打开数据库失败!可能尚未储存数据,请进行游戏后再尝试.如果依然出现错误,请与我联系:norains@163.com");
}

else
...{
try
...{
ContentDataEntityrecordData
=newContentDataEntity();
recordData.decode(rs.getRecord(recordID));
title
=recordData.title;
date
=recordData.date;
text
=recordData.text;
rs.closeRecordStore();
}

catch(Exceptionerro)
...{}
}

}


publicvoidcommandAction(Commandc,Displayabledisp)
...{
Stringcmd
=c.getLabel();
if(cmd.equals("返回"))
...{
ViewListScreenviewListScreen
=newViewListScreen(display,MIDlet);
display.setCurrent(viewListScreen);
}

elseif(cmd.equals("编辑"))
...{
EditContentScreeneditContentScreen
=newEditContentScreen(display,MIDlet,recordID);
display.setCurrent(editContentScreen);
}

}

}


//-----------------------------------------------------
//ViewListScreen
.java
//-----------------------------------------------------

package
src;
importjavax.microedition.lcdui.*;
importjavax.microedition.rms.RecordEnumeration;
importjavax.microedition.rms.RecordStore;
publicclassViewListScreenextendsFormimplementsCommandListener
...{
privateDisplaydisplay;
privateNotePadMIDletMIDlet;
privateStringList[]stringList;

publicViewListScreen(Displaydisplay,NotePadMIDletMIDlet)
...{
super("");
this.display=display;
this.MIDlet=MIDlet;
this.addCommand(newCommand("返回",Command.BACK,0));
this.setCommandListener(this);
creatList();
}


privatevoidcreatList()
...{
Stringdbname
="NotePadDataBase";
RecordStorers
=RMSUtil.openRSAnyway(dbname);
if(rs==null)
...{
this.append("打开数据库失败!可能尚未储存数据,请进行游戏后再尝试.如果依然出现错误,请与我联系:norains@163.com");
}

else
...{
intrecordID;
try
...{
ContentDataEntityrecordData
=newContentDataEntity();
RecordEnumerationre
=rs.enumerateRecords(null,null,false);
inti;
stringList
=newStringList[re.numRecords()];
if(re.numRecords()!=0)
...{
for(i=0;i<re.numRecords();i++)
...{
recordID
=re.nextRecordId();
recordData.decode(rs.getRecord(recordID));
stringList[i]
=newStringList(recordData.title,recordID);
this.append(stringList[i]);
//System.out.println(stringList[i].recordID);
}

}

else
...{
this.append("尚未存储记事!");
}

rs.closeRecordStore();
//RMSUtil.deletRS(dbname);
}

catch(Exceptionerro)
...{}
}

}


publicvoidcommandAction(Commandc,Displayabledisp)
...{
Stringcmd
=c.getLabel();
if(cmd.equals("返回"))
...{
ListScreenlistScreen
=newListScreen(display,MIDlet);
display.setCurrent(listScreen);
}

}


privateclassStringListextendsStringItemimplementsItemCommandListener
...{
publicStringtitle;
publicintrecordID;

publicStringList(Stringtitle,intrecordID)
...{
super("",title,Item.PLAIN);
this.title=title;
this.recordID=recordID;
this.setLayout(Item.LAYOUT_NEWLINE_AFTER);
this.setDefaultCommand(newCommand("查看",Command.OK,0));
this.addCommand(newCommand("编辑",Command.OK,1));
this.addCommand(newCommand("删除",Command.OK,2));
this.addCommand(newCommand("取消",Command.OK,3));
//this.addCommand(newCommand("返回",Command.OK,4));在主画面已经有一个"返回",这里可以不要
this.addCommand(newCommand("退出",Command.OK,6));
this.setItemCommandListener(this);
}


publicvoiddeleteRecord(intid)
...{
Stringdbname
="NotePadDataBase";
RecordStorers
=RMSUtil.openRSExisted(dbname);
try
...{
if(rs==null)
...{
Alertal
=newAlert("");
al.setString(
"出现错误,打开数据库!请与我联系:norains@163.com");
al.setType(AlertType.ERROR);
al.setTimeout(Alert.FOREVER);
display.setCurrent(al);
}

else
...{
rs.deleteRecord(id);
}

rs.closeRecordStore();
}

catch(Exceptionerro)
...{}
}


publicvoidcommandAction(Commandc,Itemi)
...{
Stringcmd
=c.getLabel();
StringListstringList
=(StringList)i;
if(cmd.equals("查看"))
...{
ViewContentScreenviewContentScreen
=newViewContentScreen(display,MIDlet,stringList.recordID);
display.setCurrent(viewContentScreen);
}

elseif(cmd.equals("编辑"))
...{
EditContentScreeneditContentScreen
=newEditContentScreen(display,MIDlet,stringList.recordID);
display.setCurrent(editContentScreen);
}

elseif(cmd.equals("删除"))
...{
deleteRecord(stringList.recordID);
ViewListScreenviewListScreen
=newViewListScreen(display,MIDlet);
display.setCurrent(viewListScreen);
}

elseif(cmd.equals("取消"))
...{
//不编写代码可退出选择菜单
}

elseif(cmd.equals("返回"))
...{
ListScreenlistScreen
=newListScreen(display,MIDlet);
display.setCurrent(listScreen);
}

elseif(cmd.equals("退出"))
...{
MIDlet.notifyDestroyed();
}

}

}



}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值