import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.Image; import javax.microedition.lcdui.ImageItem; import javax.microedition.lcdui.Item; import javax.microedition.lcdui.ItemCommandListener; import javax.microedition.lcdui.StringItem; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; //StringItem 组件的测试: // 作用:配合不同的外观在屏幕上显示一串字符 // 构造方法:三个参数--第一个是Label,第二个是内容,第三个是外观(PLAIN,BUTTON,HYPERLINK) // 提取外观:getAppearanceMode(); 获得外观 //案例:包含Item 和 部分高级 UI public class StringItemTest extends MIDlet implements CommandListener,ItemCommandListener { //初始化变量 private Display display ; private Form mainForm; private final static Command CMD_GO = new Command("Go",Command.ITEM,1); private final static Command CMD_PRESS = new Command("Press",Command.ITEM,1); private final static Command CMD_EXIT = new Command("Exit",Command.EXIT,1); //destroyApp protected void destroyApp(boolean arg0) throws MIDletStateChangeException { // TODO Auto-generated method stub } // pauseApp protected void pauseApp() { // TODO Auto-generated method stub } //startApp protected void startApp() throws MIDletStateChangeException { System.out.println("程序启动成功,开始执行....."); // TODO Auto-generated method stub display = Display.getDisplay(this); mainForm = new Form("String Item Demo"); mainForm.append("This is a simple label"); //文本 StringItem item = new StringItem("This is StringItem label:","This is StringItem text"); mainForm.append(item); //超连接 三个参数:第三个表示外观--超链接 item = new StringItem("Hyper-Link:","hyperlink",Item.HYPERLINK); item.setDefaultCommand(CMD_GO); //设置StringItem 默认命令为:CMD_GO item.setItemCommandListener(this); mainForm.append(item); //按钮 item = new StringItem("Button","Button",Item.BUTTON); item.setDefaultCommand(CMD_PRESS); item.setItemCommandListener(this); mainForm.append(item); System.out.println("容器添加命令.."); // 为Form 容器添加设置命令 mainForm.addCommand(CMD_EXIT); mainForm.setCommandListener(this); display.setCurrent(mainForm); imageItem(); } public void commandAction(Command c, Displayable d) { // TODO Auto-generated method stub try { destroyApp(false); //MIDlet 和AMS 进行通信,通知应用管理软件自己状态的变换,调用notifyDestroyed(); // notifyPaused(); notifyDestroyed(); } catch (MIDletStateChangeException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void commandAction(Command c, Item i) { // TODO Auto-generated method stub if(c==CMD_GO){ String text ="Go to the URL....."; Alert a = new Alert("URL",text,null,AlertType.INFO); display.setCurrent(a); }else if(c==CMD_PRESS){ String text = "Do an action...."; Alert a = new Alert("Action",text,null,AlertType.INFO); display.setCurrent(a); } // 打印测试 System.out.println("选中的命令为:"+c.getLabel()); System.out.println(i.getLabel()); } //ImageItem组件测试 //构造方法的五个参数:1、Item的Label; 2、图片; 3、等效先 ; 4、取代文字(图片无法显示的时候) ; 5、外观 public void imageItem(){ this.pauseApp(); //暂停 Image image = null; try{ image = Image.createImage("/eclipse.png"); }catch(Exception ex){ } mainForm = new Form("ImageItem 测试"); mainForm.append(image); //图片 1 ImageItem iil = new ImageItem("图片 1",image,Item.LAYOUT_CENTER|Item.LAYOUT_NEWLINE_BEFORE,"图片 1 取代文字",Item.BUTTON); mainForm.append(iil); //图片 2 ImageItem ii2 = new ImageItem("图片 2",image,Item.LAYOUT_CENTER|Item.LAYOUT_NEWLINE_BEFORE,"图片 2 取代文字",Item.HYPERLINK); mainForm.append(ii2); display.setCurrent(mainForm); } }