📄 rmsdemomidp.java
字号:
//import the 3 packages here
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class RmsDemoMidp extends MIDlet implements CommandListener {
//Declaration of Variables
TextBox textBox = new TextBox ("Diary", "", 150, TextField.ANY);
int currentId = 1;
byte[] myBytes;
String entry;
//Declare the text area for writing the events for the diary
//Declare the diary to be used for storing the events.
RecordStore diary;
Display display;
//Declare the commands for the menu
static final Command prevCommand = new Command ("Prev", Command.SCREEN, 1);
static final Command nextCommand = new Command ("Next", Command.SCREEN, 2);
static final Command newCommand = new Command ("New", Command.SCREEN, 3);
static final Command deleteCommand = new Command ("Delete", Command.SCREEN, 4);
static final Command exitCommand = new Command ("Exit", Command.SCREEN, 5);
public RmsDemoMidp ()
{
//Setup the diary for strong records, use try statement
try {
diary = RecordStore.openRecordStore ("diary", true);
String text = loadEntry (diary.getNumRecords ());
textBox.setString (text);
textBox.setTitle ("MyDiary - Day " + currentId);
myBytes = new byte(diary.getRecordSize(currentId) );
}
catch (RecordStoreException e)
{
throw new RuntimeException ("Cannot open diary; reason: "+e);
}
//add the commands to the text area
textBox.addCommand (prevCommand);
textBox.addCommand (nextCommand);
textBox.addCommand (newCommand);
textBox.addCommand (deleteCommand);
textBox.addCommand (exitCommand);
//set the command listener to listen to the commands
textBox.setCommandListener (this);
}
public String loadEntry (int newId) throws RecordStoreException
{
//check if it is a new entry or the first entry to the diary, if it is, add a blank record to the diary
if(newId!=currentId) entry = diary.addRecord(myBytes,0,30);
//else, get the record entry
else if(newId==currentId) entry = diary.getRecord(newId);
//Replace the return here with an entry from the diary for the current record
return (null);
}
public void saveEntry (String entry) throws RecordStoreException
{
//save the new entry to the diary
diary.setRecord(currentId,myBytes,0,30);
}
public void startApp ()
{
//initialise current display
display = Display.getDisplay (this);
display.setCurrent (textBox);
}
public void destroyApp (boolean unconditional)
{
try
{
String text;
text = textBox.getString ();
saveEntry (text);
diary.closeRecordStore ();
}
catch (RecordStoreException e)
{
throw new RuntimeException ("Cannot close Diary; reason: "+e);
}
}
public void pauseApp ()
{
}
public void commandAction (Command c, Displayable d)
{try{
if(c==deleteCommand)
{
destroyApp(true);
notifyDestroyed();
}
else if(c==prevCommand)
{currentId-=1;
entry = diary.getRecord(currentId);
}
else if(c==nextCommand)
{currentId+=1;
entry = diary.getRecord(currentId);
}
else if(c==newCommand)
{currentId+=1;
String text = loadEntry (diary.getNumRecords ());
textBox.setString (text);
textBox.setTitle ("MyDiary - Day " + currentId);
display = Display.getDisplay (this);
display.setCurrent (textBox);
}
else if(c==exitCommand)
{
destroyApp(true);
notifyDestroyed();
}
}
catch(RecordStoreNotOpenException e){}
catch(InvalidRecordIDException ie){}
catch(RecordStoreException re){}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -