NotePad完整源代码

    J2ME版本的记事本,类似于西门子C65自带的便签.

//---------------------------------------------------
//ContentDataEntity.java
//---------------------------------------------------
package src;
import java.io.*;
public class ContentDataEntity 
{
    
public String title;
    
public String date;
    
public String text;

    
    
public ContentDataEntity(String title,String date,String text)
    
{
        
this.title=title;
        
this.text=text;
        
this.date=date;
        
    }

    
    
public ContentDataEntity()
    
{}
    
    
public byte[] encode()
    
{
        
byte[] bytData=null;
        
try
        
{
            ByteArrayOutputStream bos
=new ByteArrayOutputStream();
            DataOutputStream dos
=new DataOutputStream(bos);
            dos.writeUTF(title);
            dos.writeUTF(text);
            dos.writeUTF(date);            
            bytData
=bos.toByteArray();
            dos.close();
            bos.close();
        }

        
catch(Exception erro)
        
{}        
        
return bytData;
    }

    
    
public void decode(byte[] data)
    
{
        
try
        
{
            ByteArrayInputStream bis
=new ByteArrayInputStream(data);
            DataInputStream dis
=new DataInputStream(bis);
            title
=dis.readUTF();
            text
=dis.readUTF();
            date
=dis.readUTF();
            bis.close();
            dis.close();
        }

        
catch(Exception erro)
        
{}
    }

}

 

//------------------------------------------------------
//CreatNewTextScreen.java
//------------------------------------------------------
package src;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.*;
import javax.microedition.rms.*;
public class CreatNewTextScreen extends Form implements CommandListener
{    
    
private Display display;
    
private NotePadMIDlet MIDlet;
    
private TextField titleField;
    
private TextField textField;
    
private DateField dateField;
    
private int recordID;
    
private boolean firstSave=true;
    
    
public CreatNewTextScreen(Display display,NotePadMIDlet MIDlet)
    
{
        
super("新建记事");
        
this.display=display;
        
this.MIDlet=MIDlet;
        titleField
=new TextField("标题:","",20,TextField.ANY);
        dateField
=new DateField("日期",DateField.DATE);        
        dateField.setDate(
new Date());//设定时间为当前时间
        textField=new TextField("正文","",140,TextField.ANY);
        
this.append(titleField);
        
this.append(dateField);
        
this.append(textField);
        
this.addCommand(new Command("保存",Command.OK,0));
        
this.addCommand(new Command("返回",Command.OK,0));
        
this.setCommandListener(this);
    }

    
    
public void commandAction(Command c,Displayable s)
    
{
        String cmd
=c.getLabel();
        
if(cmd.equals("保存"))
        
{
            saveData();
        }

        
else if(cmd.equals("返回"))
        
{
            ListScreen listScreen
=new ListScreen(display,MIDlet);
            display.setCurrent(listScreen);
        }

    }

    
    
public void saveData()
    
{
        String dbname
="NotePadDataBase";
        RecordStore rs
=RMSUtil.openRSAnyway(dbname);
        
try
        
{
            
if(rs==null)
            
{
                Alert al
=new Alert("");
                al.setType(AlertType.INFO);
                al.setTimeout(
5000);
                al.setString(
"打开数据库出错,请与我联系:norains@163.com");
                
this.addCommand(new Command("返回",Command.BACK,0));
                display.setCurrent(al);
            }

            
else
            
{
                Calendar cal
=Calendar.getInstance();
                cal.setTime(dateField.getDate());
                String strDate
=Integer.toString(cal.get(cal.YEAR))+""+Integer.toString(cal.get(cal.MONTH))+""+Integer.toString(cal.get(cal.DAY_OF_MONTH))+"日 ";
                
//String strTime=Integer.toString(cal.get(cal.HOUR_OF_DAY))+":"+Integer.toString(cal.get(cal.MILLISECOND))+":"+Integer.toString(cal.get(cal.SECOND));
                ContentDataEntity writeData=new ContentDataEntity(titleField.getString(),strDate,textField.getString());
                
byte[] bytTemp=writeData.encode();
                
if(firstSave==true)
                
{
                    recordID
=rs.addRecord(bytTemp,0,bytTemp.length);
                    firstSave
=false;
                }

                
else
                
{
                    rs.setRecord(recordID,bytTemp,
0,bytTemp.length);
                }

                
//如果加入数据出错,则会抛出异常,下面这段代码不执行
                Alert al=new Alert("");
                al.setString(
"已经成功保存!");
                al.setType(AlertType.INFO);
                al.setTimeout(Alert.FOREVER);
                display.setCurrent(al,
this);
            }

            rs.closeRecordStore();
        }

        
catch(Exception erro)
        
{
            Alert al
=new Alert("");
            al.setString(
"出现错误,无法保存!请与我联系:norains@163.com");
            al.setType(AlertType.ERROR);
            al.setTimeout(Alert.FOREVER);
            display.setCurrent(al,
this);
        }

    }

    
    
    
}

//-----------------------------------------------------
//EditContentScreen.java
//-----------------------------------------------------
package src;

import java.util.Calendar;
import java.util.Date;

import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

public class EditContentScreen extends Form implements CommandListener
{
    
private Display display;
    
private NotePadMIDlet MIDlet;
    
private TextField titleField;
    
private TextField textField;
    
private DateField dateField;
    
private String title;
    
private String date;
    
private String text;
    
private int recordID;
    
    
public EditContentScreen(Display display,NotePadMIDlet MIDlet,int ID)
    
{
        
super("新建记事");
        
this.display=display;
        
this.MIDlet=MIDlet;
        
this.recordID=ID;
        readRecordData();
        titleField
=new TextField("标题",title,20,TextField.ANY);
        dateField
=new DateField("修改日期",DateField.DATE);        
        dateField.setDate(
new Date());//设定时间为当前时间
        textField=new TextField("正文",text,140,TextField.ANY);
        
this.append(titleField);
        
this.append(dateField);
        
this.append(textField);
        
this.addCommand(new Command("保存",Command.OK,0));
        
this.addCommand(new Command("返回",Command.OK,0));
        
this.setCommandListener(this);
    }

    
    
public void commandAction(Command c,Displayable s)
    
{
        String cmd
=c.getLabel();
        
if(cmd.equals("保存"))
        
{
            saveData();
        }

        
else if(cmd.equals("返回"))
        
{
            ViewListScreen viewListScreen
=new ViewListScreen(display,MIDlet);
            display.setCurrent(viewListScreen);
        }

    }

    
    
private void readRecordData()
    
{
        String dbname
="NotePadDataBase";
        RecordStore rs
=RMSUtil.openRSExisted(dbname);
        
if(rs==null)
        
{
            
this.append("打开数据库失败!可能尚未储存数据,请进行游戏后再尝试.如果依然出现错误,请与我联系:norains@163.com");
        }

        
else
        
{    
            
try
            
{
                ContentDataEntity recordData
=new ContentDataEntity ();
                recordData.decode(rs.getRecord(recordID));
                title
=recordData.title;
                date
=recordData.date;
                text
=recordData.text;                
                rs.closeRecordStore();
            }

            
catch(Exception erro)
            
{}            
        }
    
    }

    
    
public void saveData()
    
{
        String dbname
="NotePadDataBase";
        RecordStore rs
=RMSUtil.openRSAnyway(dbname);
        
try
        
{
            
if(rs==null)
            
{
                Alert al
=new Alert("");
                al.setType(AlertType.INFO);
                al.setTimeout(
5000);
                al.setString(
"打开数据库出错,请与我联系:norains@163.com");
                
this.addCommand(new Command("返回",Command.BACK,0));
                display.setCurrent(al);
            }

            
else
            
{
                Calendar cal
=Calendar.getInstance();
                cal.setTime(dateField.getDate());
                String strDate
=Integer.toString(cal.get(cal.YEAR))+""+Integer.toString(cal.get(cal.MONTH))+""+Integer.toString(cal.get(cal.DAY_OF_MONTH))+"日 ";
                
//String strTime=Integer.toString(cal.get(cal.HOUR_OF_DAY))+":"+Integer.toString(cal.get(cal.MILLISECOND))+":"+Integer.toString(cal.get(cal.SECOND));
                ContentDataEntity writeData=new ContentDataEntity(titleField.getString(),strDate,textField.getString());
                
byte[] bytTemp=writeData.encode();
                rs.setRecord(recordID,bytTemp,
0,bytTemp.length);
                
                
//如果加入数据出错,则会抛出异常,下面这段代码不执行
                Alert al=new Alert("");
                al.setString(
"已经成功保存!");
                al.setType(AlertType.INFO);
                al.setTimeout(Alert.FOREVER);
                display.setCurrent(al,
this);
            }

            rs.closeRecordStore();
        }

        
catch(Exception erro)
        
{
            Alert al
=new Alert("");
            al.setString(
"出现错误,无法保存!请与我联系:norains@163.com");
            al.setType(AlertType.ERROR);
            al.setTimeout(Alert.FOREVER);
            display.setCurrent(al,
this);
        }

    }

    
}


//---------------------------------------
//EditPasswordScreen.java
//---------------------------------------
package src;

import javax.microedition.lcdui.*;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;

public class EditPasswordScreen extends Form implements CommandListener
{
    
private boolean passwordExist=false;
    
private Display display;
    
private String oldPassword;
    
private int recordID;
    
private TextField oldPasswordField;
    
private TextField newPasswordField1;
    
private TextField newPasswordField2;
    
private Displayable nextDisp;
    
public EditPasswordScreen(Display display,Displayable nextDisp)
    
{
        
super("");
        
this.display=display;
        
this.nextDisp=nextDisp;
        readPassword();
        
if(passwordExist==true)
        
{
            oldPasswordField
=new TextField("请输入原密码:","",20,TextField.PASSWORD);
            newPasswordField1
=new TextField("请输入新密码:","",20,TextField.PASSWORD);;
            newPasswordField2
=new TextField("请再次输入新密码:","",20,TextField.PASSWORD);;
            
this.append(oldPasswordField);
            
this.append(newPasswordField1);
            
this.append(newPasswordField2);
        }

        
else
        
{
            newPasswordField1
=new TextField("请输入新密码:","",20,TextField.PASSWORD);;
            newPasswordField2
=new TextField("请再次输入新密码:","",20,TextField.PASSWORD);;
            
this.append(newPasswordField1);
            
this.append(newPasswordField2);
        }

        
this.addCommand(new Command("提交",Command.OK,0));
        
this.addCommand(new Command("返回",Command.OK,0));    
        
this.setCommandListener(this);
    }

    
    
public void commandAction(Command c,Displayable disp)
    
{
        String cmd
=c.getLabel();
        
if(cmd.equals("提交"))
        
{
            updataPassword();
        }

        
else if(cmd.equals("返回"))
        
{
            display.setCurrent(nextDisp);
        }

    }

    
    
//读取密码
    public void readPassword()
    
{
        String dbname
="NotePadSettingDateBase";
        RecordStore rs
=RMSUtil.openRSAnyway(dbname);
        
try
        
{
            
if(rs==null)
            
{
                
this.append("打开数据库失败!请与我联系:norains@163.com");
            }

            
else
            
{                    
                RecordEnumeration re
=rs.enumerateRecords(null,null,false);
                
if(re.numRecords()!=0)
                
{
                    passwordExist
=true;
                    recordID
=re.nextRecordId();
                    SettingDataEntity sde
=new SettingDataEntity();
                    sde.decode(rs.getRecord(recordID));
                    oldPassword
=sde.password;
                }

                
else
                
{
                    passwordExist
=false;
                }

            }

            rs.closeRecordStore();
        }

        
catch(Exception erro)
        
{}
    }

    
    
//更新密码
    public void updataPassword()
    
{
        String dbname
="NotePadSettingDateBase";
        RecordStore rs
=RMSUtil.openRSAnyway(dbname);        
        
try
        
{
            
if(rs==null)//无法打开数据库
            {
                
this.append("打开数据库失败!请与我联系:norains@163.com");
            }

            
else //能够打开数据库
            {    
                String tmppasd1
=newPasswordField1.getString();                
                String tmppasd2
=newPasswordField2.getString();
                SettingDataEntity sde
=new SettingDataEntity(true,newPasswordField1.getString());
                
byte[] outputData=sde.encode();
                
if(tmppasd1.equals(tmppasd2)) //两次输入的新密码相同
                {
                    
if(passwordExist==true//原来存在密码
                    {
                        
if(oldPassword.equals(oldPasswordField.getString())) //输入的原密码和保存的密码相同
                        {
                            rs.setRecord(recordID,outputData,
0,outputData.length);
                            Alert al
=new Alert("");
                            al.setString(
"成功更改密码!");
                            al.setTimeout(Alert.FOREVER);
                            display.setCurrent(al,nextDisp);
                        }

                        
else //输入的密码和保存的密码不相同
                        {
                            Alert al
=new Alert("");
                            al.setString(
"原密码不对!");
                            al.setTimeout(Alert.FOREVER);
                            al.setType(AlertType.ERROR);
                            display.setCurrent(al);
                        }

                    }

                    
else //原来不存在密码
                    {
                        recordID
=rs.addRecord(outputData,0,outputData.length);
                        Alert al
=new Alert("");
                        al.setString(
"成功设置密码!");
                        al.setTimeout(Alert.FOREVER);
                        display.setCurrent(al,nextDisp);
                    }

                }

                
else //两次输入的新密码不同
                {
                    Alert al
=new Alert("");
                    al.setString(
"两次输入的新密码不相同!");
                    al.setTimeout(Alert.FOREVER);
                    al.setType(AlertType.ERROR);
                    display.setCurrent(al);
                }

            }

            rs.closeRecordStore();
        }

        
catch(Exception erro)
            
{}        
    }

}

//--------------------------------------
//FlashScreen.java
//--------------------------------------
package src;
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import java.util.*;
public class FlashScreen extends GameCanvas implements Runnable
{
    
private LayerManager layerManager;
    
private Sprite animation;
    
private Display display;
    
private NotePadMIDlet MIDlet;
    
public FlashScreen(Display display,NotePadMIDlet m)
    
{
        
super(false);
        
this.display=display;
        MIDlet
=m;
        
this.setFullScreenMode(true);
        layerManager
=new LayerManager();
        animation
=createAnimation("/res/flash.png");
        layerManager.append(animation);

        start();
    }

    
    
private Sprite createAnimation(String pic)
    
{
        Image img
=null;
        
try
        
{
            img
=Image.createImage(pic);            
        }

        
catch(Exception erro)
        
{}
        
return new Sprite(img,96,54);
    }

    

    
boolean conti=true;
    
int rate=300;
    
public void run()
    
{
        
long st=0;
        
long et=0;
        Graphics g
=getGraphics();
        System.out.println(
"ret");
        
while(conti==true)
        
{
            st
=System.currentTimeMillis();
            input();
            render(g);
            et
=System.currentTimeMillis();
            
if((et-st)<rate)
            
{
                
try
                
{
                    Thread.sleep(rate
-(et-st));
                }

                
catch(Exception erro)
                
{}
            }

        }

    }

    
    
public void input()
    
{
        
int keyState=getKeyStates();
        
if(keyState!=0)
        
{
            stop();
            TestPasswordScreen readSettingScreen
=new TestPasswordScreen(display,MIDlet);
            
//display.setCurrent(readSettingScreen);
            
//在这里之所以不使用上面这句setCurrent是因为ReadSettingScreen内部构造函数有setCurrent语句;
            
//如果设定了反而令流程不正常.这应该是个BUG.不明白可以把上面注释划掉测试看看----在NotePadKeyDataBase没建立的时候

        }

    }

    
    
public void render(Graphics g)
    
{
        g.setColor(
255,255,255);
        g.fillRect(
0,0,getWidth(),getHeight());
        animation.nextFrame();
        layerManager.setViewWindow(
0,0,getWidth(),getHeight());
        layerManager.paint(g,(getWidth()
-96)/2,(getHeight()-54)/2);
        flushGraphics();
    }

    
    
public void start()
    
{
        Thread t
=new Thread(this);
        t.start();
    }

    
    
public void stop()
    
{
        conti
=false;
    }

}

//-----------------------------------------------------
//InfoScreen.java
//-----------------------------------------------------

    package
 src;
import javax.microedition.lcdui.*;
public class InfoScreen extends Form implements CommandListener
{
    
private Display display;
    
private NotePadMIDlet MIDlet;
    
public InfoScreen(Display display,NotePadMIDlet MIDlet)
    
{
        
super("程序说明");
        
this.display=display;
        
this.MIDlet=MIDlet;
        
this.append("名称:掌上记事版本:0.1 联系:norains@163.com 说明:手机上编写记事的好帮手。兄弟们如果有什么好建议,欢迎和偶联系!");
        
this.addCommand(new Command("返回",Command.BACK,0));
        
this.setCommandListener(this);
    }

    
public void commandAction(Command c,Displayable disp)
    
{
        String cmd
=c.getLabel();
        
if(cmd.equals("返回"))
        
{
            ListScreen listScreen
=new ListScreen(display,MIDlet);
            display.setCurrent(listScreen);
        }

    }

}

//-----------------------------------------------------
//ListScreen.java
//-----------------------------------------------------

    package src;
import javax.microedition.lcdui.
*;
public class ListScreen extends List implements CommandListener
{    
    
private Display display;
    
private NotePadMIDlet MIDlet;
    
//传入TestLatencyMIDlet主要是为了能进行退出程序的操作.
    public ListScreen(Display display,NotePadMIDlet m)
    
{
        super(
"",Choice.IMPLICIT);    
        MIDlet
=m;
        
this.display=display;
        Image img1,img2,img3,img4,img5;
        img1
=img2=img3=img4=img5=null;
        
try
        
{
            img1
=Image.createImage("/res/1.PNG");
            img2
=Image.createImage("/res/2.PNG");
            img3
=Image.createImage("/res/3.PNG");
            img4
=Image.createImage("/res/4.PNG");
            img5
=Image.createImage("/res/5.PNG");
        }

        
catch(Exception erro)
        
{}
        
this.append("新建记事",img1);
        
this.append("浏览条目",img2);
        
this.append("系统设置",img3);
        
this.append("程序说明",img4);
        
this.append("退出程序",img5);    
        
this.setCommandListener(this);
    }

    
    
public void commandAction(Command c,Displayable s)
    
{     
        
if (c==List.SELECT_COMMAND)
        
{
            List tmp
=(List) s;
            
int selected=tmp.getSelectedIndex();
            String list
=tmp.getString(selected);
            
if(list.equals("新建记事"))
            
{
                CreatNewTextScreen newTextScreen
=new CreatNewTextScreen(display,MIDlet);
                display.setCurrent(newTextScreen);
            }

            
else if (list.equals("浏览条目"))
            
{
                ViewListScreen viewListScreen
=new ViewListScreen(display,MIDlet);
                display.setCurrent(viewListScreen);
            }

            
else if (list.equals("系统设置"))
            
{
                SettingScreen settingScreen
=new SettingScreen(display,MIDlet);
                display.setCurrent(settingScreen);
            }

            
else if (list.equals("程序说明"))
            
{
                InfoScreen infoScreen
=new InfoScreen(display,MIDlet);
                display.setCurrent(infoScreen);
            }

            
else if (list.equals("退出程序"))
            
{
                MIDlet.notifyDestroyed();
            }

        }

    }

}

//-----------------------------------------------------
// NotePadMIDlet.java
//-----------------------------------------------------

    package
 src;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class NotePadMIDlet extends MIDlet {

    
private Display display;
    
public NotePadMIDlet() {
        
super();
        
// TODO 自动生成构造函数存根
        display=Display.getDisplay(this);
        FlashScreen flashScreen
=new FlashScreen(display,this);
        display.setCurrent(flashScreen);        
    }


    
protected void startApp() throws MIDletStateChangeException {
        
// TODO 自动生成方法存根

    }


    
protected void pauseApp() {
        
// TODO 自动生成方法存根

    }


    
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        
// TODO 自动生成方法存根

    }


}

//-----------------------------------------------------
// RMSUtil .java
//-----------------------------------------------------

    package
 src;
import javax.microedition.rms.*;
//数据库操作公共类
public class RMSUtil 
{
    
public static RecordStore openRSAnyway(String rsname)
    
{
        RecordStore rs
=null;
        
if(rsname.length()>32)
            
{
                
return null;
            }

        
else
        
{
            
try
            
{
                rs
=RecordStore.openRecordStore(rsname,true);
                
return rs;
            }

            
catch (Exception erro)
            
{
                
return null;
            }

        }

    }

    
    
    
public static RecordStore openRSExisted(String rsname)
    
{
        RecordStore rs
=null;
        
if (rsname.length()>32)
        
{
            
return null;
        }

        
else
        
{
            
try
            
{
                rs
=RecordStore.openRecordStore(rsname,false);
                
return rs;
            }

            
catch(Exception erro)
            
{
                
return null;
            }

        }

    }

    
    
    
public static boolean deletRS(String rsname)
    
{
        
if(rsname.length()>32)
        
{
            
return false;
        }

        
else
        
{
            
try
            
{
                RecordStore.deleteRecordStore(rsname);
                
return true;
            }

            
catch(Exception erro)
            
{
                
return false;
            }

        }

    }

    
    
}


//----------------------------------------
    // SettingDataEntity.H
    //----------------------------------------
    package src;
import java.io.*;

public class SettingDataEntity 
{
    
public boolean usePassword;
    
public String password;
    
    
public SettingDataEntity(boolean usePassword,String password)
    
{
        
this.usePassword=usePassword;
        
this.password=password;
    }

    
    
public SettingDataEntity()
    
{}
    
    
public byte[] encode()
    
{
        
byte[] bytData=null;
        
try
        
{
            ByteArrayOutputStream bos
=new ByteArrayOutputStream();
            DataOutputStream dos
=new DataOutputStream(bos);
            dos.writeBoolean(usePassword);
            dos.writeUTF(password);
            bytData
=bos.toByteArray();
            dos.close();
            bos.close();
        }

        
catch(Exception erro)
        
{}
        
return bytData;        
    }

    
    
public void decode(byte[] bytData)
    
{
        
try
        
{
            ByteArrayInputStream bis
=new ByteArrayInputStream(bytData);
            DataInputStream dis
=new DataInputStream(bis);
            usePassword
=dis.readBoolean();
            password
=dis.readUTF();
            dis.close();
            bis.close();
        }

        
catch(Exception erro)
        
{}
    }

    
}

//-----------------------------------------------------
//SettingScreen .java
//-----------------------------------------------------

    package src;
import javax.microedition.lcdui.
*;
import javax.microedition.rms.
*;
public class SettingScreen extends Form implements CommandListener 
{
    
private Display display;
    
private NotePadMIDlet MIDlet;
    
private Displayable me=this;//定义此变量是为了在程序中传值
    private boolean settingDataExist;
    
private boolean recordUsePassword;
    
private ChoiceGroup usePasswordCG;
    
    
public SettingScreen(Display display,NotePadMIDlet MIDlet)
    
{
        super(
"设置");
        
this.display=display;
        
this.MIDlet=MIDlet;
        usePasswordCG
=new ChoiceGroup("密码保护",ChoiceGroup.EXCLUSIVE);
        usePasswordCG.append(
"",null);
        usePasswordCG.append(
"",null);
        readSettingData();
        
if(recordUsePassword==true)//
        {
            usePasswordCG.setSelectedIndex(
0,true);
        }

        
else
        
{
            usePasswordCG.setSelectedIndex(
1,true);
        }

        
this.append(usePasswordCG); 
        PasswordItem setPassword
=new PasswordItem();        
        
this.append(setPassword);
        
this.addCommand(new Command("保存",Command.OK,0));
        
this.addCommand(new Command("返回",Command.BACK,0));
        
this.setCommandListener(this);

    }

    
    
public void commandAction(Command c,Displayable disp)
    
{
        String cmd
=c.getLabel();        
        
if(cmd.equals("保存"))
        
{
            saveSettingData();
        }

        
else if(cmd.equals("返回"))
        
{
            ListScreen ls
=new ListScreen(display,MIDlet);
            display.setCurrent(ls);
        }

    }

    
    
private void saveSettingData()
    
{
        SettingDataEntity sde;
        String dbname
="NotePadSettingDateBase";
        RecordStore rs
=RMSUtil.openRSAnyway(dbname);
        
if(rs==null)//不能打开数据库
        {
            Alert al
=new Alert("");
            al.setString(
"无法打开数据库!");
            al.setTimeout(Alert.FOREVER);
            display.setCurrent(al);
        }

        
else//数据库顺利打开
        {            
            
try
            
{
                RecordEnumeration re
=rs.enumerateRecords(null,null,false);
                
if(re.numRecords()!=0)//记录存在
                {
                    
int id;
                    id
=re.nextRecordId();
                    sde
=new SettingDataEntity();
                    sde.decode(rs.getRecord(id));
//取出原来的数据,主要是因为密码在此画面没有,但要写进去
                    if(usePasswordCG.getSelectedIndex()==0)
                    
{    
                        sde.usePassword
=true;
                    }

                    
else if(usePasswordCG.getSelectedIndex()==1)
                    
{    
                        sde.usePassword
=false;
                    }

                    
byte[] writeData=sde.encode();
                    rs.setRecord(id,writeData,
0,writeData.length);        
                    
                    Alert al
=new Alert("");
                    al.setString(
"设置已保存!");
                    al.setTimeout(Alert.FOREVER);
                    ListScreen ls
=new ListScreen(display,MIDlet);
                    display.setCurrent(al,ls);
                }

                
else//记录不存在
                {                                    
                    
if(usePasswordCG.getSelectedIndex()==0)
                    
{    
                        
//密码不存在又要密码保护,则直接转到编辑密码画面
                        EditPasswordScreen eps=new EditPasswordScreen(display,me);
                        
                        display.setCurrent(eps);
                    }

                    
else if(usePasswordCG.getSelectedIndex()==1)
                    
{    
                        sde
=new SettingDataEntity();
                        sde.usePassword
=false;
                        sde.password
=null;
                        
byte[] writeData=sde.encode();
                        rs.addRecord(writeData,
0,writeData.length);
                        
                        Alert al
=new Alert("");
                        al.setString(
"设置已保存!");
                        al.setTimeout(Alert.FOREVER);
                        ListScreen ls
=new ListScreen(display,MIDlet);
                        display.setCurrent(al,ls);
                    }

                }

                rs.closeRecordStore();
            }

            
catch(Exception erro)
            
{}
        }

    }

    
    
private void readSettingData()
    
{
        SettingDataEntity sde;
        String dbname
="NotePadSettingDateBase";
        RecordStore rs
=RMSUtil.openRSAnyway(dbname);
        
if(rs==null)//不能打开数据库
        {
            Alert al
=new Alert("");
            al.setString(
"无法打开数据库!");
            al.setTimeout(Alert.FOREVER);
            display.setCurrent(al);
        }

        
else//数据库顺利打开
        {            
            
try
            
{
                RecordEnumeration re
=rs.enumerateRecords(null,null,false);
                
if(re.numRecords()!=0)//记录存在
                {
                    
int id;
                    id
=re.nextRecordId();
                    sde
=new SettingDataEntity();
                    sde.decode(rs.getRecord(id));
                    
if(sde.usePassword=true)
                    
{
                        recordUsePassword
=true;
                    }

                    
else
                    
{
                        recordUsePassword
=false;
                    }

                }

                
else//记录不存在
                {                                    
                    recordUsePassword
=false;
                }

                rs.closeRecordStore();
            }

            
catch(Exception erro)
            
{}
        }

    }

    
    
//定义这个类是为了方便进行按钮操作
    public  class PasswordItem extends StringItem implements ItemCommandListener 
    
{
        
public PasswordItem()
        
{
            super(
"","设置或更改密码",Item.PLAIN);
            
this.setDefaultCommand(new Command("更改密码",Command.OK,0));
            
this.setItemCommandListener(this);
        }

        
public void commandAction(Command c,Item i)
        
{
            String cmd
=c.getLabel();
            
if(cmd.equals("更改密码"))
            
{
                
//System.out.println("设置!");
                EditPasswordScreen eps=new EditPasswordScreen(display,me);
                display.setCurrent(eps);
            }

        }

    }


}

//-----------------------------------------------------
//TestPasswordScreen  .java
//-----------------------------------------------------

    package
 src;

import javax.microedition.rms.*;
import javax.microedition.lcdui.*;
public class TestPasswordScreen extends Form implements CommandListener
{
    
private Display display;
    
private NotePadMIDlet MIDlet;
    
private TextField passwordField;
    
private int testNum=0;
    
private String recordPassword;
    
private boolean usePassword;
    
    
public TestPasswordScreen(Display display,NotePadMIDlet MIDlet)
    
{
        
super("");
        
this.display=display;
        
this.MIDlet=MIDlet;
        readSetting();
        
if(usePassword==true)
        
{
            passwordField
=new TextField("请在下面方框输入密码:","",20,TextField.PASSWORD);
            
this.append(passwordField);
            
this.addCommand(new Command("确定",Command.OK,0));
            
this.addCommand(new Command("退出",Command.OK,0));
            
this.setCommandListener(this);
            display.setCurrent(
this);            
        }

        
else
        
{
            ListScreen listScreen
=new ListScreen(display,MIDlet);
            display.setCurrent(listScreen);
            
//System.out.println("KO");
        }

    }

    
    
public void commandAction(Command c ,Displayable disp)
    
{
        String cmd
=c.getLabel();        
        
if(cmd.equals("确定"))
        
{
            testpassword();
        }

        
else if(cmd.equals("退出"))
        
{}
    }

    
    
private void readSetting()
    
{
        String dbname
="NotePadSettingDateBase";
        RecordStore rs
=RMSUtil.openRSExisted(dbname);
        
try
        
{
            
if(rs==null)
            
{
                usePassword
=false;
            }

            
else
            
{
                
int id;
                SettingDataEntity settingData
=new SettingDataEntity();
                RecordEnumeration re
=rs.enumerateRecords(null,null,false);
                id
=re.nextRecordId();
                settingData.decode(rs.getRecord(id));
                usePassword
=settingData.usePassword;
                recordPassword
=settingData.password;                
            }

            rs.closeRecordStore();
        }

        
catch(Exception erro)
        
{}        
    }

    
    
private void testpassword()
    
{
        
if(recordPassword.equals(passwordField.getString()))
        
{
            ListScreen listScreen
=new ListScreen(display,MIDlet);
            display.setCurrent(listScreen);
        }

        
else
        
{
            testNum
++;
            
int num=3-testNum;
            
if(testNum<3)
            
{
                Alert al
=new Alert("密码错误");
                al.setString(
"您还有"+num+"次输入机会,超过此次数程序将自动关闭");
                al.setTimeout(Alert.FOREVER);
                passwordField.setString(
"");
                display.setCurrent(al);
            }

            
else
            
{
                MIDlet.notifyDestroyed();
            }

        }

    }

    
}



    //-----------------------------------------------------
//ViewContentScreen
.java
//-----------------------------------------------------

    package
 src;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

public class ViewContentScreen extends Form implements CommandListener
{
    
private Display display;
    
private NotePadMIDlet MIDlet;
    
private int recordID;
    
private String title;
    
private String date;
    
private String text;
    
    
    
public ViewContentScreen(Display display,NotePadMIDlet MIDlet,int recordID)
    
{
        
super("");
        
this.display=display;
        
this.MIDlet=MIDlet;
        
this.recordID=recordID;
        readRecordData();
        
this.append("标题:"+title+" 修改日期:"+date+" 内容:"+text);
        
this.addCommand(new Command("返回",Command.BACK,0));
        
this.addCommand(new Command("编辑",Command.OK,0));
        
this.setCommandListener(this);
    }

    
    
private void readRecordData()
    
{
        String dbname
="NotePadDataBase";
        RecordStore rs
=RMSUtil.openRSExisted(dbname);
        
if(rs==null)
        
{
            
this.append("打开数据库失败!可能尚未储存数据,请进行游戏后再尝试.如果依然出现错误,请与我联系:norains@163.com");
        }

        
else
        
{    
            
try
            
{
                ContentDataEntity recordData
=new ContentDataEntity ();
                recordData.decode(rs.getRecord(recordID));
                title
=recordData.title;
                date
=recordData.date;
                text
=recordData.text;                
                rs.closeRecordStore();
            }

            
catch(Exception erro)
            
{}            
        }
    
    }

    
    
public void commandAction(Command c,Displayable disp)
    
{
        String cmd
=c.getLabel();
        
if(cmd.equals("返回"))
        
{
            ViewListScreen viewListScreen
=new ViewListScreen(display,MIDlet);
            display.setCurrent(viewListScreen);
        }

        
else if(cmd.equals("编辑"))
        
{
            EditContentScreen editContentScreen
=new EditContentScreen(display,MIDlet,recordID);
            display.setCurrent(editContentScreen);
        }

    }

}


//-----------------------------------------------------
//ViewListScreen
.java
//-----------------------------------------------------

    package
 src;
import javax.microedition.lcdui.*;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
public class ViewListScreen extends Form implements CommandListener
{
    
private Display display;
    
private NotePadMIDlet MIDlet;
    
private StringList[] stringList;

    
public ViewListScreen(Display display,NotePadMIDlet MIDlet)
    
{
        
super("");
        
this.display=display;
        
this.MIDlet=MIDlet;        
        
this.addCommand(new Command("返回",Command.BACK,0));
        
this.setCommandListener(this);
        creatList();
    }

    
    
private void creatList()
    
{
        String dbname
="NotePadDataBase";
        RecordStore rs
=RMSUtil.openRSAnyway(dbname);
        
if(rs==null)
        
{
            
this.append("打开数据库失败!可能尚未储存数据,请进行游戏后再尝试.如果依然出现错误,请与我联系:norains@163.com");
        }

        
else
        
{
            
int recordID;            
            
try
            
{
                ContentDataEntity recordData
=new ContentDataEntity ();
                RecordEnumeration re
=rs.enumerateRecords(null,null,false);
                
int i;
                stringList
=new StringList[re.numRecords()];
                
if(re.numRecords()!=0)
                
{
                    
for(i=0;i<re.numRecords();i++)
                    
{
                        recordID
=re.nextRecordId();    
                        recordData.decode(rs.getRecord(recordID));
                        stringList[i]
=new StringList(recordData.title,recordID);    
                        
this.append(stringList[i]);
                        
//System.out.println(stringList[i].recordID);
                    }

                }

                
else
                
{
                    
this.append("尚未存储记事!");
                }

                rs.closeRecordStore();
                
//RMSUtil.deletRS(dbname);
            }

            
catch(Exception erro)
            
{}            
        }
    
    }

    
    
public void commandAction(Command c,Displayable disp)
    
{
        String cmd
=c.getLabel();
        
if(cmd.equals("返回"))
        
{
            ListScreen listScreen
=new ListScreen(display,MIDlet);
            display.setCurrent(listScreen);
        }

    }

    
    
private class StringList extends StringItem implements ItemCommandListener
    
{
        
public String title;
        
public int recordID;
        
        
public StringList(String title,int recordID)
        
{
            
super("",title,Item.PLAIN);
            
this.title=title;
            
this.recordID=recordID;
            
this.setLayout(Item.LAYOUT_NEWLINE_AFTER);
            
this.setDefaultCommand(new Command("查看",Command.OK,0));
            
this.addCommand(new Command("编辑",Command.OK,1));
            
this.addCommand(new Command("删除",Command.OK,2));
            
this.addCommand(new Command("取消",Command.OK,3));
            
//this.addCommand(new Command("返回",Command.OK,4)); 在主画面已经有一个"返回",这里可以不要
            this.addCommand(new Command("退出",Command.OK,6));
            
this.setItemCommandListener(this);
        }

        
        
public void deleteRecord(int id)
        
{
            String dbname
="NotePadDataBase";
            RecordStore rs
=RMSUtil.openRSExisted(dbname);
            
try
            
{
                
if(rs==null)
                
{
                    Alert al
=new Alert("");
                    al.setString(
"出现错误,打开数据库!请与我联系:norains@163.com");
                    al.setType(AlertType.ERROR);
                    al.setTimeout(Alert.FOREVER);
                    display.setCurrent(al);
                }

                
else
                
{
                    rs.deleteRecord(id);
                }

                rs.closeRecordStore();
            }
            
            
catch(Exception erro)
            
{}        
        }
    
        
        
public void commandAction(Command c,Item i)
        
{
            String cmd
=c.getLabel();        
            StringList stringList
=(StringList) i;
            
if(cmd.equals("查看"))
            
{
                ViewContentScreen viewContentScreen
=new ViewContentScreen(display,MIDlet,stringList.recordID);
                display.setCurrent(viewContentScreen);
            }

            
else if(cmd.equals("编辑"))
            
{
                EditContentScreen editContentScreen
=new EditContentScreen(display,MIDlet,stringList.recordID);
                display.setCurrent(editContentScreen);
            }

            
else if(cmd.equals("删除"))
            
{
                deleteRecord(stringList.recordID);
                ViewListScreen viewListScreen
=new ViewListScreen(display,MIDlet);
                display.setCurrent(viewListScreen);
            }

            
else if(cmd.equals("取消"))
            
{
                
//不编写代码可退出选择菜单
            }

            
else if(cmd.equals("返回"))
            
{
                ListScreen listScreen
=new ListScreen(display,MIDlet);
                display.setCurrent(listScreen);
            }

            
else if(cmd.equals("退出"))
            
{
                MIDlet.notifyDestroyed();
            }

        }

    }


    
}
posted @ 2007-01-28 00:04  我的一天  阅读(366)  评论(0编辑  收藏  举报