⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainmidlet.java

📁 基于j2me的手机应用程序-电话本
💻 JAVA
字号:
/*
 * 主程序
 */
package PhoneBook;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;


public class MainMIDlet extends MIDlet implements CommandListener {
	private final static Command exitCommand = new Command("退出",Command.EXIT, 1);

	private final static Command confirmCommand = new Command("确定",Command.SCREEN, 1);

	private Display display;
	private List mainList;
	private RecordStore rs;
	

	public MainMIDlet() {
		display = Display.getDisplay(this);		

	}

	protected void startApp() {

		// 创建图像对象
		Image[] imageArray = null;

		try {
			// 读取图像资源
			Image icon = Image.createImage("/doc.png");
			// 创建图像数组
			imageArray = new Image[] { icon, icon, icon ,icon};
		} catch (java.io.IOException err) {
			
			imageArray = null;
		}

		String[] stringArray = { "添加新记录", "浏览电话簿", "查找记录" ,"删除记录"};
		mainList = new List("电话簿", Choice.IMPLICIT, stringArray,imageArray);
		mainList.addCommand(exitCommand);
		mainList.addCommand(confirmCommand);
		mainList.setCommandListener(this);
		display.setCurrent(mainList);
		
		//打开记录文件,如果不存在则创建
		try {
			rs = RecordStore.openRecordStore("MyPhoneBook", true);
		} catch (Exception e) {
			rs = null;
		}

	}

	protected void destroyApp(boolean unconditional) {
	}

	protected void pauseApp() {
	}

	public void commandAction(Command c, Displayable d) {
		if (d.equals(mainList)) {
			// 如果选择了确定按钮则判断选择了哪一个列表框
			if (c == List.SELECT_COMMAND || c == confirmCommand) {
				if (d.equals(mainList)) {
					switch (((List) d).getSelectedIndex()) {
					case 0:  
						//添加新纪录
						NewRecordForm newRF = new NewRecordForm(BookConstant.record_added,mainList,this,rs);						
						display.setCurrent(newRF);						
						
						break;

					case 1:
						//显示电话本
						ListRecord list = new ListRecord(BookConstant.listPhoneTitle,Choice.IMPLICIT,mainList,this,rs);
						display.setCurrent(list);
					
						break;

					case 2:
						FindRecord fr = new FindRecord(mainList,this,rs);
						display.setCurrent(fr);
						break;
					case 3:
						//删除记录
						DeleteRecord dr = new DeleteRecord(mainList,this,rs);
						display.setCurrent(dr);
						
						break;
					}
				}
			}
		} 					
		//如果键入退出按钮,则退出程序	
		if (c == exitCommand) {
			//关闭记录文件
			try {
				rs.closeRecordStore();
				rs = null;
			} catch (Exception e) {
			}
			
			destroyApp(false);
			notifyDestroyed();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -