做手机的程序开发,肯定就会涉及到图形处理方面的问题咯。与事务处理一样,MIDP将图形介面处理
也分成了高级图形介面处理与低级图形介面处理。同样,高级图形介面处理方法是可以方便移值的,但是
功能有限,低级图形介面功能强大,但是再移值以后可能会需要更改部分代码。高级图形介面都需要继承
Screen,而低级图形介面则继承Canvas,并大量使用Graphics所定义的图形处理方法。MIDP所使用的图形
介面类都属于前面代码中都用过的javax.microedition.lcdui这个package里面。在同一时间(只是同一
时间)内Canvas和Scree的子类只能存在一个。 Screen类中有四个子类,Alert,List,TextBox,Form。其中Form是属于没有任何使用介面的元素,
它只是一个容器,可以容纳其他Item类在其中用来组合一个复杂的图形使用介面。而其他几个子类则属于
封装了预定的固定图形介面,只能单独的拿来使用。这个其实就和HTML里面各种输入框与Form之间的关系
一样。 呵呵,和以前学其他开发语言一样,一个个的来测试这些子类的各种使用方法。^_^用实际第程序来
说明之间第关系和之间第区别。 一、List 1)Choice.IMPLICIT
package com.graph;
/** * @author You Li * * CopyRight (C) 2005 */ import javax.microedition.lcdui.*; import javax.microedition.midlet.*;
public class IMPListTest extends MIDlet implements CommandListener {
private Display display; public IMPListTest() { // TODO 自动生成构造函数存根 display = Display.getDisplay(this); }
protected void startApp() throws MIDletStateChangeException { // TODO 自动生成方法存根 List aList = new List("ListTest",Choice.IMPLICIT) ; aList.append(" A ",null); aList.append(" B ",null); aList.append(" C ",null); aList.append(" D ",null); aList.setCommandListener(this); display.setCurrent(aList); }
protected void pauseApp() { // TODO 自动生成方法存根
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException { // TODO 自动生成方法存根
}
public void commandAction(Command command, Displayable display) { // TODO 自动生成方法存根 System.out.println("Command action start ... ..."); if(command==List.SELECT_COMMAND){ List test= (List) display ; int selected = test.getSelectedIndex() ; System.out.println(" Item " + selected + "selected") ; } }
}
当我们运行这个程序的时候会发现屏幕上出现4个选项,答应出选定的选项的indexid,由此可以看出
来Choice.IMPLICIT的选项被选中,它会马上使用setCommandListener()方法注册内容,并同时呼叫comma
dnAction()方法。同时通过commadnAction()方法的第一个参数判断是否使用的是Choice.IMPLICIT选项。
2)Choice.EXCLUSIVE
package com.graph;
/** * @author You Li * * CopyRight (C) 2005 */ import javax.microedition.midlet.*; import javax.microedition.lcdui.*;
public class EXCListTest extends MIDlet implements CommandListener {
private Display display; Command commit ; public EXCListTest() { // TODO 自动生成构造函数存根 display = Display.getDisplay(this); }
protected void startApp() throws MIDletStateChangeException { // TODO 自动生成方法存根 commit = new Command("提交",Command.SCREEN,1) ; List alist = new List("ListTest",Choice.EXCLUSIVE) ; alist.append(" A ",null) ; alist.append(" B ",null) ; alist.append(" C ",null) ; alist.addCommand(commit) ; alist.setCommandListener(this) ; display.setCurrent(alist) ; }
protected void pauseApp() { // TODO 自动生成方法存根
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException { // TODO 自动生成方法存根
}
public void commandAction(Command command, Displayable display) { // TODO 自动生成方法存根 System.out.println("Commadn action start... ..."); if(command == commit){ List tmp = (List) display ; int selected = tmp.getSelectedIndex() ; System.out.println("Item " + selected + "selected") ; } } }
运行这个程序我们会发现Choice.EXCLUSIVE和Choice.IMPLICIT之间第区别了。Choice.EXCLUSIVE不
会在我们选择以后马上运行commandAction()方法,我们只能commit的系统选项菜单来完成最后第提交。
当我们点击系统菜单的commit以后会执行commandAction()方法。
3)、Choice.MULTIPLE
/** * @author You Li * * CopyRight (C) 2005 */ import javax.microedition.midlet.*; import javax.microedition.lcdui.*;
public class MULTListTest extends MIDlet implements CommandListener {
private Display display; Command commit; public MULTListTest() { display = Display.getDisplay(this); }
protected void startApp() throws MIDletStateChangeException { // TODO 自动生成方法存根 commit = new Command("提交",Command.SCREEN,1); List alist = new List("ListTest",Choice.MULTIPLE); alist.append(" A ",null) ; alist.append(" B ",null) ; alist.append(" C ",null) ; alist.addCommand(commit); alist.setCommandListener(this); display.setCurrent(alist); }
protected void pauseApp() { // TODO 自动生成方法存根
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException { // TODO 自动生成方法存根
}
public void commandAction(Command command, Displayable display) { // TODO 自动生成方法存根 System.out.println("Commadn action start... ..."); if(command == commit){ List tmp = (List) display; int num = tmp.size(); for(int i = 0;i< num;i++){ if(tmp.isSelected(i)){ System.out.println("Item " + i + "selected"); } } } }
}
这个程序里面我们使用了Choice.MULTIPLE,也就是多选框的使用。和Choice.EXCLUSIVE一样,只有当
我们使用系统菜单commit以后才会调用commandAction()方法,来进行操作。
二、Alert和AlertType windows,还有一般页面上的对话框见多了,这个Alert其实也就是这个作用。它属于Screen的子类,
我们可以用Display累的setCurrent()方法将它显示在屏幕上,当然,当Alert设置喂屏幕画面的时候系统
本身一定要存在一个画面,不然Alert无法跳回,会抛出一个错误出来。如果我们喂Alert对象设置其Aler
tType的属性AlertType.ALARM,Alert.FOREVER以后那么需要我们点击系统确认键才会从Alert窗口返回原
始窗口。一个简单的例子如下: 只有当显示“窗口”的时候设置了AlertType.ALARM,Alert.FOREVER属性,所以需要点击“done”以
后Alert对象才会关闭。
package com.graph;
/** * @author You Li * * CopyRight (C) 2005 www.iguess.com.cn */ import javax.microedition.midlet.*; import javax.microedition.lcdui.*;
public class AlertTest extends MIDlet implements CommandListener{
private Display display; Command commit; public AlertTest() { display = Display.getDisplay(this); }
protected void startApp() throws MIDletStateChangeException { // TODO 自动生成方法存根 List aList = new List("ListTest",Choice.IMPLICIT) ; aList.append(" 窗口 ",null) ; aList.append(" 确认 ",null) ; aList.append(" 错误 ",null) ; aList.append(" 信息 ",null) ; aList.append(" 警告 ",null) ; aList.setCommandListener(this) ; display.setCurrent(aList) ; }
protected void pauseApp() { // TODO 自动生成方法存根
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException { // TODO 自动生成方法存根
} public void commandAction(Command command,Displayable dis){ if(command == List.SELECT_COMMAND){ Alert alert = new Alert("窗口测试") ; List temp = (List) dis ; switch(temp.getSelectedIndex()){ case 0 : alert.setType(AlertType.ALARM) ; alert.setString(" 窗口 ") ; alert.setTimeout(Alert.FOREVER) ; display.setCurrent(alert) ; break ; case 1 : alert.setType(AlertType.CONFIRMATION) ; alert.setString(" 确认 ") ; display.setCurrent(alert) ; break ; case 2 : alert.setType(AlertType.ERROR) ; alert.setString( "错误 ") ; display.setCurrent(alert) ; break ; case 3 : alert.setType(AlertType.INFO) ; alert.setString(" 信息 ") ; display.setCurrent(alert) ; break ; case 4 : alert.setType(AlertType.WARNING) ; alert.setString(" 警告 ") ; display.setCurrent(alert) ; break ; } } } }
11:30。。。准时上床看电视了。。呵呵,今天从早上8:00一直到现在除了吃饭其他时间都在电脑前
面。。感觉眼睛都痛了。。唉。。。工作是给老板做的,钱赚来还要还给国家,还就是身体才是自己的。
。程序员。。就tmd命苦亚。。。。休息去也。。学习是学不完的。。。
============================================ 还是希望大家多多留言,多多讨论,思想的碰撞才能有火花呀。还有,me的基础知识快要学完了,准
备搞几个小游戏练手。。如果那位兄台有意一起练手请给我发邮件。[email protected]
,告诉我你们的msn,我来加^_^。。。 
|