程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> J2ME練筆notebook

J2ME練筆notebook

編輯:J2ME

/*總感覺手機上的記事本不好用,要記點隱私的都不行,於是乎就實踐一把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 ;

  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 ;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved