阿熊
发贴: 0
积分: 0
|
于 2002-08-11 09:39
Command对象 发信站: 北大未名站 (2001年10月20日20:33:56 星期六) , 站内信件 在前面我们其实已经使用过Command对象了。J2ME的事件系统比较特殊,你必须首先定义一 系列的命令,然后注册到容器对象中,例如(Form、Alert、List、TextBox),再设定命令监听者 ,编写好commandAction()方法即可。当系统发送某个命令,便由commandAction()方法进行统 筹处理。下面的程序演示了如何定义多个命令以及如何编写commandAction()方法。 package fancy.test; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class CMD extends MIDlet implements CommandListener { private Display display; private Form props; private Command backCommand = new Command("BACK", Command.BACK, 2); private Command cancelCommand = new Command("CANCEL", Command.CANCEL, 1); private Command exitCommand = new Command("EXIT", Command.EXIT, 1); private Command helpCommand = new Command("HELP", Command.HELP, 1); private Command itemCommand = new Command("ITEM", Command.ITEM, 1); private Command okCommand = new Command("OK", Command.OK, 1); private Command screenCommand = new Command("SCREEN", Command.SCREEN, 1); private Command stopCommand = new Command("STOP", Command.STOP, 1); public CMD() { display = Display.getDisplay(this); } public void startApp() { props = new Form("Hello World"); props.append("Hello World!\n"); props.addCommand(backCommand); props.addCommand(cancelCommand); props.addCommand(exitCommand); props.addCommand(helpCommand); props.addCommand(itemCommand); props.addCommand(okCommand); props.addCommand(screenCommand); props.addCommand(stopCommand); props.setCommandListener(this); display.setCurrent(props); } public void showScreen(String cmd) { Form form=new Form("show cmd"); form.append(cmd); form.addCommand(exitCommand); form.setCommandListener(this); display.setCurrent(form); } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } else if(c==helpCommand) { showScreen("help"); } else if(c==backCommand) { showScreen("back"); } else if(c==cancelCommand) { showScreen("cancel"); } else if(c==itemCommand) { showScreen("item"); } else if(c==okCommand) { showScreen("ok"); } else if(c==screenCommand) { showScreen("screen"); } if(c==stopCommand) { showScreen("stop"); } } public void destroyApp(boolean unconditional) { } public void pauseApp() { display.setCurrent(null); props = null; } } 在上面的程序(CMD.java)中定义了八个命令。如果commandAction()方法接到这八个命令, 多半是调用showScreen()方法,将这几个命令输出。showScreen()方法会产生一个新的容器对 象(Form),作为当前的屏幕,并把截获的命令显示在屏幕中。 CMD.java的运行效果如下2图所示(当屏幕出现Hello World字样的时候,你需要按下退出键 ,命令菜单就会出现了,你可以依次执行各个命令)。 TextBox文本框对象 发信站: 北大未名站 (2001年10月20日20:36:34 星期六) , 站内信件 TextBox是一个容器类型的对象(和Form的性质一样)。用法如下所示: package fancy.test; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class ShowTextBox extends MIDlet implements CommandListener { private Display display; private TextBox txtBox; private Command exitCommand = new Command("Exit", Command.EXIT, 1); public ShowTextBox() { display = Display.getDisplay(this); } public void startApp() { //or : //String str="hello world"; //txtBox = new TextBox("Text Box",str,str.length(),0); //the follow code is wrong: //txtBox = new TextBox("Text Box",str,any number here,0); txtBox = new TextBox("Text Box",null,200,0); txtBox.addCommand(exitCommand); txtBox.setCommandListener(this); display.setCurrent(txtBox); } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } } public void destroyApp(boolean unconditional) { } public void pauseApp() { display.setCurrent(null); txtBox = null; } } 请注意TextBox类的构造函数,第一个参数实际上是窗口的名称(因为TextBox是一个容器,可 能是当前屏幕的显示对象),第二个参数是缺省值,第三个参数是输入字符的总长度。如果你设 置了文本框的缺省值,那么第三个参数必须是缺省字符的长度。如果第三个参数的值和缺省字 符的长度不一样,那么程序运行不成功(编译可以通过)。如果你将第二个参数置为null值,那 么第三个参数可以任意设。 ShowTextBox.java的运行效果如下图所示: TextField文本域对象 发信站: 北大未名站 (2001年10月20日20:37:59 星期六) , 站内信件 TextField和TextBox有点相似,不过TextBox是多行的,而TextField是单行的。而且TextBo x是容器类型的对象,但是TextField是项目类型的对象,只能够被容器包含,不能够单独显示。 TextField文本域对象的用法如下所示: package fancy.test; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class ShowTextField extends MIDlet implements CommandListener { private Display display; private Form props; private TextField txtField; private Command exitCommand = new Command("Exit", Command.EXIT, 1); public ShowTextField() { display = Display.getDisplay(this); } public void startApp() { props = new Form("Hello World"); props.append("Hello World!\n"); txtField=new TextField("EMail:", "", 15,TextField.EMAILADDR); props.append(txtField); props.addCommand(exitCommand); props.setCommandListener(this); display.setCurrent(props); } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } } public void destroyApp(boolean unconditional) { } public void pauseApp() { display.setCurrent(null); props = null; } } 请注意startApp()方法,我们使用Form对象作为当前屏幕的显示对象,而将TextField对象作 为Form的一个子项目显示。下面来介绍TextField类的构造函数,第一个参数是文本域的名称 ,第二个参数是缺省值,第三个参数是长度,第四个参数是文本域的类型,可选的值有: TextFi eld.PASSWORD、TextField.EMAILADDR、TextField.PHONENUMBER、TextField. URL、TextFi eld. NUMERIC等等。构造好TextField对象之后,调用Form的append()方法将它添加到Form对 象的子项目中。ShowTextField.java程序的运行效果如下图所示: DateField对象 发信站: 北大未名站 (2001年10月20日20:39:14 星期六) , 站内信件 DateField对象和TextField对象一样同属于项目类型的对象,不能够单独显示,必须作为容 器对象的子项目显示。DateField对象的作用是显示一个日期,它和Windows控制面板中的时间 和日期设置程序有点近似。DateField对象的用法如下所示: package fancy.test; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class ShowDateField extends MIDlet implements CommandListener { private Display display; private Form props; private DateField datField; private Command exitCommand = new Command("Exit", Command.EXIT, 1); public ShowDateField() { display = Display.getDisplay(this); } public void startApp() { props = new Form("Hello World"); props.append("Hello World!\n"); //change: //datField=new DateField("Date:",DateField.DATE_TIME); //datField=new DateField("Date:",DateField.TIME); datField=new DateField("Date:",DateField.DATE); props.append(datField); props.addCommand(exitCommand); props.setCommandListener(this); display.setCurrent(props); } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } } public void destroyApp(boolean unconditional) { } public void pauseApp() { display.setCurrent(null); props = null; } } ShowDateField.java程序的运行效果如下面两图所示: StringItem对象 发信站: 北大未名站 (2001年10月20日20:41:21 星期六) , 站内信件 StringItem对象和TextField、DateField对象类似,同样属于项目类型的对象。它的作用 就是在容器对象中显示一条字符串。 package fancy.test; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class ShowStringItem extends MIDlet implements CommandListener { private Display display; private Form props; private StringItem strItem; private StringItem strItem2; private Command exitCommand = new Command("Exit", Command.EXIT, 1); public ShowStringItem() { display = Display.getDisplay(this); } public void startApp() { props = new Form("Hello World"); props.append("Hello World!\n"); strItem=new StringItem("signature:","小楼一夜听春雨"); strItem2=new StringItem("signature:","三教明天考物化"); props.append(strItem); props.append(strItem2); props.addCommand(exitCommand); props.setCommandListener(this); display.setCurrent(props); } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } } public void destroyApp(boolean unconditional) { } public void pauseApp() { display.setCurrent(null); props = null; } } ShowStringItem.java程序的运行效果如下图所示: ImageItem对象 发信站: 北大未名站 (2001年10月20日21:10:50 星期六) , 站内信件 ImageItem对象是一个项目类型的对象,他的作用是在容器中显示图片。那么如何使用Imag eItem对象呢?请按照下面三个步骤进行: 1.构造一个Image对象,相关代码如下所示: Image img=Image.createImage("/fancy/test/JavaPowered-8.png"); createImage()方法是Image类的静态方法,它的作用是根据图形文件创建一个Image对象。 J2ME程序中所用到的图片文件必须存放在apps\fancy\res文件夹下面。 2.构造ImageItem对象,相关代码如下所示: imgItem=new ImageItem("Default Layout",img,ImageItem.LAYOUT_DEFAULT, "Image Cannot be shown"); ImageItem类的构造函数有三个参数,第一个参数的作用是显示一个标签,第二个参数指明图 片的对齐方式,第三个参数的作用是显示图片的tip。 3.利用容器类对象的append()方法将ImageItem对象添加进去。如: props.append(imgItem); 下面我们来看一个比较完整的例子。 package fancy.test; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class ShowImageItem extends MIDlet implements CommandListener { private Display display; private Form props; private Image img; private ImageItem imgItem; private Command exitCommand = new Command("Exit", Command.EXIT, 1); public ShowImageItem() { display = Display.getDisplay(this); } public void startApp() { props = new Form("Hello World"); //props.append("Hello World!\n"); try { img=Image.createImage("/fancy/test/JavaPowered-8.png"); imgItem=new ImageItem("Default Layout", img,ImageItem.LAYOUT_DEFAULT,"Image Cannot be shown"); props.append(imgItem); props.append(new ImageItem("Left Layout", img,ImageItem.LAYOUT_LEFT,"Image Cannot be shown")); props.append(new ImageItem("Center Layout", img,ImageItem.LAYOUT_CENTER,"Image Cannot be shown")); } catch(Exception fe) { //to do nothing } props.addCommand(exitCommand); props.setCommandListener(this); display.setCurrent(props); } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } } public void destroyApp(boolean unconditional) { } public void pauseApp() { display.setCurrent(null); props = null; } } ShowImageItem.java程序的运行效果如下图所示: ChoiceGroup对象 发信站: 北大未名站 (2001年10月20日21:12:23 星期六) , 站内信件 ChoiceGroup也是一个项目类型的对象,它代表一个选择列表,它的作用和List对象类似,不 过后者是一个容器,而前者是一个项目。 我们需要特别注意ChoiceGroup类的构造函数,它有四个参数,第一个参数是标签,第二个参 数是此选择列表的类型,例如多选还是单选。第三个参数是一个字符串数组,代表每个选项的 标签,第四个选项是一个Image类型的数组,代表每个选项前面的小图标。下面是一个比较完整 的例子。 package fancy.test; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class ShowChoiceGroup extends MIDlet implements CommandListener { private Display display; private Form props; private Image duke; private Image[] imageArray; private ChoiceGroup choice; private Command exitCommand = new Command("Exit", Command.EXIT, 1); public ShowChoiceGroup() { display = Display.getDisplay(this); } public void startApp() { props = new Form("Hello World"); //props.append("Hello World!\n"); try { Image duke= Image.createImage("/fancy/test/Icon.png"); imageArray = new Image[]{duke,duke,duke}; String[] stringArray = { "Option A", "Option B", "Option C" }; choice=new ChoiceGroup("choice group", ChoiceGroup.MULTIPLE,stringArray,imageArray); props.append(choice); } catch(Exception fe) { //to do nothing. } props.addCommand(exitCommand); props.setCommandListener(this); display.setCurrent(props); } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } public void destroyApp(boolean unconditional) { } public void pauseApp() { display.setCurrent(null); props = null; } } ShowChoiceGroup.java程序的运行效果如下图所示: Gauge对象 发信站: 北大未名站 (2001年10月20日21:13:19 星期六) , 站内信件 Gauge对象是一个项目类型的对象,它的作用是显示一个进度条。请看下面的源代码。Gaug e类的构造函数的后面两个参数分别是进度条的最大值和初始值。 package fancy.test; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class ShowGauge extends MIDlet implements CommandListener { private Display display; private Form props; private Command exitCommand = new Command("Exit", Command.EXIT, 1); public ShowGauge() { display = Display.getDisplay(this); } public void startApp() { props = new Form("Hello World"); //props.append("Hello World!\n"); Gauge gauge=new Gauge("show gauge",true,100,50); props.append(gauge); props.addCommand(exitCommand); props.setCommandListener(this); display.setCurrent(props); } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } } public void destroyApp(boolean unconditional) { } public void pauseApp() { display.setCurrent(null); props = null; } }
|