首页 | 联系我们 | 叶凡网络官方QQ群:323842844
游客,欢迎您! 请登录 免费注册 忘记密码
您所在的位置:首页 > 开发语言 > Java开发 > 正文

List类用法的例子

作者:cocomyyz 来源: 日期:2013-07-25 02:13:39 人气:6 加入收藏 评论:0 标签:java j2me

package com.brj.test;

import java.io.IOException;

import javax.microedition.lcdui.ChoiceGroup;
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.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
* 重点练习List类
* @author brj
*
*/
public class TestList extends MIDlet implements CommandListener {
private Display display;

private Command exitCommand;
private Command showExclusive;
private Command showImplicit;
private Command showMultiple;
private Command back;

private TextBox mainFace;

private List exclusive;
private List implicit;
private List multiple;

private String[] listTitle;
private String[] listContent;
private Image[] listImage;

public TestList() {
  display = Display.getDisplay(this);
 
  //exit Command button
  exitCommand = new Command("Exit", Command.EXIT, 0);
 
  //show different type screen groupware List
  showExclusive = new Command("sExclusive", Command.SCREEN, 1);
  showImplicit = new Command("sImplicit", Command.SCREEN, 1);
  showMultiple = new Command("sMultiple", Command.SCREEN, 1);
 
  //back Command button
  back = new Command("Back", Command.BACK, 0);
 
  //show this face when enter to program
  mainFace = new TextBox("MainFace", "You can choos the type.", 30,
    TextField.ANY);
 
  //the title of three style List
  listTitle = new String[] { "ExclusiveTitle", "ImplictTitle",
    "MultipleTitle" };
 
  // the choice content of the option
  listContent = new String[] { "OptionA", "OptionB", "OptionC", "OptionD" };
 
  //the image of the List which initialize them with null object is ok
  try {
   listImage = new Image[] { Image.createImage("/girl.png"), null,
     null, null };
  } catch (IOException e) {
   System.out.println("path error------");
   e.printStackTrace();
  }
 
  //new the objects of the List
  exclusive = new List(listTitle[0], ChoiceGroup.EXCLUSIVE, listContent,
    listImage);
  implicit = new List(listTitle[1], ChoiceGroup.IMPLICIT, listContent,
    listImage);
  multiple = new List(listTitle[2], ChoiceGroup.MULTIPLE, listContent,
    listImage);
}

//when the midLet exit execute this method
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  System.out.println("The Program is destroyed.");
}

//pause action
protected void pauseApp() {
  System.out.println("The Program is paused.");
}

//start midlet
protected void startApp() throws MIDletStateChangeException {
  mainFace.addCommand(exitCommand);
  mainFace.addCommand(showExclusive);
  mainFace.addCommand(showImplicit);
  mainFace.addCommand(showMultiple);
  mainFace.setCommandListener(this);
  display.setCurrent(mainFace);
}

//register itself to the monitor
public void commandAction(Command c, Displayable d) {
  //eixt
  if (c == exitCommand) {
   try {
    this.destroyApp(false);
   } catch (MIDletStateChangeException e) {
    e.printStackTrace();
   }
   this.notifyDestroyed();
  }
  //Exclusive
  if (c == showExclusive) {
   exclusive.addCommand(back);
   exclusive.setCommandListener(this);
   display.setCurrent(exclusive);
  }
  //Implicit
  if (c == showImplicit) {
   implicit.addCommand(back);
   implicit.setCommandListener(this);
   display.setCurrent(implicit);
  }
  //Multiple
  if (c == showMultiple) {
   multiple.addCommand(back);
   multiple.setCommandListener(this);
   display.setCurrent(multiple);
  }
  //back
  if (c == back) {
   display.setCurrent(mainFace);
  }
}

}


本文网址:http://www.mingyangnet.com/html/java/126.html
读完这篇文章后,您心情如何?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
上一篇: Ticker的用法
更多>>网友评论
发表评论
编辑推荐
  • 没有资料