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

/*
 * 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按钮按下后无法退出,这件事提醒我,编程序时要细心!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值