我的J2ME编程练习(5)——ImageItem

王朝java/jsp·作者佚名  2006-01-09
宽屏版  字体: |||超大  

/*

* 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对象分别位于左右两端,布局上好看一些。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有