1.概述 MIDP为运行在MIDP容器中的MIDP应用定义了一个API,此API本身是建立在CLDC API之上的。MIDP用户接口API的Java类设计不是基于Java Abstract Window Toolkit(AWT)类的,而是为移动电话这类小型移动信息设备非凡设计的。这类设备只有有限的屏幕尺寸和键盘性能。当程序员采用MIDP编写图形应用时,他们只能使用MIDP或CLDC API。 2.MIDP GUI类 1)所有MIDP GUI类都是javax.microedition.lcdui包的组成部分。 2)MIDP用户接口的基本抽象图形是屏幕,Screen类对面向设备的图形和用户交互进行了封装。每次应用只能显示一个屏幕。 3)MIDP API具有”高级(high-level)”和”低级(low-level)”UI类。 高级UI-----例如Form,List,TextBox,TextField,Alert和Ticker具有设备适配功能,它对图象,文本,文本域以及单选按钮等进行支持。 低级UI-----例如Canvas类答应操作者任意绘画。 3. MIDP GUI中主要的类 1) Graphics类-----提供了用来绘画2D几何对象的Graphics对象。 javax.microedition.lcdui.Graphics类不是在MIDlet中显式创建的,表示抽象画面的Canvas类在MIDP GUI编程中提供了paint()方法,piant()方法取类型为Graphics的一个参数,用此参数实现MIDlet中Graphics类的功能。 2) Displayable类-----是一个抽象类 ,Displayable对象处理MIDlet的GUI输出.它有两个派生类javax.microedition.lcdui.Canvas和javax.microedition.lcdui.Screen。 3) Canvas类-----Canvas类答应操作者任意绘画,与Graphics类都是低级UI.由Graphics类类提供的方法总是在Canvas类的派生类中使用的。 4) Screen类-----是高级UI,Form,List,TextBox,Alert类都是它的派生类。 4. 下面是Graphics类用法的一段代码 import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class MidpGraphics extends MIDlet implements CommandListener { Display display; testCanvas canvas; public MidpGraphics() { display=Display.getDisplay(this); canvas=new testCanvas(); } public void startApp() throws MIDletStateChangeException { display.setCurrent(canvas); } public void pauseApp() { } public void destroyApp(boolean unconditional) throws MIDletStateChangeException { } public void commandAction(Command c,Displayable d) { } } class testCanvas extends Canvas { public void paint(Graphics g) { //创建字体对象 Font font=Font.getFont (Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_MEDIUM); //设定字体对象 g.setFont(font); //绘画出文本 g.drawString("Hello MIDP",getWidth()/2,getHeight ()/2,Graphics.HCENTERGraphics.TOP); try { //绘画出图像 Image image=Image.createImage("myimage.png"); g.drawImage(image,0,0,Graphics.HCENTERGraphics.TOP); } catch(Exception e) { } } } 运行效果就是在屏幕的中心显示文本”Hello MIDP”。 5.用MIDlet编程来实现SaveMyMoney银行应用的不同图形用户界面实例代码: 1) 任务陈述:第一屏幕显示包含名为Balance Enquiry(当前余额),Fixed Deposit Enquiry(定期储蓄),Check Status Enquiry(支票状况)项的菜单 当用户选择Balance Enquiry应显示进度指示器 当用户选择Fixed Deposit Enquiry应显示进度指示器 当用户选择Check Status Enquiry应显示打入支票号的TextBox 当用户打入支票号后应显示进度指示器 2.代码如下: // 导入lcdui和Midlet包 import javax.microedition.lcdui.*; import javax.microedition.midlet.*; // 定义扩展MidpGui类的MIDlet类 public class MidpGui extends MIDlet implements CommandListener { // 由Display类创建显示治理器 Display display; // 定义窗体对象 Form form = new Form("Container Form"); // Screen类派生的 // 定义列表(主菜单) List menu; // 定义文本框 TextBox input; // Item类的组件 (Gauge类表示显示屏幕上的一个条形图) Gauge gauge = new Gauge("Your enquiry is being processed", false, 100, 30); // 定义来自Command类的命令 static final Command okCommand = new Command("OK",Command.OK,1); static final Command backCommand = new Command("Back",Command.BACK,0); static final Command exitCommand = new Command("Exit", Command.STOP,2); String currentMenu; // 定义串变量以标识当前的窗体名 public MidpGui() { } /** * The startApp() starts the MIDlet, creates a list of items and * Uses the EXIT command */ public void startApp() throws MIDletStateChangeException { // 得到display对象 display = Display.getDisplay(this); // 创建初始菜单并加入项 menu = new List("Enquiries", Choice.IMPLICIT); menu.append("Current Balance ", null); menu.append("Fixed Deposit ", null); menu.append("Check Status ", null); // 加入Exit命令到窗体 menu.addCommand(exitCommand); menu.setCommandListener(this); // 调用mainmenu方法,设置初始屏幕 mainMenu(); // 加入计量组件到窗体 form.append(gauge); } // mainMenu 方法 void mainMenu() { display.setCurrent(menu); currentMenu = "Main"; } public void pauseApp() { form = null; display = null; menu = null; input = null; gauge = null; } // 当MIDlet被撤消是调用 public void destroyApp(boolean unconditional) throws MIDletStateChangeException { notifyDestroyed(); } //显示TextBox组件 public void showTextBox() { input = new TextBox ("Enter the Check Number:", "", 20, TextField.ANY); input.addCommand(backCommand); input.addCommand(okCommand); input.setCommandListener(this); input.setString(""); display.setCurrent(input); currentMenu = "input"; } //显示进度指示器屏幕 public void showForm() { form.addCommand(backCommand); form.setCommandListener(this); display.setCurrent(form); currentMenu = "form"; } //命令激活 public void commandAction(Command c, Displayable d) { String label = c.getLabel(); if (label.equals("Exit")) { try{ destroyApp(true);} catch (
|