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