J2ME学习笔记(基础6)

xiaoxiao2025-02-09  23

ImageItem对象是一个项目类型的对象,他的作用是在容器中显示图片。那么如何使用ImageItem对象呢?请按照下面三个步骤进行: 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; } } 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; } } Gauge对象是一个项目类型的对象,它的作用是显示一个进度条。请看下面的源代码。 Gauge类的构造函数的后面两个参数分别是进度条的最大值和初始值。 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; } } Ticker对象是一个项目类型的对象,它的作用相当于一个滚动消息栏,在屏幕的上方显示滚动的信息。 Ticker类的构造函数仅有一个参数,那就是需要滚动显示的消息。 package fancy.test; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class ShowTicker extends MIDlet implements CommandListener { private Display display; private Form props; private Command exitCommand = new Command("Exit", Command.EXIT, 1); public ShowTicker() { display = Display.getDisplay(this); } public void startApp() { props = new Form("Hello World"); props.append("Hello World!\n"); Ticker ticker=new Ticker("D??¥ò?ò1 ;ìy′oóê"); props.setTicker(ticker); 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; } } <!--v:3.2--> 相关资源:敏捷开发V1.0.pptx
转载请注明原文地址: https://www.6miu.com/read-5024376.html

最新回复(0)