J2me和j2se的编译和运行差不多,确却的说,是j2me的一个延伸。如下一段
:import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* @author Administrator
*/
public class ImageMidlet extends MIDlet implements CommandListener{
private Display display;
private Command exit;
private Form form;
private ImageItem myi1;
private ImageItem myi2;
private Image img1 = null;
private Image img2 =null;
public ImageMidlet()
{
display=Display.getDisplay(this);
exit = new Command("退出",Command.SCREEN,1);
form = new Form("图片演示");
try{
img1 = Image.createImage("/icons/App.png");
img2 = Image.createImage("/icons/Duke.png");
}catch(Exception e){
form.append("load image is failed");
}
myi1 = new ImageItem("图片1",img1,Item.LAYOUT_LEFT,"图片1代替文本");
myi2 = new ImageItem("图片1",img2,Item.LAYOUT_LEFT,"图片2代替文本");
form.append(myi1);
form.insert(1,new Spacer(100,100) );
form.append(myi2);
form.addCommand(exit);
form.setCommandListener(this);
form.setCommandListener(this);
}
public void startApp() {
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c,Displayable d)
{
if(c==exit)
{
destroyApp(false);
notifyDestroyed();
}
}
}
,它主要包含了四个方法:startApp() ,public void pauseApp(),destroyApp,commandAction。前面三个是MIDlet类的三个方法,是必要的,覆盖父类的方法。commandAction是CommandListener的一个方法,当一个类想要实现它的接口,就必须要重写commandAction。这个代码运行出来的结果是