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

类别:Java 点击:0 评论:0 推荐:

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

本文地址:http://com.8s8s.com/it/it10231.htm