zoukankan      html  css  js  c++  java
  • J2ME练笔notebook

    /*总感觉手机上的记事本不好用,要记点隐私的都不行,于是乎就实践一把J2ME,自己写个记事本,主要是带加密功能的。。。。把课本上的东西几乎都涵盖进去了。。。本身没什么技术含量,把commandAction方法的逻辑能清楚就OK了。。。
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */

    package Window;

    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import java.util.*;
    import javax.microedition.io.file.*;
    import java.io.*;
    import javax.microedition.io.*;
    import javax.microedition.rms.* ;
    /**
     * @author mant
     */
    public class myNote extends MIDlet  implements CommandListener{
         private Display display;
         //Form fPassword;
         List list;
         String selectTitle="";
         int[] ids ;
         //private TextBox tb=new TextBox("编辑","",100000,TextField.ANY);
         noteSave ns=new noteSave();
         Form f;
         TextField tfTitle;
        TextBox tfRecData;
        TextBox tbModify;
        Alert alert;
        Form frmInfo;
        Form fPassword;
        passwordSave ps=new passwordSave();
        String password;
        TextField tfpassword;
        int flag=0;
         public myNote()
         {
             display = Display.getDisplay(this);
             tfpassword=new TextField("","",20,TextField.ANY);
             password=ps.getPassword();
             if(password.equals("nopassword"))
             {
                // fPassword.append("你还没有设置密码!");
                 flag=1;
                 tfpassword.setLabel("设置密码");
             }
             else
             {
                tfpassword.setLabel("输入密码");
             }
         }
         public void run()
         {
                     list=new List("所有记录",Choice.IMPLICIT);
            Ticker tk=new Ticker("Miss U...Everyday :)");
            list.setTicker(tk);

            Command cmd_Exit = new Command("Exit",Command.EXIT,1);
           
            Command cmd_Add=new Command("添加",Command.OK,1);
            Command cmd_Change = new Command("查看",Command.OK,1);
            Command cmd_Delete=new Command("删除",Command.OK,1);
            Command cmd_Info=new Command("信息",Command.OK,1);
            list.setSelectCommand(cmd_Add);
            list.addCommand(cmd_Exit);
            list.addCommand(cmd_Change);
            list.addCommand(cmd_Delete);
            list.addCommand(cmd_Info);
            list.setCommandListener(this);
            this.readAll();
            display.setCurrent(list);
         }
        public void startApp() {
                fPassword=new Form("登录");
                Command c=new Command("OK",Command.OK,1);
                Command cexit=new Command("Exit",Command.EXIT,1);
                fPassword.append(tfpassword);
                fPassword.addCommand(c);
                fPassword.addCommand(cexit);
                fPassword.setCommandListener(this);
                display.setCurrent(fPassword);
        }
            private void readAll() {
            list.deleteAll();

            try {
                RecordStore rs = RecordStore.openRecordStore("DB", true);
                if(rs != null ) {
                    RecordEnumeration re = rs.enumerateRecords(null, null, false);
                    //首先获得记录ID的数组
                    ids = new int[re.numRecords()];
                    int i = 0;
                    while(re.hasNextElement()) {
                        ids[i++] = re.nextRecordId();
                    }
                   
                    //获得记录的值
                    re.reset();
                    i = 0;
                    while(re.hasNextElement() ){
                        byte[] data = re.nextRecord();
                        ns.readCode(data);
                        if (data != null) {
                            StringBuffer sb = new StringBuffer();
                            //添加记录ID
                            //sb.append("[" + ids[i++] + "]  ");
                            sb.append(ns.noteTitle);
                            //添加到List中
                            list.append(sb.toString(), null);
                            System.out.println(sb);
                        }
                    }
                } else {
                    System.out.println( "没有记录" );
                }
                rs.closeRecordStore();
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        public void commandAction(Command c,Displayable s){
            String cmd = c.getLabel();
            String title="";
            if(cmd.equals("继续"))
            {
                tfRecData = new TextBox("日记","",100000,TextField.ANY);
                 Command c1 = new Command("确定",Command.EXIT,1);
                 Command c2 = new Command("退出",Command.BACK,1);
                 tfRecData.addCommand(c1);
                 tfRecData.addCommand(c2);
                tfRecData.setCommandListener(this);
                display.setCurrent(tfRecData);
            }
            else if (cmd.equals("添加")){
                f=new Form("添加记录");
                //f.
                tfTitle=new TextField("记录名","",20,TextField.ANY);
                tfRecData = new TextBox("日记","",100000,TextField.ANY);
               f.append("记录数据");
                f.append(tfTitle);
                Command cc = new Command("继续",Command.EXIT,1);
                Command ccc=new Command("退出",Command.BACK,1);
    //             tfRecData.addCommand(c1);
                f.addCommand(cc);
                f.addCommand(ccc);
                 f.setCommandListener(this);
                 //tfRecData.setCommandListener(this);
                display.setCurrent(f);
                     }
            else if(cmd.equals("确定"))
            {
                if(c.getCommandType()==Command.EXIT)
                {
                //this.add();
                    this.save(tfRecData.getString(),tfTitle.getString());
                this.readAll();
               
                }
                else if(c.getCommandType()==Command.BACK)
                {
                    this.modify();
                    this.readAll();
                }
                else if(c.getCommandType()==Command.HELP)
                {
                    //System.out.print("delete");
                    this.delete();
                     this.readAll();
                }
                display.setCurrent(list);
            }
            else if(cmd.equals("查看"))
            {
               // String index=Integer.toString(ids[list.getSelectedIndex()]) ;
              
                int indexInt=ids[list.getSelectedIndex()];
                title="查看记录"+":"+list.getString(list.getSelectedIndex());
                tbModify=new TextBox(title,"",100000,TextField.ANY);
                tbModify.setString(this.read(indexInt));
                Command cmd_OK=new Command("确定",Command.BACK,1);
                Command cmdext=new Command("退出",Command.BACK,1);
                tbModify.addCommand(cmd_OK);
                tbModify.addCommand(cmdext);
                tbModify.setCommandListener(this);
                display.setCurrent(tbModify);
            }
            else if(cmd.equals("删除"))
            {
                Command al_EXIT=new Command("退出",Command.CANCEL,1);
                Command al_OK=new Command("确定",Command.HELP,1);
               // String index=Integer.toString(ids[list.getSelectedIndex()]);
                title="删除记录"+":"+list.getString(list.getSelectedIndex());
                  alert=new Alert(title);
                  alert.setType(AlertType.WARNING);
                  //alert.setTimeout(Alert.);
                  alert.addCommand(Alert.DISMISS_COMMAND);
                  alert.addCommand(al_OK);
                  alert.addCommand(al_EXIT);
                  alert.setString("下面将要"+title+""n"+""n是否继续?");
                  alert.setCommandListener(this);
                  display.setCurrent(alert);
            }
            else if(cmd.equals("信息"))
            {
                this.showInfo();
            }
            else if(cmd.equals("退出"))
            {
                list.setCommandListener(this);
                display.setCurrent(list);
            }
            else if(cmd.equals("Exit"))
            {
                this.exitMIDlet();
            }
            else if(cmd.equals("OK"))
            {
                if(flag==1)
                 {
                     ps.setPassword(tfpassword.getString());
                     this.run();
                 }
                else if(flag==0)
                {
                    if(password.equals(tfpassword.getString()))
                    {
                        this.run();
                    }
                    else
                    {
                        tfpassword.setLabel("密码错误");
                    }
                }
            }
       
        }
        public void save(String str,String title)
        {
           RecordStore rs=noteSave.openRSAnyway("DB");
           noteSave ns=new noteSave();
           ns.noteTitle=title;
           ns.noteString=str;
           byte tmp[] = ns.insertCode();
           try
           {
           rs.addRecord(tmp,0,tmp.length) ;
           rs.closeRecordStore();
           }
           catch(Exception e)
           {
               System.out.print(e.toString());
           }
      
        }
        public String read(int index)
        {
            String str="";
           RecordStore rs=noteSave.openRSAnyway("DB");
           noteSave ns=new noteSave();
           try
           {
           byte tmp[] = rs.getRecord(index);
           ns.readCode(tmp);
           str=ns.noteString;
           rs.closeRecordStore();
           }
           catch(Exception e)
           {
               return null;
           }
          
          return str;
            //
        }
        private void modify() {
          try {
            RecordStore rs = RecordStore.openRecordStore("DB", true);
            String title = tbModify.getTitle();
            int pos = title.indexOf(":");
            int id = Integer.parseInt(title.substring(pos+1));
            byte[] data =rs.getRecord(id);
            ns.readCode(data);
            ns.noteString=tbModify.getString();
            data=ns.insertCode();
                //RecordStore rs = RecordStore.openRecordStore("DB", true);
                //添加记录监听器
                //rs.addRecordListener(new MyRecordListener());
                if( rs != null ) {
                    rs.setRecord(id, data, 0, data.length);
                }
                rs.closeRecordStore();
            } catch (Exception e) {
                System.out.println(e);
            }
        }
      private void delete() {
            //String title = alert.getTitle();
            //int pos = title.indexOf(":");
            int id =ids[list.getSelectedIndex()];
            //System.out.print("delete");
            try {
                RecordStore rs = RecordStore.openRecordStore("DB", true);
                //添加记录监听器
               // rs.addRecordListener(new MyRecordListener());
                if( rs != null ) {
                    rs.deleteRecord(id);
                }
                rs.closeRecordStore();
            }catch (Exception e) {
                System.out.println(e);
            }
        }
      private void showInfo()
      {
                frmInfo = new Form("显示记录存储信息");
                Command cmdBack=new Command("退出",Command.CANCEL,1);
                frmInfo.addCommand(cmdBack);
                frmInfo.setCommandListener(this);
                try {
                    RecordStore rs = RecordStore.openRecordStore( "DB", true);
                    int recNums = rs.getNumRecords();
                    frmInfo.append("总记录数:" + recNums +""n");
                    frmInfo.append("可利用空间:" + rs.getSizeAvailable() + ""n");
                    frmInfo.append("下一个记录ID:" + rs.getNextRecordID() + ""n");
                    frmInfo.append("版本:" + rs.getVersion() + ""n");
                } catch (Exception e) {
                    System.out.println(e);
                }
                display.setCurrent(frmInfo);
      }
        public void pauseApp() {
        }
    public void exitMIDlet(){

        try{

            destroyApp(false);

            notifyDestroyed();

        }catch(Exception ex){

            ex.printStackTrace();

        }

    }

        public void destroyApp(boolean unconditional) {
        }
    }
    ===================================================================================
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */

    package Window;
    import java.io.*;
    import javax.microedition.io.*;
    import javax.microedition.rms.* ;
    /**
     *
     * @author mant
     */
    public class noteSave
    {
     String noteTitle;
     String noteString;
    public byte[] insertCode()
    {
    byte[] result = null ;
    try
    {
    ByteArrayOutputStream bos =
    new ByteArrayOutputStream() ;
    DataOutputStream dos = new DataOutputStream (bos) ;
    dos.writeUTF(noteTitle) ;
    dos.writeUTF(noteString) ;
    result = bos.toByteArray() ;
    dos.close() ;
    bos.close() ;
    }catch(Exception e)
    {
    }
    return result ;
    }

    public void readCode(byte[] data)
    {
    try
    {
    ByteArrayInputStream bis =
    new ByteArrayInputStream(data) ;
    DataInputStream dis = new DataInputStream (bis) ;
    noteTitle = dis.readUTF() ;
    noteString = dis.readUTF() ; 
    dis.close() ;
    bis.close() ;
    }catch(Exception e)
    {
    }
    }
    public static RecordStore openRSAnyway(String rsname)
    {
            RecordStore rs = null ;
            //名称大于32个字符就不接受
            if(rsname.length() > 32)
            return null ;
            try
            {
            rs = RecordStore.openRecordStore(rsname,true) ;
            return rs ;
            }catch(Exception e)
            {
            return null ;
        }
    }

    }
    =====================================================================================
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */

    package Window;
    import javax.microedition.rms.* ;
    import java.io.*;
    import javax.microedition.io.*;
    import javax.microedition.io.file.*;
    /**
     *
     * @author mant
     */
    public class passwordSave {
        RecordStore rs;
        public passwordSave()
        {
            this.openRSAnyway("password");
        }
        public String getPassword()
        {
            String str="";
            //byte[] data=rs.getRecord(1);
        try
        {
        ByteArrayInputStream bis =
        new ByteArrayInputStream(rs.getRecord(1)) ;
        DataInputStream dis = new DataInputStream (bis) ;
        str = dis.readUTF() ;
        //noteString = dis.readUTF() ; 
        dis.close() ;
        bis.close() ;
        }catch(Exception e)
        {
            return str="nopassword";
        }
            return str;
        }
        public void setPassword(String password)
        {
            byte[] data = null ;
            try
            {
            ByteArrayOutputStream bos =
            new ByteArrayOutputStream() ;
            DataOutputStream dos = new DataOutputStream (bos) ;
            dos.writeUTF(password) ;
            //dos.writeUTF(noteString) ;
            data = bos.toByteArray() ;
            dos.close() ;
            bos.close() ;
            rs.addRecord( data, 0, data.length);
            }catch(Exception e)
            {
            }
           // return result;
        }
        public  void openRSAnyway(String rsname)
    {
           // RecordStore rs = null ;
            //名称大于32个字符就不接受
            if(rsname.length() > 32)
                return;
            try
            {
            rs = RecordStore.openRecordStore(rsname,true) ;
           // return rs ;
            }catch(Exception e)
            {
            return ;
        }
    }

    }

    QQ群:1022985150 VX:kklldog 一起探讨学习.NET技术
    作者:Agile.Zhou(kklldog)
    出处:http://www.cnblogs.com/kklldog/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    KubeSphere 社区开源负载均衡器 Porter 进入 CNCF 云原生全景图
    The Overview of KubeSphere 3.0
    对象存储在无人驾驶高精度地图的场景实践
    谁来打通混合云“最后一公里”?唯有容器混合云
    QingStor 对象存储架构设计及最佳实践
    如何通过IAM打造零信任安全架构
    智能家居巨头 Aqara 基于 KubeSphere 打造物联网微服务平台
    微信小程序集成jenkins自动打码
    windows运行python,提示import win32file ImportError: DLL load failed: 找不到指定的程序。
    centos7搭建easy-mock服务
  • 原文地址:https://www.cnblogs.com/kklldog/p/1287773.html
Copyright © 2011-2022 走看看