您的位置:首页 > 其它

J2ME从零开始(学习笔记5)

2009-09-23 17:36 477 查看
★ Form 及其 Item 组件
Form窗体是Screen的一个子类,可以包含图片、只读文本、可编辑文本、进度条、选项组以及自己可以定义的项目,这些都是Item类的子类。也就是说Item子类都可以显示在Form窗体中。
Form窗体也有两个构造函数,和List相似
Form(String title) 创建一个只包含标题的空白Form
Form(String title ,Item [ ] items)创建一个包含标题和Item的Form
Form窗体的体系结构图



1 StringItem

StringItem只是向用户提示一些信息,其内容不能像文本框那样编辑,其功能如同Lable
有两个构造函数:
StringItem(String label , String text)
StringItem(String label , String text, int appearanceMode)
第二个构造函数的第三个参数为外观,分别是PLAIN、HYPERLINK、BUTTON
同样该组件也提供了一些函数,参照API

以下是些示例代码:
package J2ME001;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
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;

public class StringItemMIDlet extends MIDlet implements ItemCommandListener {

private Form form;
private StringItem strItem1;
private StringItem strItem2;
private StringItem strItem3;

private final Command cmdItem1=new Command("Plain",Command.ITEM,1);
private final Command cmdItem2=new Command("Button",Command.ITEM,1);
private final Command cmdItem3=new Command("Hyperlink",Command.ITEM,1);

public StringItemMIDlet(){
strItem1=new StringItem("1","Plain",Item.PLAIN);
strItem1.addCommand(cmdItem1);
strItem1.setItemCommandListener(this);
strItem2=new StringItem("2","Button",Item.BUTTON);
strItem2.addCommand(cmdItem2);
strItem2.setItemCommandListener(this);
strItem3=new StringItem("3","Hyperlink",Item.HYPERLINK);
strItem3.addCommand(cmdItem3);
strItem3.setItemCommandListener(this);
form=new Form("StringItem");
form.append(strItem1);//将Item加到Form中
form.append(strItem2);
form.append(strItem3);
}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
// TODO Auto-generated method stub

}

protected void pauseApp() {
// TODO Auto-generated method stub

}

protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(form);

}

public void commandAction(Command command, Item item) {
if(command.equals(cmdItem1)){
System.out.println("Plain");
}else if(command.equals(cmdItem2)){
System.out.println("Button");
}else if(command.equals(cmdItem3)){
System.out.println("Hyperlink");
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: