?? passwordmanagermidlet.java
字號:
package pm.core;
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.microedition.rms.*;
/**
* 密碼管理軟件
* 實現將常用的密碼信息都保存在手機中。如銀行密碼、郵箱密碼等等,該軟件不訪問網絡,所以絕對不會泄漏您的個人隱私。
*/
public class PasswordManagerMidlet extends MIDlet implements CommandListener {
//顯示對象
Display display;
//主題
String title = "開源密碼管理軟件";
//軟件主界面
List lstMain;
//主界面確定按鈕
Command cmdEnterMain;
//退出按鈕
Command cmdExit;
//增加密碼界面
Form frmAddPassword;
//密碼ID
TextField tfId;
//密碼
TextField tfPsswordAdd;
//確認密碼
TextField tfConfirmPasswordAdd;
//備注信息
TextField tfRemark;
//確定按鈕
Command cmdEnterAdd;
//返回按鈕
Command cmdBackAdd;
//查看密碼主界面
List lstView;
//查看詳細信息按鈕
Command cmdInfo;
//返回按鈕
Command cmdBackViewMain;
//查看密碼中的顯示詳細信息窗口
TextBox tbViewInfo;
//刪除按鈕
Command cmdDelView;
//修改按鈕
Command cmdModify;
//返回按鈕
Command cmdBackViewInfo;
//查看密碼中的修改密碼窗口
Form frmViewModifyPassword;
//密碼文本框
TextField tfPasswordView;
//確認密碼文本框
TextField tfConfirmPasswordView;
//修改按鈕
Command cmdModifyView;
//返回按鈕
Command cmdBackViewModify;
//設置密碼界面
Form frmSetPassword;
//密碼
TextField tfPassword;
//確認密碼
TextField tfConfirmPassword;
//確定按鈕
Command cmdEnterSetPassword;
//返回按鈕
Command cmdBackSetPassword;
//刪除按鈕
Command cmdDelSetPassword;
//幫助界面
Form frmHelp;
//幫助界面的返回按鈕
Command cmdBackHelp;
//關于界面
Form frmAbout;
//關于界面的返回按鈕
Command cmdBackAbout;
//提示窗口
Alert alert;
//啟動時的輸入密碼界面
Form frmStartPassword;
//密碼輸入文本框
TextField tfStartPassword;
//密碼界面中的確定按鈕
Command cmdEnterStart;
//密碼
String password = "";
//記錄對象
RecordStore rs;
//存儲密碼ID內容
String[] id;
//保存顯示的密碼ID對應的索引號,數組下標為0的代表第一個密碼ID,1的代表第二個,依次類推
int[] idIndex;
/**
* 初始化界面元素
*/
public PasswordManagerMidlet() {
//初始化顯示對象
display = Display.getDisplay(this);
//初始化主窗體
lstMain = new List(title,List.IMPLICIT);
lstMain.append("增加",null);
lstMain.append("察看",null);
lstMain.append("設置",null);
lstMain.append("幫助",null);
lstMain.append("關于",null);
//初始化主界面確定按鈕
cmdEnterMain = new Command("確定",Command.OK,1);
//添加到主界面
lstMain.addCommand(cmdEnterMain);
//初始化退出按鈕
cmdExit = new Command("退出",Command.EXIT,1);
//添加到主界面
lstMain.addCommand(cmdExit);
//初始化增加密碼界面
frmAddPassword = new Form(title);
tfId = new TextField("密碼ID:","",100,TextField.ANY);
tfPsswordAdd = new TextField("密碼:","",20,TextField.PASSWORD);
tfConfirmPasswordAdd = new TextField("1確認密碼:","",20,TextField.PASSWORD);
tfRemark = new TextField("備注:","",100,TextField.ANY);
cmdEnterAdd = new Command("確定",Command.OK,1);
cmdBackAdd = new Command("返回",Command.BACK,1);
//添加到增加密碼界面
frmAddPassword.append(tfId);
frmAddPassword.append(tfPsswordAdd);
frmAddPassword.append(tfConfirmPasswordAdd);
frmAddPassword.append(tfRemark);
frmAddPassword.addCommand(cmdEnterAdd);
frmAddPassword.addCommand(cmdBackAdd);
//初始化查看密碼主界面
lstView = new List(title,List.IMPLICIT);
cmdInfo = new Command("查看",Command.SCREEN,1);
cmdBackViewMain = new Command("返回",Command.BACK,1);
//添加到查看密碼主界面
lstView.addCommand(cmdInfo);
lstView.addCommand(cmdBackViewMain);
//初始化查看密碼詳細信息界面
tbViewInfo = new TextBox("詳細信息","",200,TextField.ANY);
cmdDelView =new Command("刪除",Command.SCREEN,1);
cmdModify = new Command("修改",Command.SCREEN,1);
cmdBackViewInfo =new Command("返回",Command.BACK,1);
//添加到查看密碼詳細信息界面中
tbViewInfo.addCommand(cmdDelView);
tbViewInfo.addCommand(cmdModify);
tbViewInfo.addCommand(cmdBackViewInfo);
//初始化查看密碼中的修改密碼界面
frmViewModifyPassword = new Form("修改密碼");
tfPasswordView = new TextField("密碼:","",20,TextField.PASSWORD);
tfConfirmPasswordView = new TextField("確認密碼:","",20,TextField.PASSWORD);
cmdModifyView = new Command("確定",Command.OK,1);
cmdBackViewModify = new Command("返回",Command.BACK,1);
//添加到修改密碼界面
frmViewModifyPassword.append(tfPasswordView);
frmViewModifyPassword.append(tfConfirmPasswordView);
frmViewModifyPassword.addCommand(cmdModifyView);
frmViewModifyPassword.addCommand(cmdBackViewModify);
//初始化設置密碼界面
frmSetPassword = new Form("設置密碼");
tfPassword = new TextField("密碼:","",20,TextField.PASSWORD);
tfConfirmPassword = new TextField("確認密碼:","",20,TextField.PASSWORD);
cmdEnterSetPassword = new Command("確定",Command.OK,1);
cmdBackSetPassword = new Command("返回",Command.BACK,1);
cmdDelSetPassword = new Command("刪除",Command.SCREEN,1);
//添加到設置密碼界面
frmSetPassword.append(tfPassword);
frmSetPassword.append(tfConfirmPassword);
frmSetPassword.addCommand(cmdEnterSetPassword);
frmSetPassword.addCommand(cmdBackSetPassword);
frmSetPassword.addCommand(cmdDelSetPassword);
//初始化幫助界面
frmHelp = new Form("幫助");
frmHelp.append("開源密碼管理軟件是一款幫助您管理各種密碼的軟件,\"增加\"中可以增加新的密碼,\"察看\"中可以查看、修改和刪除已有的密碼,\"設置\"中可以進行該軟件的進入密碼設置。");
//初始化返回按鈕
cmdBackHelp = new Command("返回",Command.BACK,1);
//添加到幫助界面
frmHelp.addCommand(cmdBackHelp);
//初始化關于界面
frmAbout = new Form("關于...");
frmAbout.append("版權所有 2004- 作者:陳躍峰 email:cqucyf@263.net 歡迎您提出該版本的更新建議");
//初始化返回按鈕
cmdBackAbout = new Command("返回",Command.BACK,1);
//添加到關于界面
frmAbout.addCommand(cmdBackAbout);
//初始化提示窗口
alert = new Alert(title);
//初始化啟動時的密碼界面
frmStartPassword = new Form(title);
//初始化啟動時的密碼輸入文本框
tfStartPassword = new TextField("請輸入密碼","",20,TextField.PASSWORD);
//初始化確定按鈕
cmdEnterStart = new Command("確定",Command.OK,1);
//添加到密碼界面中
frmStartPassword.append(tfStartPassword);
frmStartPassword.addCommand(cmdEnterStart);
frmStartPassword.addCommand(cmdExit);
//事件處理
lstMain.setCommandListener(this);
frmHelp.setCommandListener(this);
frmAbout.setCommandListener(this);
frmStartPassword.setCommandListener(this);
frmSetPassword.setCommandListener(this);
frmAddPassword.setCommandListener(this);
lstView.setCommandListener(this);
tbViewInfo.setCommandListener(this);
frmViewModifyPassword.setCommandListener(this);
/* //測試代碼,添加記錄
try{
rs = RecordStore.openRecordStore("password",true);
rs.setRecord(1,new String("123456").getBytes(),0,6);
// System.out.println(i);
rs.closeRecordStore();
}catch(Exception e){
System.out.println("測試代碼--添加記錄:" + e);
}
//測試代碼,刪除所有記錄集
try{
RecordStore.deleteRecordStore("id");
RecordStore.deleteRecordStore("pwd");
RecordStore.deleteRecordStore("remark");
RecordStore.deleteRecordStore("flag");
//RecordStore.deleteRecordStore("password");
}catch(Exception e){
System.out.println(e);
} */
}
/**
* 啟動方法
*/
public void startApp () {
try{
//打開密碼紀錄
rs = RecordStore.openRecordStore("password",false);
//讀取密碼
byte[] b = rs.getRecord(1);
password = new String(b,"iso8859_1");
//關閉記錄
rs.closeRecordStore();
//顯示輸入密碼界面
display.setCurrent(frmStartPassword);
}catch(Exception e){
//沒有密碼記錄,則顯示主界面
display.setCurrent(lstMain);
}
}
public void destroyApp(boolean unconditional) {
}
public void pauseApp() {
}
/**
* 事件處理
*/
public void commandAction(Command c, Displayable s) {
//處理啟動時的密碼窗口中的確定按鈕事件
if(c == cmdEnterStart){
//用戶輸入的密碼
String pwd = tfStartPassword.getString();
//判斷用戶輸入是否為空
if(pwd == null || pwd.length() ==0){ //輸入為空
//顯示警告提示
displayAlert("請輸入密碼!");
}else{ //輸入不為空
//比較密碼
if(pwd.equals(password)){ //密碼正確
//顯示主界面
display.setCurrent(lstMain);
}else{//密碼錯誤
//顯示警告提示
displayAlert("密碼錯誤,請重新輸入!");
}
}
}
//處理退出事件
if(c == cmdExit){
destroyApp(false);
notifyDestroyed();
}
//處理主界面中的選擇
if(c == cmdEnterMain){
int index = lstMain.getSelectedIndex();
//System.out.println(index);
//選擇“增加”
if(index == 0){
//顯示增加密碼界面
display.setCurrent(frmAddPassword);
}
//選擇“查看”
if(index == 1){
//獲得密碼ID列表
try{
//打開flag記錄集
RecordStore rsTemp = RecordStore.openRecordStore("flag",true);
//打開ID記錄集
rs = RecordStore.openRecordStore("id",true);
//獲得記錄集中記錄的個數
int num = rs.getNumRecords();
//初始化密碼ID索引數組
idIndex = new int[num];
//創建存儲ID的數組
id = new String[num];
//將ID信息讀入ID數組中
int j = 0;//代表數組的下標
for(int i = 1;i <= num;i++){
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -