首先我们来说说List的用法,这是个列表式屏幕,List好比我上篇文章说过的TextBox一样,也是作为一个画布一样的界面然后往里面添加需要的控件就可以,只不过TextBox添加上去的顺序和位置是从左到右从上到下的,而List添加的是一行一行的列表式的文字或选项或图片,更多用在选项框的界面,以下给出源代码和运行截图
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*; //提供了一个命令类
public class list extends MIDlet implements CommandListener
//要处理按钮高级别事件。程序必须继承一个命令监听器接口类CommandListener类
{
private List mainList;
Image[] imageArray;
private Command exitCommand;
String stringArray[] = {
"Test1",
"Test2",
"Test3"
};
public list() //类的构造函数
{
try{
Image icon = Image.createImage("/1.png");
imageArray = new Image[]{
icon,
icon,
icon
};
}
catch (java.io.IOException err)
{
imageArray = null;
System.out.println("dkfkdfjdkjfkdfkdj");
}
mainList = new List("terry",Choice.MULTIPLE,stringArray,imageArray);
exitCommand = new Command("Exit",Command.EXIT,1);
mainList.addCommand(exitCommand);
mainList.setCommandListener(this);
}
protected void startApp() throws MIDletStateChangeException //负责程序的初始化功能
{
Display.getDisplay(this).setCurrent(mainList);
System.out.println("startMyfirstJ2ME");
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException //程序推出时调用的函数
{
System.out.println("玩完咯");
}
protected void pauseApp() //暂停状态时调用的函数
{
System.out.println("PauseApp");
}
public void commandAction(Command arg0, Displayable arg1) {
// TODO Auto-generated method stub
}
}
