我的J2ME编程练习(4)——StringItem

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

/*
 * stringItemlet.java
 *
 * Created on 2005年4月14日, 下午4:26
 */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 *
 * @author  Administrator
 * @version
 */
public class stringItemlet extends MIDlet implements CommandListener,
ItemCommandListener{
   
    private Form aform;
    private Command okCommand;
    private Command exitCommand;
    private Command hllinkCommand;
    private Command bCommand;
    private Display aDisplay;
    private StringItem hlstringItem;
    private StringItem bstringItem;
    private Alert hlAlert;
    private Alert bAlert;
   
    public stringItemlet(){
        okCommand=new Command("OK",Command.OK,1);
        exitCommand=new Command("EXIT",Command.EXIT,1);
        hllinkCommand=new Command("LINK",Command.ITEM,2);
        bCommand=new Command("BUTTON",Command.ITEM,2);
       
       
        aform=new Form("StringItemTest");
       
        //if click hyperlink "here",display anAlert
        hlstringItem=new StringItem(null,"here",Item.HYPERLINK);
        hlstringItem.setItemCommandListener(this);
        hlstringItem.setDefaultCommand(hllinkCommand);
       
        bstringItem=new StringItem(null,"Available?",Item.BUTTON);
        bstringItem.setItemCommandListener(this);
        bstringItem.setDefaultCommand(bCommand);
       
       
        hlAlert=new Alert("Item.HYPERLINK","You Can Call Me 800-8101234"
                ,null,AlertType.INFO);
        bAlert=new Alert("Item.Button","The Button is Available!"
                ,null,AlertType.INFO);
       
       
        aform.append("Any question ,please click ");
        aform.append(hlstringItem);
        aform.append(bstringItem);
       
        aform.addCommand(okCommand);
        aform.addCommand(exitCommand);
        aform.setCommandListener(this);
       
    }
    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();
        }
        else{
           
        }
       
    }
   
    public void commandAction(Command c,Item i){
       
        if(c==hllinkCommand){
            aDisplay.setCurrent(hlAlert,aform);
        }
        else if(c==bCommand){
            aDisplay.setCurrent(bAlert,aform);
        }
    }
   
   
}

这个程序如果说有什么比较新的东西的话,那就在于运用了StringItem的外观模式:HYPERLINK和BUTTON。由此也使用了ItemCommandListener接口,实现了commandAction(Command c ,Item i)方法。这个方法的方法体的写法和commandAction(Command c , Displayable d)很类似,这一点可以从程序中看出来。需要说明的是,commandAction(Command c , Item i)方法也可以使用Item变量i进行选择,例如:

if (i==oneItem){

   if (c==oneCommand){

……// the program

}}

else if (i==otherItem){

if (c==otherCommand){

……  //the program

}}

此外,该程序在编写时,忘记写aform.setCommandListener(this);语句了,致使EXIT按钮按下后无法退出,这件事提醒我,编程序时要细心!

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