我是靠谱客的博主 多情水蜜桃,最近开发中收集的这篇文章主要介绍javaME 学习之-简单通讯录,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

界面说明:  欢迎界面--包含 退出按钮   电话本按钮  欢迎说明
                 主界面--显示通讯录列表    包含 返回(回到欢迎界面) 按钮    删除按钮(对列表中的记录删除)  添加按钮(跳转到添加界面)  
               添加界面--有姓名、号码文本框   保存和返回按钮

代码说明:

  1.欢迎界面 WelcomeCanvas.class
        
contractedblock.gif expandedblockstart.gif Code
 1package org;
 2
 3import javax.microedition.lcdui.Canvas;
 4import javax.microedition.lcdui.Command;
 5import javax.microedition.lcdui.CommandListener;
 6import javax.microedition.lcdui.Displayable;
 7import javax.microedition.lcdui.Font;
 8import javax.microedition.lcdui.Graphics;
 9
10expandedblockstart.gifcontractedblock.gifpublic class WelcomeCanvas extends Canvas implements CommandListener {
11
12    private Command cmdExit = new Command("退出", Command.EXIT, 1);
13    private Command cmdPhone = new Command("电话本", Command.SCREEN, 1);
14    private MainMIDlet mid;
15
16expandedsubblockstart.gifcontractedsubblock.gif    protected void paint(Graphics g) {
17
18        g.setColor(25500);
19        g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
20                Font.SIZE_LARGE));
21        String welcomeString = "欢迎使用";
22        int fontStringWidth = g.getFont().stringWidth(welcomeString);// 字长
23        int fontStringHeight = g.getFont().getHeight();// 高度
24        // 显示屏幕正中央
25        g.drawString(welcomeString, this.getWidth() / 2 - fontStringWidth / 2,
26                this.getHeight() / 2 - fontStringHeight / 2, Graphics.LEFT
27                        | Graphics.TOP);
28    }

29
30expandedsubblockstart.gifcontractedsubblock.gif    public WelcomeCanvas(MainMIDlet midDlet) {
31        this.addCommand(cmdExit);
32        this.addCommand(cmdPhone);
33        this.setCommandListener(this);
34        this.mid = midDlet;
35    }

36
37expandedsubblockstart.gifcontractedsubblock.gif    public void commandAction(Command c, Displayable d) {
38
39expandedsubblockstart.gifcontractedsubblock.gif        if (c == cmdExit) {
40            mid.notifyDestroyed();
41        }

42expandedsubblockstart.gifcontractedsubblock.gif        if (c == cmdPhone) {
43            mid.changeCurrentUI("phoneList");
44        }

45    }

46
47}

48

2.通讯列表   PhoneList.class
contractedblock.gif expandedblockstart.gif Code
 1package org;
 2
 3import javax.microedition.lcdui.Command;
 4import javax.microedition.lcdui.CommandListener;
 5import javax.microedition.lcdui.Displayable;
 6import javax.microedition.lcdui.List;
 7import javax.microedition.rms.RecordStoreException;
 8
 9expandedblockstart.gifcontractedblock.gifpublic class PhoneList extends List implements CommandListener {
10
11    private Command cmdBack = new Command("返回", Command.BACK, 1);
12    private Command cmdAdd = new Command("添加", Command.SCREEN, 1);
13    private Command cmdDelete = new Command("删除", Command.SCREEN, 2);
14    private MainMIDlet mid;
15
16expandedsubblockstart.gifcontractedsubblock.gif    public PhoneList(MainMIDlet mid) {
17        super("电话本", List.IMPLICIT);
18        this.addCommand(cmdBack);
19        this.addCommand(cmdAdd);
20        this.addCommand(cmdDelete);
21        this.setCommandListener(this);
22        this.mid = mid;
23    }

24
25expandedsubblockstart.gifcontractedsubblock.gif    public void commandAction(Command c, Displayable d) {
26         
27        //返回
28expandedsubblockstart.gifcontractedsubblock.gif        if (c == cmdBack) {
29            mid.changeCurrentUI("welcome");
30        }

31        //添加
32expandedsubblockstart.gifcontractedsubblock.gif        if (c == cmdAdd) {
33            mid.changeCurrentUI("PhoneAddForm");
34        }

35        //删除
36expandedsubblockstart.gifcontractedsubblock.gif        if (c == cmdDelete) {
37expandedsubblockstart.gifcontractedsubblock.gif            try {
38                mid.rStoreOperate.deleteRecord(this.getString(this
39                        .getSelectedIndex()));
40                
41                mid.LoadData();
42expandedsubblockstart.gifcontractedsubblock.gif            }
 catch (RecordStoreException e) {
43                // TODO Auto-generated catch block
44                e.printStackTrace();
45            }

46        }

47
48    }

49}

50

3.添加界面  PhoneNumberAddForm.class
contractedblock.gif expandedblockstart.gif Code
 1package org;
 2
 3import javax.microedition.lcdui.Command;
 4import javax.microedition.lcdui.CommandListener;
 5import javax.microedition.lcdui.Displayable;
 6import javax.microedition.lcdui.Form;
 7import javax.microedition.lcdui.StringItem;
 8import javax.microedition.lcdui.TextField;
 9import javax.microedition.rms.RecordStoreException;
10
11expandedblockstart.gifcontractedblock.gifpublic class PhoneNumberAddForm extends Form implements CommandListener {
12
13    private Command cmdAddNumber = new Command("添加号码", Command.OK, 2);
14
15    private Command cmdBack = new Command("返回", Command.BACK, 3);
16
17    private TextField txtName = new TextField("姓名"""50, TextField.ANY);
18
19    private TextField txtNumber = new TextField("号码"""50, TextField.NUMERIC);
20
21    private StringItem alertInfo = new StringItem("""");
22
23    MainMIDlet midDlet;
24
25expandedsubblockstart.gifcontractedsubblock.gif    public PhoneNumberAddForm(MainMIDlet mid) {
26        super("添加电话号码");
27        this.addCommand(cmdAddNumber);
28        this.addCommand(cmdBack);
29        this.append(txtName);
30        this.append(txtNumber);
31        this.append(alertInfo);
32        this.setCommandListener(this);
33        this.midDlet = mid;
34
35    }

36
37expandedsubblockstart.gifcontractedsubblock.gif    public void commandAction(Command c, Displayable d) {
38
39expandedsubblockstart.gifcontractedsubblock.gif        if (c == cmdBack) {
40            midDlet.changeCurrentUI("phoneList");
41        }

42expandedsubblockstart.gifcontractedsubblock.gif        if (c == cmdAddNumber) {
43expandedsubblockstart.gifcontractedsubblock.gif            try {
44
45                midDlet.rStoreOperate.addRecord(txtName.getString() + "-"
46                        + txtNumber.getString());
47
48                alertInfo.setText("添加成功!");
49                txtName.setString("");
50                txtNumber.setString("");
51
52expandedsubblockstart.gifcontractedsubblock.gif            }
 catch (RecordStoreException e) {
53                e.printStackTrace();
54                alertInfo.setText("添加失败!");
55            }

56        }

57    }

58}

59

4.主界面MIDlet
contractedblock.gif expandedblockstart.gif Code
 1package org;
 2
 3import java.util.Enumeration;
 4import java.util.Vector;
 5
 6import javax.microedition.lcdui.Display;
 7import javax.microedition.midlet.MIDlet;
 8import javax.microedition.midlet.MIDletStateChangeException;
 9import javax.microedition.rms.RecordStoreException;
10
11expandedblockstart.gifcontractedblock.gifpublic class MainMIDlet extends MIDlet {
12
13    private Display display;
14    WelcomeCanvas wc = new WelcomeCanvas(this);
15    PhoneList pList = new PhoneList(this);
16    PhoneNumberAddForm phoneNumberAddForm = new PhoneNumberAddForm(this);
17    public RecordStoreOperate rStoreOperate = new RecordStoreOperate("number");
18
19expandedsubblockstart.gifcontractedsubblock.gif    public MainMIDlet() {
20        display = Display.getDisplay(this);
21    }

22
23    protected void destroyApp(boolean unconditional)
24expandedsubblockstart.gifcontractedsubblock.gif            throws MIDletStateChangeException {
25
26    }

27
28expandedsubblockstart.gifcontractedsubblock.gif    protected void pauseApp() {
29
30    }

31
32    // /加载通讯列表
33expandedsubblockstart.gifcontractedsubblock.gif    public void LoadData() {
34expandedsubblockstart.gifcontractedsubblock.gif        try {
35            pList.deleteAll();
36            Vector vector = rStoreOperate.loadData();
37            Enumeration en = vector.elements();
38expandedsubblockstart.gifcontractedsubblock.gif            while (en.hasMoreElements()) {
39                pList.append(en.nextElement().toString(), null);
40            }

41expandedsubblockstart.gifcontractedsubblock.gif        }
 catch (RecordStoreException e) {
42            e.printStackTrace();
43        }

44
45    }

46
47expandedsubblockstart.gifcontractedsubblock.gif    protected void startApp() throws MIDletStateChangeException {
48        changeCurrentUI("welcome");
49    }

50
51expandedsubblockstart.gifcontractedsubblock.gif    public void changeCurrentUI(String name) {
52expandedsubblockstart.gifcontractedsubblock.gif        if (name.equals("phoneList")) {
53            display.setCurrent(pList);
54            LoadData();
55        }

56expandedsubblockstart.gifcontractedsubblock.gif        if (name.equals("welcome")) {
57            display.setCurrent(wc);
58        }

59expandedsubblockstart.gifcontractedsubblock.gif        if (name == "PhoneAddForm"{
60            display.setCurrent(phoneNumberAddForm);
61
62        }

63    }

64
65}

66

5.RMS操作
contractedblock.gif expandedblockstart.gif Code
 1package org;
 2
 3import java.util.Vector;
 4
 5import javax.microedition.rms.RecordEnumeration;
 6import javax.microedition.rms.RecordFilter;
 7import javax.microedition.rms.RecordStore;
 8import javax.microedition.rms.RecordStoreException;
 9import javax.microedition.rms.RecordStoreNotOpenException;
10
11expandedblockstart.gifcontractedblock.gifpublic class RecordStoreOperate implements RecordFilter {
12
13    private RecordStore rStore = null;
14
15expandedsubblockstart.gifcontractedsubblock.gif    public RecordStoreOperate(String rsName) {
16
17expandedsubblockstart.gifcontractedsubblock.gif        try {
18            rStore = RecordStore.openRecordStore(rsName, true);
19expandedsubblockstart.gifcontractedsubblock.gif        }
 catch (RecordStoreException e) {
20            e.printStackTrace();
21        }

22    }

23
24    public Vector loadData() throws RecordStoreNotOpenException,
25expandedsubblockstart.gifcontractedsubblock.gif            RecordStoreException {
26
27        Vector v = new Vector();
28
29        RecordEnumeration re = rStore.enumerateRecords(thisnullfalse);
30expandedsubblockstart.gifcontractedsubblock.gif        while (re.hasNextElement()) {
31            String str = new String(re.nextRecord());
32            v.addElement(str);
33        }

34        return v;
35    }

36
37    // /删除记录 by ID
38expandedsubblockstart.gifcontractedsubblock.gif    public void deleteRecord(int id) throws RecordStoreException {
39        rStore.deleteRecord(id);
40    }

41
42expandedsubblockstart.gifcontractedsubblock.gif    /** *//**
43     * 删除记录 注意点 删除记录后 该记录的recordId 还是保存着的 所以删除不能把记录的numRecord当做RecordId来用
44     * 我这里采取置为空字符窜" " 而且长度要大于0 如果为"" 读取的时候就会当做NULL 而且此处也并非真正意思上的删除
45     * 删除记录也不是真正意义上的删除  只是把值改成" "  读取过滤
46     * **/

47
48expandedsubblockstart.gifcontractedsubblock.gif    public void deleteRecord(String record) throws RecordStoreException {
49
50        int numRecords = rStore.getNumRecords();
51
52expandedsubblockstart.gifcontractedsubblock.gif        for (int i = 0; i < numRecords; i++{
53            byte[] b = rStore.getRecord(i + 1);
54
55expandedsubblockstart.gifcontractedsubblock.gif            if (new String(b).equals(record)) {
56
57                rStore.setRecord(i + 1" ".getBytes(), 01);
58            }

59        }

60
61    }

62
63    // 添加记录
64expandedsubblockstart.gifcontractedsubblock.gif    public void addRecord(String rsString) throws RecordStoreException {
65        byte[] b = rsString.getBytes();
66        rStore.addRecord(b, 0, b.length);
67    }

68     
69    
70expandedsubblockstart.gifcontractedsubblock.gif    /** *//**过滤 " "  意为" "是已被删除
71    **返回true 记录集中包含该条件的记录 flase返回不符此条件的记录
72    **/

73expandedsubblockstart.gifcontractedsubblock.gif     public boolean matches(byte[] value) {
74
75        return !new String(value).equals(" ");
76    }

77}

78

转载于:https://www.cnblogs.com/zhangqifeng/archive/2009/09/13/1565878.html

最后

以上就是多情水蜜桃为你收集整理的javaME 学习之-简单通讯录的全部内容,希望文章能够帮你解决javaME 学习之-简单通讯录所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部