📄 stopwatch.java
字号:
package stopwatch;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class StopWatch extends MIDlet {
// preferences database name
private static final String PREFERENCES_DATABASE = "Preferences";
static StopWatch instance;
MainMenu mainMenu = new MainMenu();
public StopWatch() {
instance = this;
}
public void startApp() {
// Load any previously-saved user options.
loadOptions();
// Display the main stop watch screen.
Display.getDisplay(this).setCurrent(mainMenu);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
// Save all options.
saveOptions();
// Release references.
mainMenu.destroy();
mainMenu = null;
}
public static void quitApp() {
instance.destroyApp(true);
instance.notifyDestroyed();
instance = null;
}
/**
* <p>Retrieves the user preferences stored off in the local
* database file.</p>
*/
private void loadOptions() {
RecordStore database = null;
try {
// open the database of stored stock symbols
database = RecordStore.openRecordStore(PREFERENCES_DATABASE, true);
// we store the display format option value as the only record in the
// database.
String displayFormatString = null;
RecordEnumeration enum = database.enumerateRecords(null, null, false);
// This loop will just extract the first element (our display format
// option) and then quit the loop.
while (enum.hasNextElement()) {
displayFormatString = new String(enum.nextRecord());
break;
}
// set the display format
if (displayFormatString != null) {
// Convert the stored string into an integer that represents the
// display time format.
int displayFormat = Integer.parseInt(displayFormatString);
// Now, set it as the currently selected display time format.
mainMenu.setDisplayFormat(displayFormat);
}
}
catch (Exception e) {
// If anything goes wrong, we will see a stack trace.
e.printStackTrace();
}
finally {
// we have (hopefully) retrieved our data. Tidy up and close the
// database.
if (database != null) try {
database.closeRecordStore();
}
catch (Exception e) {
}
}
}
/**
* <p>Save user preferences to database.</p>
*/
private void saveOptions() {
RecordStore preferencesDb = null;
try {
// Remove the old preferences database to be sure that we save a clean
// copy. (overwriting it may not overwrite properly so we delete it
// instead).
RecordStore.deleteRecordStore(PREFERENCES_DATABASE);
// Now create a fresh preferences database and store off our
// user preferences.
// (The openRecordStore() method creates a new database if it can't find
// one with the given name. Since we just deleted the old one, it will
// just create a new database.)
preferencesDb = RecordStore.openRecordStore(PREFERENCES_DATABASE, true);
// Retrieve the currently selected display time format.
int displayFormat = mainMenu.getDisplayFormat();
// Store the display format as a string.
// First, convert the display format to a string.
String displayFormatString = String.valueOf(displayFormat);
// Now, add it to the database.
preferencesDb.addRecord(
displayFormatString.getBytes(), // The string bytes.
0, // Record offset.
displayFormatString.getBytes().length); // Number of bytes to store.
}
catch (Exception e) {
// If anything went wrong, we will get a stack trace.
e.printStackTrace();
}
finally {
// Close the database.
if (preferencesDb != null) try {
preferencesDb.closeRecordStore();
}
catch (Exception e1) {
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -