?? phone.java
字號:
import javax.microedition.rms.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import java.util.Enumeration;
import java.util.Date;
import java.util.Calendar;
import java.io.*;
public class Phone extends MIDlet implements CommandListener
{
Display display = null;
List menu = null; // The initial menu
List choose = null;
Form form = new Form("添加記錄");
//Define the Alert
Alert alert = new Alert("刪除記錄", "正在刪除信息...", null, AlertType.INFO);
static final Command backCommand = new Command("Back", Command.BACK, 0);
static final Command mainMenuCommand = new Command("Main", Command.SCREEN, 1);
static final Command saveCommand = new Command("Save", Command.OK, 2);
static final Command exitCommand = new Command("Exit", Command.STOP, 3);
String currentMenu = null;
// 定義RecordStore對象
RecordStore rs = null;
// Form components
//償還日期域
//DateField loanDate = new DateField("Repay Date", DateField.DATE);
//貸款號
TextField loanNumber = new TextField("姓名:", null, 50, TextField.ANY);
//貸款金額
TextField loanAmount = new TextField("電話號碼:", null, 20, TextField.NUMERIC);
// constructor
public Phone()
{
}
// 打開指定名稱的記錄存儲
public RecordStore openRS(String fileName)
{
try
{
rs = RecordStore.openRecordStore(fileName, true);
}
catch(RecordStoreException rse)
{
rse.printStackTrace();
}
return rs;
}
// 關閉記錄存儲
public void closeRS() throws RecordStoreNotOpenException, RecordStoreException
{
//如果記錄存儲中無記錄則關閉后刪除;如果有記錄則只關閉。
if (rs.getNumRecords() == 0)
{
String fileName = rs.getName();
rs.closeRecordStore();
rs.deleteRecordStore(fileName);
}
else
{
rs.closeRecordStore();
}
}
//使用枚舉對象刪除記錄存儲中的一條記錄
public void deleteRS()
{
try
{
//創建記錄枚舉對象(自定義方法)
RecordEnumeration re = enumerate();
//遍歷每條記錄,
while(re.hasNextElement())
{
int id = re.nextRecordId();
rs.deleteRecord(id);
}
showAlert();
}
catch (Exception e)
{ }
}
// 獲得記錄枚舉對象,枚舉值為記錄存儲中的記錄
public synchronized RecordEnumeration enumerate() throws RecordStoreNotOpenException
{
return rs.enumerateRecords(null, null, false);
}
// start the MIDlet
public void startApp() throws MIDletStateChangeException
{
display = Display.getDisplay(this);
// 打開記錄源
try
{
openRS("myloan");
}
catch(Exception e)
{}
//添加菜單項
menu = new List("通訊錄", Choice.IMPLICIT);
menu.append("添加新信息", null);
menu.append("查看信息", null);
menu.append("刪除信息", null);
menu.addCommand(exitCommand);
menu.setCommandListener(this);
//加載文本框和日期域
form.append(loanNumber);
//form.append(loanDate);
form.append(loanAmount);
mainMenu();
}
public void pauseApp()
{
display = null;
choose = null;
menu = null;
form = null;
try
{
closeRS();
}
catch(Exception e)
{}
}
public void destroyApp(boolean unconditional)
{
try
{
closeRS();
}
catch(Exception e)
{}
notifyDestroyed();
}
void mainMenu()
{
display.setCurrent(menu);
currentMenu = "Main";
}
// 顯示警告信息
public void showAlert()
{
try
{
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
catch(Exception e)
{}
}
// 將日期類型轉換成字符串類型,日期格式為mm/dd/yyyy
public String dateTostr(Date dateVal)
{
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateVal);
String strDate = Integer.toString (calendar.get(calendar.DAY_OF_MONTH));
String strMonth = Integer.toString (calendar.get(calendar.MONTH)+1);
String strYear = Integer.toString (calendar.get(calendar.YEAR));
return strMonth + "/" + strDate + "/" + strYear;
}
// Show the form
public void addLoan ()
{
form.addCommand(saveCommand);
form.addCommand(backCommand);
form.setCommandListener(this);
display.setCurrent(form);
currentMenu = "Add";
}
// 添加新記錄
public synchronized void addNewLoan(String record)
{
//創建字節數組輸出流對象,存儲記錄
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//將字節數組輸出流打包成數據輸出流
DataOutputStream daos = new DataOutputStream(baos);
try
{
//獲取輸入數據
daos.writeUTF(record);
}
catch (IOException ioe)
{
System.out.println(ioe);
ioe.printStackTrace();
}
//創建字節數組,存儲用戶輸入的字節數據
byte[] bytearr = baos.toByteArray();
try
{
//將數據寫入記錄存儲
rs.addRecord(bytearr, 0, bytearr.length);
}
catch (RecordStoreException rse)
{
System.out.println(rse);
rse.printStackTrace();
}
}
// 在記錄存儲中列出償還材料
public void listItems()
{
choose = new List("察看信息", Choice.IMPLICIT);
choose.addCommand(backCommand);
choose.setCommandListener(this);
try
{
//獲得記錄集
RecordEnumeration re = enumerate();
while(re.hasNextElement())
{
String theList = new String(re.nextRecord());
//向列表項中插入記錄
choose.append(theList, null);
}
}
catch(Exception e)
{}
display.setCurrent(choose);
currentMenu = "List";
}
// 時間處理程序
public void commandAction(Command c, Displayable d)
{
String label = c.getLabel();
//Exit按鈕退出程序
if (label.equals("Exit"))
{
destroyApp(true);
}
//Save按鈕保存程序
else if (label.equals("Save"))
{
if(currentMenu.equals("Add"))
{
// add it to record store
try
{
String strNumber = loanNumber.getString();
String strAmount = loanAmount.getString();
// String strDate = dateTostr (loanDate.getDate());
// Create the new record by concatenating the inputs
String strResult = strNumber + " ;" + strAmount;
// Add it to the record store
addNewLoan(strResult);
}
catch(Exception e)
{}
mainMenu();
}
}
//Back按鈕返回主窗體
else if (label.equals("Back"))
{
if(currentMenu.equals("List"))
{
// go back to menu
mainMenu();
}
else if(currentMenu.equals("Add"))
{
// go back to menu
mainMenu();
}
}
//菜單處理
else
{
List down = (List)display.getCurrent();
switch(down.getSelectedIndex())
{
case 0: addLoan();break;
case 1: listItems();break;
case 2: deleteRS();break;
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -