J2ME版本的记事本,类似于西门子C65自带的便签.
//---------------------------------------------------
//ContentDataEntity.java
//---------------------------------------------------
package src;
import java.io.*;
public class ContentDataEntity

...{
public String title;
public String date;
public String text;

public ContentDataEntity(String title,String date,String text)

...{
this.title=title;
this.text=text;
this.date=date;
}
public ContentDataEntity()

...{}
public byte[] encode()

...{
byte[] bytData=null;
try

...{
ByteArrayOutputStream bos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(bos);
dos.writeUTF(title);
dos.writeUTF(text);
dos.writeUTF(date);
bytData=bos.toByteArray();
dos.close();
bos.close();
}
catch(Exception erro)

...{}
return bytData;
}
public void decode(byte[] data)

...{
try

...{
ByteArrayInputStream bis=new ByteArrayInputStream(data);
DataInputStream dis=new DataInputStream(bis);
title=dis.readUTF();
text=dis.readUTF();
date=dis.readUTF();
bis.close();
dis.close();
}
catch(Exception erro)

...{}
}
}

//------------------------------------------------------
//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

...{
private Display display;
private NotePadMIDlet MIDlet;
private TextField titleField;
private TextField textField;
private DateField dateField;
private int recordID;
private boolean firstSave=true;
public CreatNewTextScreen(Display display,NotePadMIDlet MIDlet)

...{
super("新建记事");
this.display=display;
this.MIDlet=MIDlet;
titleField=new TextField("标题:","",20,TextField.ANY);
dateField=new DateField("日期",DateField.DATE);
dateField.setDate(new Date());//设定时间为当前时间
textField=new TextField("正文","",140,TextField.ANY);
this.append(titleField);
this.append(dateField);
this.append(textField);
this.addCommand(new Command("保存",Command.OK,0));
this.addCommand(new Command("返回",Command.OK,0));
this.setCommandListener(this);
}
public void commandAction(Command c,Displayable s)

...{
String cmd=c.getLabel();
if(cmd.equals("保存"))

...{
saveData();
}
else if(cmd.equals("返回"))

...{
ListScreen listScreen=new ListScreen(display,MIDlet);
display.setCurrent(listScreen);
}
}
public void saveData()

...{
String dbname="NotePadDataBase";
RecordStore rs=RMSUtil.openRSAnyway(dbname);
try

...{
if(rs==null)

...{
Alert al=new Alert("");
al.setType(AlertType.INFO);
al.setTimeout(5000);
al.setString("打开数据库出错,请与我联系:norains@163.com");
this.addCommand(new Command("返回",Command.BACK,0));
display.setCurrent(al);
}
else

...{
Calendar cal=Calendar.getInstance();
cal.setTime(dateField.getDate());
String strDate=Integer.toString(cal.get(cal.YEAR))+"年"+Integer.toString(cal.get(cal.MONTH))+"月"+Integer.toString(cal.get(cal.DAY_OF_MONTH))+"日 ";
//String strTime=Integer.toString(cal.get(cal.HOUR_OF_DAY))+":"+Integer.toString(cal.get(cal.MILLISECOND))+":"+Integer.toString(cal.get(cal.SECOND));
ContentDataEntity writeData=new ContentDataEntity(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);
}
//如果加入数据出错,则会抛出异常,下面这段代码不执行
Alert al=new Alert("");
al.setString("已经成功保存!");
al.setType(AlertType.INFO);
al.setTimeout(Alert.FOREVER);
display.setCurrent(al,this);
}
rs.closeRecordStore();
}
catch(Exception erro)

...{
Alert al=new Alert("");
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

...{
private Display display;
private NotePadMIDlet MIDlet;
private TextField titleField;
private TextField textField;
private DateField dateField;
private String title;
private String date;
private String text;
private int recordID;
public EditContentScreen(Display display,NotePadMIDlet MIDlet,int ID)

...{
super("新建记事");
this.display=display;
this.MIDlet=MIDlet;
this.recordID=ID;
readRecordData();
titleField=new TextField("标题",title,20,TextField.ANY);
dateField=new DateField("修改日期",DateField.DATE);
dateField.setDate(new Date());//设定时间为当前时间
textField=new TextField("正文",text,140,TextField.ANY);
this.append(titleField);
this.append(dateField);
this.append(textField);
this.addCommand(new Command("保存",Command.OK,0));
this.addCommand(new Command("返回",Command.OK,0));
this.setCommandListener(this);
}
public void commandAction(Command c,Displayable s)

...{
String cmd=c.getLabel();
if(cmd.equals("保存"))

...{
saveData();
}
else if(cmd.equals("返回"))

...{
ViewListScreen viewListScreen=new ViewListScreen(display,MIDlet);
display.setCurrent(viewListScreen);
}
}
private void readRecordData()

...{
String dbname="NotePadDataBase";
RecordStore rs=RMSUtil.openRSExisted(dbname);
if(rs==null)

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

...{
try

...{
ContentDataEntity recordData=new ContentDataEntity ();
recordData.decode(rs.getRecord(recordID));
title=recordData.title;
date=recordData.date;
text=recordData.text;
rs.closeRecordStore();
}
catch(Exception erro)

...{}
}
}
public void saveData()

...{
String dbname="NotePadDataBase";
RecordStore rs=RMSUtil.openRSAnyway(dbname);
try

...{
if(rs==null)

...{
Alert al=new Alert("");
al.setType(AlertType.INFO);
al.setTimeout(5000);
al.setString("打开数据库出错,请与我联系:norains@163.com");
this.addCommand(new Command("返回",Command.BACK,0));
display.setCurrent(al);
}
else

...{
Calendar cal=Calendar.getInstance();
cal.setTime(dateField.getDate());
String strDate=Integer.toString(cal.get(cal.YEAR))+"年"+Integer.toString(cal.get(cal.MONTH))+"月"+Integer.toString(cal.get(cal.DAY_OF_MONTH))+"日 ";
//String strTime=Integer.toString(cal.get(cal.HOUR_OF_DAY))+":"+Integer.toString(cal.get(cal.MILLISECOND))+":"+Integer.toString(cal.get(cal.SECOND));
ContentDataEntity writeData=new ContentDataEntity(titleField.getString(),strDate,textField.getString());
byte[] bytTemp=writeData.encode();
rs.setRecord(recordID,bytTemp,0,bytTemp.length);
//如果加入数据出错,则会抛出异常,下面这段代码不执行
Alert al=new Alert("");
al.setString("已经成功保存!");
al.setType(AlertType.INFO);
al.setTimeout(Alert.FOREVER);
display.setCurrent(al,this);
}
rs.closeRecordStore();
}
catch(Exception erro)

![]()