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);
}
}
分享到:
相关推荐
5) J2ME 中,MIDP 的用户界面组件包括 `Form`、`ChoiceGroup`、`TextBox`、`Alert`、`ImageItem` 和 `Command` 等。这些组件允许开发者构建交互式的用户界面,例如,`Form` 用于展示文本和控件的列表,`ChoiceGroup`...
正确的代码片段是:`StringItem strIt = new StringItem(" ", "Msg"); show.append(strIt);`这将在Form "Name"中添加一个显示文本"Msg"的条目。 3. 为了显示进程指示,我们通常会使用`Gauge`组件。正确的方法是创建...
它支持多种子组件,如字符串项(StringItem)、图像项(ImageItem)等。 **2.6 StringItem及ImageItem** - **StringItem**:用于显示文本信息。 - **ImageItem**:用于显示图像信息。 **2.7 CustomItem** CustomItem...
以StringItem为例,`StringItem si1 = new StringItem("测试 1 :", "内容 1", Item.PLAIN);`创建了一个标题为“测试 1 :”,内容为“内容 1”的StringItem,且Item的显示样式为PLAIN。 DateField则用于处理日期和...
- **Item**:所有的UI元素都是从`Item`类派生的,例如`StringItem`、`TextField`等。 - **ItemStateListener**:用于监听`Item`的状态变化。 - **MIDlet**:J2ME应用程序的基本单位,每个应用程序都是一个`MIDlet`...
- **La Clase StringItem:** Para mostrar texto. - **La Clase ImageItem:** Para mostrar imágenes. - **La Clase TextField:** Para la entrada de texto. - **La Clase DateField:** Para la entrada de fechas...