J2ME学习笔记:ImageItem图形显示控件(原创)
ImageItem是一个图形的控件,注意的是运用的时候要加上try捕捉错误,构造方法是:new ImageItem(标题,图像,布局,代替文字,类型)以下是代码和截图(如果发现图片载入错误,
把Display.getDisplay(this).setCurrent(mainForm);放回到构造函数里面就好了。
):
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;
public class ImageDemo extends MIDlet implements CommandListener {
private Command exitCommand = new Command("exit",Command.EXIT,1);
private Form mainForm;
private ImageItem pngImage1;
private ImageItem pngImage2;
public ImageDemo() {
mainForm = new Form("Image");
mainForm.append("显示图像的");
try{
//创建第一个图片控件
pngImage1 = new ImageItem("默认图像控件",Image.createImage("/2.png"),Item.LAYOUT_DEFAULT,"图片没有了");
//添加扩充宽度以及新起一行布局的图像
pngImage2 = new ImageItem("图像控件",Image.createImage("/1.png"),Item.LAYOUT_LEFT,"图片没有了");
}catch (Exception e){
System.out.println("图像出错!");
}
mainForm.append(pngImage1);
mainForm.append(pngImage2);//添加两个图像控件在当前 Form上并显示出来
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
Display.getDisplay(this).setCurrent(mainForm);
}
protected void startApp() throws MIDletStateChangeException {
//Display.getDisplay(this).setCurrent(mainForm);
}
public void commandAction(Command c, Displayable d) {
if(c == exitCommand)
{
try {
destroyApp(false);
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
notifyDestroyed();
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
}