/* * 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對象分別位於左右兩端,布局上好看一些。