j2me的List列表开发 ListDemo.java

本文展示了一个使用Java MIDP(Mobile Information Device Profile)编写的UI示例程序,该程序通过不同的列表选项展示了如何使用Choice Group来创建互斥选择、隐式选择及多重选择等界面。代码中还包含了如何为这些界面添加命令以及如何响应命令的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.IOException;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;


public class ListDemo extends MIDlet implements CommandListener {
private static final Command CMD_EXIT = new Command("Exit",Command.EXIT,1);
private static final Command CMD_BACK = new Command("Back",Command.BACK,1);
private Display display;
private List mainList;
private List exclusiveList;
private List implicitList;
private List multipletList;
private boolean firstTime;

public ListDemo(){
display = Display.getDisplay(this);

String[] stringArray = {
"Option A",
"Option B",
"Option C",
"Option D"
};

Image[] imageArray = null;

exclusiveList = new List("Exclusive",Choice.EXCLUSIVE,stringArray,imageArray);
exclusiveList.addCommand(CMD_BACK);
exclusiveList.addCommand(CMD_EXIT);
exclusiveList.setCommandListener(this);

implicitList = new List("Implicit",Choice.IMPLICIT,stringArray,imageArray);
implicitList.addCommand(CMD_BACK);
implicitList.addCommand(CMD_EXIT);
implicitList.setCommandListener(this);

multipletList = new List("Multiplet",Choice.MULTIPLE,stringArray,imageArray);
multipletList.addCommand(CMD_BACK);
multipletList.addCommand(CMD_EXIT);
multipletList.setCommandListener(this);

firstTime = true;
}

protected void startApp() {
if(firstTime){
Image[] imageArray = null;

try {
Image image = Image.createImage("/Icon.png");

imageArray = new Image[]{
image,
image,
image
};

} catch (IOException e) {
e.printStackTrace();
}

String[] stringArray = {
"Exclusive",
"Implicit",
"Multiple"
};

mainList = new List("Choose type",Choice.IMPLICIT,stringArray,imageArray);
mainList.addCommand(CMD_EXIT);
mainList.setCommandListener(this);
display.setCurrent(mainList);
firstTime = false;
}
}

protected void destroyApp(boolean arg0){
}

protected void pauseApp() {
}

public void commandAction(Command c, Displayable d) {
if(d.equals(mainList)){
if(c == List.SELECT_COMMAND){
switch(((List)d).getSelectedIndex()){
case 0:
display.setCurrent(exclusiveList);
break;
case 1:
display.setCurrent(implicitList);
break;
case 2:
display.setCurrent(multipletList);
}
}
}else{
if(c == CMD_BACK){
display.setCurrent(mainList);
}
}
if(c == CMD_EXIT){
destroyApp(false);
notifyDestroyed();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值