⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formchoicedemomidlet.java

📁 里面的工程文件是本人做的一些小程序
💻 JAVA
字号:
package formchoicedemo;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class FormChoiceDemoMIDlet extends MIDlet implements CommandListener{
  static FormChoiceDemoMIDlet instance;
  Form mainForm;  //定义容纳choicegroup对象的form
  ChoiceGroup cgrpExlc,cgrpMulti;
  List menuList; //定义用作菜单的impict模式列表框
  //image[] listImage;
  Command backCmd,showAnswerCmd;
  Font myfont; //定义choicegroup中要设置的字体对象
  String[] listitem={"单项选择","多项选择","退出本程序"};//菜单选项
  String[] onlyChoice = {
      "the robbers_____the possibility of the alarm system sounding.",
      "A.overtook", "B.overflowered", "C.overcame", "D.overcame"};
  String[] multiChoice = {
      "在Internet上,典型的电子支付方式包括_____.",
      "A.电子货币支付", "B.电子支票", "C.银行卡", "D.牡丹卡"};

  public FormChoiceDemoMIDlet() {
    instance = this;
    backCmd = new Command("返回", Command.BACK, 0);
    showAnswerCmd = new Command("结果", Command.SCREEN, 1);
   // listImage = new Image[listitem.length];
    /*
    for (int i = 0; i < listitem.length; i++) {
      try {
        listImage[i] = Image.createImage("/pic/icon" + i ".png");
      }
      catch (java.io.IOException ioe) {}
    }
      }
 */
    menuList = new List("选择测试题型", List.IMPLICIT, listitem, null);
    menuList.setCommandListener(this);
    myfont = Font.getFont(Font.FACE_PROPORTIONAL,
                          Font.STYLE_BOLD,
                          Font.SIZE_MEDIUM);

  }

  public void startApp() {
    Display.getDisplay(this).setCurrent(menuList);
  }

  public void pauseApp() { }

  public void destroyApp(boolean unconditional) { }
  public void commandAction(Command c,Displayable d){
     if(c==menuList.SELECT_COMMAND){
       int index = menuList.getSelectedIndex();
       switch(index){
         case 0:
           //构造单选数组对象
           cgrpExlc = new ChoiceGroup(onlyChoice[0],Choice.EXCLUSIVE);
           //从数组中读出数据并且添加到选项数组对象内
           for(int i=0;i<onlyChoice.length-1;i++){
             cgrpExlc.append(onlyChoice[i+1],null);
           }
           for(int f=0;f<cgrpExlc.size();f++){
             cgrpExlc.setFont(f,myfont);
           }
           mainForm = new Form("单项选择测试题");
           mainForm.append(cgrpExlc);
           mainForm.addCommand(backCmd);
           mainForm.addCommand(showAnswerCmd);
           mainForm.setCommandListener(this);
           Display.getDisplay(this).setCurrent(mainForm);
           break;
          case 1:
            //构造复选数组对象
          cgrpMulti = new ChoiceGroup(multiChoice[0],Choice.MULTIPLE);
          //从数组中读出数据并且添加到选项数组对象内
          for(int i=0;i<multiChoice.length-1;i++){
            cgrpMulti.append(multiChoice[i+1],null);
          }
          for(int f=0;f<cgrpMulti.size();f++){
            cgrpMulti.setFont(f,myfont);
          }
          mainForm = new Form("单项选择测试题");
          mainForm.append(cgrpMulti);
          mainForm.addCommand(backCmd);
          mainForm.addCommand(showAnswerCmd);
          mainForm.setCommandListener(this);
          Display.getDisplay(this).setCurrent(mainForm);
          break;
         case 2:
           quitApp();
       }
     }
     if(c==backCmd){
        Display.getDisplay(this).setCurrent(menuList);
     }
     if(c==showAnswerCmd){
       if (d == mainForm && mainForm.get(0) == cgrpExlc) { //get 参数0代表什么????
         if (cgrpExlc.isSelected(3)) {
           mainForm.setTitle("答案");
           mainForm.append("恭喜你,答对了");
           mainForm.removeCommand(showAnswerCmd);
         }
         else {
           mainForm.setTitle("答案");
           mainForm.append("你答错了,请看上面正确答案");
           cgrpExlc.setSelectedIndex(3, true); //设置为正确答案的选项
           mainForm.removeCommand(showAnswerCmd);
         }
       }
       //判断当前对象是否是多选数组
       if (d == mainForm && mainForm.get(0) == cgrpMulti) { //get 参数0代表什么????
         String ans="";
         for(int i=0;i<cgrpMulti.size();i++){
          //判断是否选中
          if (cgrpMulti.isSelected(i)) {
            //取选中的选项组对象的第一个字符
            ans=ans+cgrpMulti.getString(i).substring(0,1);
          }
          if(ans.equals("ABC")){
            mainForm.setTitle("答案");
            mainForm.append("恭喜你,答对了");
            mainForm.removeCommand(showAnswerCmd);
          }
          else {
           mainForm.setTitle("答案");
           mainForm.append("你答错了,请看上面正确答案");
           boolean[] answer={true,true,true,false};
           cgrpMulti.setSelectedFlags(answer); //设置为正确答案的选项
           mainForm.removeCommand(showAnswerCmd);
         }
        }
       }
     }

  }

  public static void quitApp() {
    instance.destroyApp(true);
    instance.notifyDestroyed();
    instance = null;
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -