名单字符串和图像 : 列表 « 基于J2ME « Java 教程

En
Java 教程
1. 语言基础
2. 数据类型
3. 操作符
4. 流程控制
5. 类定义
6. 开发相关
7. 反射
8. 正则表达式
9. 集合
10. 线
11. 文件
12. 泛型
13. 本土化
14. Swing
15. Swing事件
16. 二维图形
17. SWT
18. SWT 二维图形
19. 网络
20. 数据库
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web服务SOA
27. EJB3
28. Spring
29. PDF
30. 电子邮件
31. 基于J2ME
32. J2EE应用
33. XML
34. 设计模式
35. 日志
36. 安全
37. Apache工具
38. 蚂蚁编译
39. JUnit单元测试
Java
Java 教程 » 基于J2ME » 列表 
31. 11. 3. 名单字符串和图像
import java.io.IOException;

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

public class ListMIDlet extends MIDlet implements CommandListener {
  TextBox main;

  private Command exitCommand;

  private Command exclusiveCommand;

  private Command multipleCommand;

  private Command implicitCommand;

  private Command confirmCommand;

  private List aEXCLUSIVEList;

  private List aMULTIPLEList;

  private List aIMPLICITList;

  String[] position = "1""2""3" };

  String[] hobby = "A""B""C""D""E" };

  Image[] positionIcon = createImage("/1.png"), createImage("/2.png"), createImage("/3.png") };

  Image[] hobbyIcon = createImage("/A.png"), createImage("/B.png"), createImage("/C.png"),
      createImage("/D.png"), createImage("/E.png") };

  private Display display;

  private String currentScreen = "";

  public ListMIDlet() {
    display = Display.getDisplay(this);
    exitCommand = new Command("exit", Command.SCREEN, 1);
    exclusiveCommand = new Command("exclusive", Command.SCREEN, 1);
    multipleCommand = new Command("multiple", Command.SCREEN, 1);
    implicitCommand = new Command("implicit", Command.SCREEN, 1);
    confirmCommand = new Command("confirm", Command.SCREEN, 2);

  }

  public void startApp() {
    main = new TextBox("main", null, 256, TextField.ANY);
    main.addCommand(exitCommand);

    main.addCommand(exclusiveCommand);
    main.addCommand(multipleCommand);
    main.addCommand(implicitCommand);

    main.setCommandListener(this);
    display.setCurrent(main);
    currentScreen = "Main";
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  private Image createImage(String name) {
    Image aImage = null;
    try {
      aImage = Image.createImage(name);
    catch (IOException e) {
    }
    return aImage;
  }

  private void exclusiveList() {
    aEXCLUSIVEList = new List("exclusive", List.EXCLUSIVE, position, positionIcon);
    aEXCLUSIVEList.addCommand(exitCommand);
    aEXCLUSIVEList.addCommand(confirmCommand);
    aEXCLUSIVEList.setCommandListener(this);
    display.setCurrent(aEXCLUSIVEList);
    currentScreen = "exclusive";
  }

  private void multipleList() {
    aMULTIPLEList = new List("multiple", List.MULTIPLE, hobby, hobbyIcon);
    aMULTIPLEList.addCommand(exitCommand);
    aMULTIPLEList.addCommand(confirmCommand);
    aMULTIPLEList.setCommandListener(this);
    display.setCurrent(aMULTIPLEList);
    currentScreen = "multiple";
  }

  private void implicitList() {
    aIMPLICITList = new List("implicit", List.IMPLICIT);
    aIMPLICITList.append("A", createImage("/A.png"));
    aIMPLICITList.append("B", createImage("/B.png"));
    aIMPLICITList.append("C", createImage("/C.png"));
    aIMPLICITList.append("D", createImage("/D.png"));
    aIMPLICITList.append("E", createImage("/E.png"));
    aIMPLICITList.addCommand(exitCommand);

    aIMPLICITList.setCommandListener(this);
    display.setCurrent(aIMPLICITList);
    currentScreen = "implicit";
  }

  public void commandAction(Command c, Displayable s) {
    if (c == exitCommand) {
      if (currentScreen == "implicit") {
        destroyApp(false);
        notifyDestroyed();
      else {
        display.setCurrent(main);
        currentScreen = "no";
      }
    }

    if (c == exclusiveCommand) {
      exclusiveList();
    }
    if (c == multipleCommand) {
      multipleList();
    }
    if (c == implicitCommand) {
      implicitList();
    }
    if (c == confirmCommand) {
      Alert aAlert;

      if (currentScreen == "implicit") {
        int aIndex = aEXCLUSIVEList.getSelectedIndex();
        aAlert = new Alert("Result", position[aIndex], null, AlertType.INFO);
        display.setCurrent(aAlert);
      }
      if (currentScreen == "multiple") {
        String result = "result\n";
        for (int i = 0; i < aMULTIPLEList.size(); i++)
          if (aMULTIPLEList.isSelected(i))
            result += hobby[i", ";
        aAlert = new Alert("alert", result, null, AlertType.INFO);
        aAlert.setTimeout(5000);
        display.setCurrent(aAlert);
      }
    }
    if (c == List.SELECT_COMMAND) {
      int aIndex = aIMPLICITList.getSelectedIndex();
      String menuItem = aIMPLICITList.getString(aIndex);
      Alert aAlert = new Alert("imple", menuItem, null, AlertType.INFO);
      aAlert.setTimeout(5000);
      display.setCurrent(aAlert);
    }
  }

}
31. 11. 列表
31. 11. 1. 使用列表
31. 11. 2. 使用列表进行选择使用列表进行选择
31. 11. 3. 名单字符串和图像
31. 11. 4. 隐含菜单隐含菜单
31. 11. 5. 复选框列表复选框列表
31. 11. 6. 单选按钮列表单选按钮列表
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.