/* * imageitemlet.java * * created on 2005年4月17日, 下午8:56 */
import javax.microedition.midlet.*;import javax.microedition.lcdui.*;
/** * * @author administrator * @version */public class imageitemlet extends midlet implements commandlistener,itemcommandlistener{ private form aform; private image sohuimage; private image neimage; private imageitem sohuimageitem; private imageitem neimageitem; private display adisplay; private command exitcommand; private command connectcommand; private spacer aspacer; private alert analert; public imageitemlet() { aform=new form("imageitemtest"); exitcommand=new command("exit",command.exit,1); connectcommand=new command("connect",command.item,2); aspacer=new spacer(10,2); analert=new alert("connecting…","connecting to www.163.com …", null,alerttype.info); try{ sohuimage=image.createimage("/sohu.png"); sohuimageitem=new imageitem(null,sohuimage,imageitem.layout_right, "this is sohu!"); aspacer.setlayout(item.layout_expand);
//the profram will connect to www.163.com and display the alert //when click the imageitem of 163.png .
neimage=image.createimage("/163.png"); neimageitem=new imageitem(null,neimage,imageitem.layout_right, "this is 163",item.hyperlink); neimageitem.setitemcommandlistener(this); neimageitem.setdefaultcommand(connectcommand); aform.append(sohuimageitem); aform.append(aspacer); aform.append(neimageitem); aform.addcommand(exitcommand); aform.setcommandlistener(this); } catch (exception e){ e.printstacktrace(); } } public void startapp() { adisplay=display.getdisplay(this); adisplay.setcurrent(aform); } public void pauseapp() { } public void destroyapp(boolean unconditional) { } public void commandaction(command c,displayable d){ if(c==exitcommand){ destroyapp(false); notifydestroyed(); } } public void commandaction(command c,item i){ if(c==connectcommand){ adisplay.setcurrent(analert,aform); } } }
这个程序的比较新的地方在于使用了midp2.0中新增加的item类的外观模式和spacer类。其中外观模式的使用在stringitem中已经介绍过了。
public spacer(int minwidth, int minheight),spacer类在本程序中设置为item.layout_expand,即填充剩余的空白部分。这样,运行后可以看到两个imageitem对象分别位于左右两端,布局上好看一些。
