一個簡單的猜謎遊戲的代碼(進階使用者介面製作)

來源:互聯網
上載者:User

//MiDlet1.java

package test;

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

public class MIDlet1 extends MIDlet {
    static MIDlet1 instance;
    Displayable1 displayable = new Displayable1(Display.getDisplay(this));
    public MIDlet1() {
        instance = this;
    }

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

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

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

}

//Displayable1.java

package test;

import javax.microedition.lcdui.*;

public class Displayable1 extends Form implements CommandListener {
    /**題目內容*/
    String[] question = {
         "花園裡都是草。(打一植物)",
         "一年後花園裡還是草。(也打一植物)",
         "來了一隻羊。(還打一植物)",
         "來了一隻狼。(再打一植物)"
    };
    /**題目選項*/
    String[][] answer = {
                      {"桃花","草莓","梨花","梅花"},
                      {"人蔘果","梨樹","草莓","野梅花"},
                      {"野梅花","梨樹","草莓","灌木"},
                      {"桃花","梅花","梨花","揚梅"}
    };
    /**正確答案的序號,第一個是0,依次類推*/
    int[] rightAnswer={3,3,2,3};

    ChoiceGroup cgQuestion;
    /**當前題目編號*/
    int index = 0;

    /**確定按鍵*/
    Command cmdOk;

    Display display;
    public Displayable1(Display display) {
        super("猜謎語");
        this.display = display;
        //建立題目
        cgQuestion = new ChoiceGroup(question[index],ChoiceGroup.EXCLUSIVE);
        //填充選擇項
        for(int i = 0;i < 4;i++){
            cgQuestion.append(answer[index][i],null);
        }
        append(cgQuestion);
        cmdOk = new Command("確定",Command.OK,1);
        addCommand(cmdOk);
        setCommandListener(this);
    }

    public void commandAction(Command command, Displayable displayable) {
        //確定按鈕
        if(command == cmdOk){
            //獲得使用者的選擇
            int selectIndex = cgQuestion.getSelectedIndex();

            //判斷是否回答不正確
            if(selectIndex != rightAnswer[index]){
                Alert a = new Alert("猜謎語","回答錯誤,請仔細思考!",null,AlertType.INFO);
                a.setTimeout(3000);
                display.setCurrent(a);
                //返回
                return;
            }

            //如果不是最後一個題目,則顯示下一題
            if(index < question.length - 1){
                index++;
                //修改介面顯示的內容
                cgQuestion.setLabel(question[index]);
                //修改選項的內容
                for(int i = 0;i < 4;i++){
                    cgQuestion.set(i,answer[index][i],null);
                }
            }else{//顯示成功介面
                Alert a = new Alert("猜謎語","遊戲成功!",null,AlertType.INFO);
                a.setTimeout(Alert.FOREVER);
                display.setCurrent(a);
            }
        }
    }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.