我的漫漫程序之旅

          專注于JavaWeb開發
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數據加載中……

          我的第一個J2ME 程序

          package com;

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

          /*******************************************************************************
           * 
           * 
          @author zdw
           * 
           
          */

          public class TextTest extends MIDlet implements CommandListener
          {
              
          // 文本框
              private TextBox tbx = null;
              
          // 控制輸入輸出的類
              private Display display = Display.getDisplay(this);
              
          // 命令菜單(清除)
              private Command clear;
              
          // 得到鼠標位置
              private Command getCursorPos;
              
          // 輸入限制
              private Command astrict;
              
          // 發送
              private Command send;
              
          // 退出
              private Command exit;

              
          public TextTest()
              
          {
                  
          // 初始化textBox
                  tbx = new TextBox("測試標題""測試內容"200, TextField.ANY);
                  
          // 設置為當前顯示
                  display.setCurrent(tbx);
                  
          // 清除菜單
                  clear = new Command("清空", Command.SCREEN, 1);
                  
          // 光標位置菜單
                  getCursorPos = new Command("光標位置", Command.SCREEN, 1);
                  
          // 輸入限制菜單
                  astrict = new Command("只能輸入數字", Command.SCREEN, 1);
                  
          // 發送菜單
                  send = new Command("發送", Command.SCREEN, 1);
                  
          // 退出菜單
                  exit = new Command("退出", Command.EXIT, 1);

                  tbx.addCommand(clear);
                  tbx.addCommand(getCursorPos);
                  tbx.addCommand(astrict);
                  tbx.addCommand(send);
                  tbx.addCommand(exit);
                  
          // 添加Ticker(顯示在TextBox上方)
                  tbx.setTicker(new Ticker("短信編輯器"));
                  
          // 添加事件監聽器
                  tbx.setCommandListener(this);
              }


              
          // Alert的初始函數
              public void initAlert()
              
          {
                  Alert alert 
          = new Alert("提示""發送成功"null, AlertType.INFO);
                  alert.setTimeout(Alert.FOREVER);
                  display.setCurrent(alert);
              }


              
          // 事件處理
              public void commandAction(Command cmd, Displayable dis)
              
          {
                  
          if (cmd == getCursorPos)
                  
          {
                      System.out.println(
          "光標位置為:" + tbx.getCaretPosition());
                  }

                  
          if (cmd == clear)
                  
          {
                      tbx.setString(
          "");
                  }

                  
          if (cmd.getLabel().equals("只能輸入數字"))
                  
          {
                      tbx.setConstraints(TextField.DECIMAL);
                      tbx.removeCommand(astrict);
                      astrict 
          = new Command("取消限制", Command.SCREEN, 1);
                      tbx.addCommand(astrict);
                  }

                  
          if (cmd.getLabel().equals("取消限制"))
                  
          {
                      tbx.setConstraints(TextField.ANY);
                      tbx.removeCommand(astrict);
                      astrict 
          = new Command("只能輸入數字", Command.SCREEN, 1);
                      tbx.addCommand(astrict);
                  }

                  
          if (cmd.getCommandType() == Command.EXIT)
                  
          {
                      
          this.notifyDestroyed();
                  }

                  
          if (cmd == send)
                  
          {
                      
          this.initAlert();
                  }

              }


              
          protected void destroyApp(boolean arg0) throws MIDletStateChangeException
              
          {

              }


              
          protected void pauseApp()
              
          {

              }


              
          protected void startApp() throws MIDletStateChangeException
              
          {

              }


          }



          posted on 2008-06-23 12:55 々上善若水々 閱讀(1066) 評論(0)  編輯  收藏 所屬分類: J2SEJ2ME

          主站蜘蛛池模板: 安溪县| 玛曲县| 东乌珠穆沁旗| 百色市| 高清| 阿拉尔市| 枝江市| 壤塘县| 皮山县| 上杭县| 金寨县| 长岛县| 虎林市| 包头市| 右玉县| 千阳县| 垫江县| 泸州市| 麻城市| 扬州市| 浑源县| 左贡县| 双桥区| 繁昌县| 刚察县| 双牌县| 收藏| 安丘市| 宝坻区| 安福县| 色达县| 河间市| 登封市| 开江县| 行唐县| 绥滨县| 靖西县| 昭通市| 康定县| 蕉岭县| 凌云县|