我的J2ME編程練習(4)——StringItem - 中國WEB開發者網絡 (http://www.webasp.net) -- 技術教程 (http://www.webasp.net/article/) --- 我的J2ME編程練習(4)——StringItem (http://www.webasp.net/article/18/17758.htm) |
| -- 作者:未知 -- 發佈日期: 2005-04-20 |
|
/* * 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按鈕按下後無法退出,這件事提醒我,編程序時要細心! |
| webasp.net |