📄 hellomidlet.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package hello;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import java.util.*;/** * @author Administrator */public class HelloMIDlet extends MIDlet implements CommandListener { private boolean midletPaused = false; //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0| private Command okCommand1; private Command exitCommand1; private Command okCommand; private Command exitCommand; private Form form; private TextField tfAns; private TextField textField; private StringItem SI_result; private Alert alert; //</editor-fold>//GEN-END:|fields|0| /** * The HelloMIDlet constructor. */ public HelloMIDlet() { } //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0| //</editor-fold>//GEN-END:|methods|0| //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize /** * Initilizes the application. * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method. */ private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize // write pre-initialize user code here//GEN-LINE:|0-initialize|1|0-postInitialize // write post-initialize user code here }//GEN-BEGIN:|0-initialize|2| //</editor-fold>//GEN-END:|0-initialize|2| //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction /** * Performs an action assigned to the Mobile Device - MIDlet Started point. */ public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction // write pre-action user code here switchDisplayable(null, getForm());//GEN-LINE:|3-startMIDlet|1|3-postAction // write post-action user code here }//GEN-BEGIN:|3-startMIDlet|2| //</editor-fold>//GEN-END:|3-startMIDlet|2| //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction /** * Performs an action assigned to the Mobile Device - MIDlet Resumed point. */ public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction // write pre-action user code here//GEN-LINE:|4-resumeMIDlet|1|4-postAction // write post-action user code here }//GEN-BEGIN:|4-resumeMIDlet|2| //</editor-fold>//GEN-END:|4-resumeMIDlet|2| //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch /** * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable. * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately * @param nextDisplayable the Displayable to be set */ public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch // write pre-switch user code here Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch if (alert == null) { display.setCurrent(nextDisplayable); } else { display.setCurrent(alert, nextDisplayable); }//GEN-END:|5-switchDisplayable|1|5-postSwitch // write post-switch user code here }//GEN-BEGIN:|5-switchDisplayable|2| //</editor-fold>//GEN-END:|5-switchDisplayable|2| //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction /** * Called by a system to indicated that a command has been invoked on a particular displayable. * @param command the Command that was invoked * @param displayable the Displayable where the command was invoked */ public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction // write pre-action user code here if (displayable == alert) {//GEN-BEGIN:|7-commandAction|1|33-preAction if (command == exitCommand1) {//GEN-END:|7-commandAction|1|33-preAction // write pre-action user code here exitMIDlet();//GEN-LINE:|7-commandAction|2|33-postAction // write post-action user code here } else if (command == okCommand1) {//GEN-LINE:|7-commandAction|3|31-preAction // write pre-action user code here switchDisplayable(null, getForm());//GEN-LINE:|7-commandAction|4|31-postAction textField.setString(""); SI_result.setText(""); rankGenerator(); // write post-action user code here }//GEN-BEGIN:|7-commandAction|5|19-preAction } else if (displayable == form) { if (command == exitCommand) {//GEN-END:|7-commandAction|5|19-preAction // write pre-action user code here exitMIDlet();//GEN-LINE:|7-commandAction|6|19-postAction // write post-action user code here } else if (command == okCommand) {//GEN-LINE:|7-commandAction|7|27-preAction // write pre-action user code here//GEN-LINE:|7-commandAction|8|27-postAction // write post-action user code here String strAns=tfAns.getString(); String strLab=textField.getString(); if(strLab.equals("9")){ tfAns.setConstraints(TextField.NUMERIC); }else{ int intA=0,intB=0; for(int i=0;i<=3;i++){ for(int j=0;j<=3;j++){ if(strLab.substring(i,i+1).equals(strAns.substring(j,j+1))){ if(i==j){ intA++; }else{ intB++; } } } } SI_result.setText("\n" + strLab + " " + intA + "A" + intB + "B" + SI_result.getText()); if(intA==4){ SI_result.setText("Yeh~~" + SI_result.getText()); switchDisplayable(getAlert(), getForm()); } textField.setString("");} }//GEN-BEGIN:|7-commandAction|9|7-postCommandAction }//GEN-END:|7-commandAction|9|7-postCommandAction // write post-action user code here }//GEN-BEGIN:|7-commandAction|10| //</editor-fold>//GEN-END:|7-commandAction|10| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|18-getter|0|18-preInit /** * Returns an initiliazed instance of exitCommand component. * @return the initialized component instance */ public Command getExitCommand() { if (exitCommand == null) {//GEN-END:|18-getter|0|18-preInit // write pre-init user code here exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit // write post-init user code here }//GEN-BEGIN:|18-getter|2| return exitCommand; } //</editor-fold>//GEN-END:|18-getter|2| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: form ">//GEN-BEGIN:|14-getter|0|14-preInit /** * Returns an initiliazed instance of form component. * @return the initialized component instance */ public Form getForm() { if (form == null) {//GEN-END:|14-getter|0|14-preInit // write pre-init user code here form = new Form("Welcome", new Item[] { getTextField(), getTfAns(), getSI_result() });//GEN-BEGIN:|14-getter|1|14-postInit form.addCommand(getExitCommand()); form.addCommand(getOkCommand()); form.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit // write post-init user code here }//GEN-BEGIN:|14-getter|2| return form; } //</editor-fold>//GEN-END:|14-getter|2| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textField ">//GEN-BEGIN:|22-getter|0|22-preInit /** * Returns an initiliazed instance of textField component. * @return the initialized component instance */ public TextField getTextField() { if (textField == null) {//GEN-END:|22-getter|0|22-preInit // write pre-init user code here textField = new TextField("\u56DB\u4F4D\u6578", "", 32, TextField.ANY);//GEN-LINE:|22-getter|1|22-postInit // write post-init user code here }//GEN-BEGIN:|22-getter|2| return textField; } //</editor-fold>//GEN-END:|22-getter|2| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: tfAns ">//GEN-BEGIN:|23-getter|0|23-preInit /** * Returns an initiliazed instance of tfAns component. * @return the initialized component instance */ public TextField getTfAns() { if (tfAns == null) {//GEN-END:|23-getter|0|23-preInit // write pre-init user code here tfAns = new TextField("\u7B54\u6848\u662F", "", 32, TextField.ANY);//GEN-LINE:|23-getter|1|23-postInit // write post-init user code here }//GEN-BEGIN:|23-getter|2| return tfAns; } //</editor-fold>//GEN-END:|23-getter|2| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: SI_result ">//GEN-BEGIN:|24-getter|0|24-preInit /** * Returns an initiliazed instance of SI_result component. * @return the initialized component instance */ public StringItem getSI_result() { if (SI_result == null) {//GEN-END:|24-getter|0|24-preInit // write pre-init user code here SI_result = new StringItem("\u8F38\u5165\u7B54\u6848\u662F", "");//GEN-LINE:|24-getter|1|24-postInit // write post-init user code here }//GEN-BEGIN:|24-getter|2| return SI_result; } //</editor-fold>//GEN-END:|24-getter|2| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|26-getter|0|26-preInit /** * Returns an initiliazed instance of okCommand component. * @return the initialized component instance */ public Command getOkCommand() { if (okCommand == null) {//GEN-END:|26-getter|0|26-preInit // write pre-init user code here okCommand = new Command("Ok", Command.OK, 0);//GEN-LINE:|26-getter|1|26-postInit // write post-init user code here }//GEN-BEGIN:|26-getter|2| return okCommand; } //</editor-fold>//GEN-END:|26-getter|2| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand1 ">//GEN-BEGIN:|30-getter|0|30-preInit /** * Returns an initiliazed instance of okCommand1 component. * @return the initialized component instance */ public Command getOkCommand1() { if (okCommand1 == null) {//GEN-END:|30-getter|0|30-preInit // write pre-init user code here okCommand1 = new Command("Ok", Command.OK, 0);//GEN-LINE:|30-getter|1|30-postInit // write post-init user code here }//GEN-BEGIN:|30-getter|2| return okCommand1; } //</editor-fold>//GEN-END:|30-getter|2| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand1 ">//GEN-BEGIN:|32-getter|0|32-preInit /** * Returns an initiliazed instance of exitCommand1 component. * @return the initialized component instance */ public Command getExitCommand1() { if (exitCommand1 == null) {//GEN-END:|32-getter|0|32-preInit // write pre-init user code here exitCommand1 = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|32-getter|1|32-postInit // write post-init user code here }//GEN-BEGIN:|32-getter|2| return exitCommand1; } //</editor-fold>//GEN-END:|32-getter|2| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: alert ">//GEN-BEGIN:|28-getter|0|28-preInit /** * Returns an initiliazed instance of alert component. * @return the initialized component instance */ public Alert getAlert() { if (alert == null) {//GEN-END:|28-getter|0|28-preInit // write pre-init user code here alert = new Alert("alert", "恭喜你答對啦", null, null);//GEN-BEGIN:|28-getter|1|28-postInit alert.addCommand(getOkCommand1()); alert.addCommand(getExitCommand1()); alert.setCommandListener(this); alert.setTimeout(5000);//GEN-END:|28-getter|1|28-postInit // write post-init user code here }//GEN-BEGIN:|28-getter|2| return alert; } //</editor-fold>//GEN-END:|28-getter|2| /** * Returns a display instance. * @return the display instance. */ public Display getDisplay () { return Display.getDisplay(this); } /** * Exits MIDlet. */ public void exitMIDlet() { switchDisplayable (null, null); destroyApp(true); notifyDestroyed(); } /** * Called when MIDlet is started. * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet. */ public void startApp() { if (midletPaused) { resumeMIDlet (); } else { initialize (); startMIDlet (); rankGenerator(); } midletPaused = false; } /** * Called when MIDlet is paused. */ public void pauseApp() { midletPaused = true; } /** * Called to signal the MIDlet to terminate. * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released. */ public void destroyApp(boolean unconditional) { } public void rankGenerator(){ tfAns.setString(""); // tfAns.setConstraints(TextField.PASSWORD | tfAns.getConstraints()); Random random = new Random(); int i=0; while(i<=3){ //可產生0~9的數字; int intRank = Math.abs(random.nextInt() % 9); String strAns=tfAns.getString(); if(strAns.indexOf(""+intRank)==-1){ tfAns.setString(tfAns.getString() + (int) intRank); i++; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -