📄 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 + -