list中的ICON为什么不能显示了?(附源码)
目地:学习到list,希望有按照书上讲的一样,在list内容前面能显示一个ICON.
问题:下面的代码按着书上讲的做的。但是能显示list项,ICON却显示不出来。
Platform: xp sp2+jdk1.5+eclips3.2 + wtk2.5
问题估计:以为是ICON的路径问题,使用了绝对地址,把ICON copy到所有目录下都不行。是不是和ECLIPS有关系了?
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ListMode extends MIDlet implements CommandListener {
private String []elements = { "Sun china area ", "java.net ", "J2me R&D Web "};
private List list = null;
private Command okCommand = new Command( "Choose ",Command.OK,1);
private Display display = null;
public ListMode() {
Image img1 = null;
Image img2 = null;
try{
img1 = Image.createImage( "F:/eclipse-java-europa-win32/eclipse/WorkPlace/J2meList/bin/img1.png ");
img2 = Image.createImage( "/img2.png ");
}catch(Exception ioe){
}
Image [] images = {img1,img1,img2};
list = new List( "Best Web ",List.EXCLUSIVE,elements,images);
list.append( "new ele0 ", img2);
list.append( "new ele1 ", img1);
list.addCommand(okCommand);
list.setCommandListener(this);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() {
// TODO Auto-generated method stub
if(display == null){
display = Display.getDisplay(this);
display.setCurrent(list);
}else{
display.setCurrent(list);
}
}
public void commandAction(Command cmd, Displayable displayable){
if(cmd == okCommand){
int selectedIndex = list.getSelectedIndex();
String item = list.getString(selectedIndex);
Alert alert = new Alert( "Pls Confrim ", "Your decision is "+ item,null,AlertType.CONFIRMATION);
alert.setTimeout(2000);
display.setCurrent(alert,list);
}
}
}
[解决办法]
路径问题,把图片拷贝到eclipse项目存储目录下的res目录,在eclipse界面中刷新项目,使项目列表中显示出res文件夹,把代码中的图片路径修改成./文件名即可