LWUIT与LCDUI的结合

    技术2022-05-19  20

    Lwuit与Lcdui可以相互调用,如果需要在一个Midlet中同时使用Lwuit和Lcdui库,需要在Midlet中保留一个Lcdui Display的全局变量,可以在startApp方法中初始化。示例如下:

    private javax.microedition.lcdui.Display lcduiDisplay;   //....   public void startMIDlet() { //...      lcduiDisplay = Display.getDisplay( this ); //... } 如果是从Lwuit切换到Lcdui,通过使用Lcdui Display的setCurrent方法,示例如下: lcduiDisplay.setCurrent(lcduiForm); 如果是从Lcdui切换到Lwuit,先初始化Lwuit Display,然后调用Component的show方法,示例如下: com.sun.lwuit.Display.init( this ); com.sun.lwuit.Form lwuitForm = new com.sun.lwuit.Form( "LWUIT FORM" ); //... lwuitForm.show();

    附源代码:

    /*   * To change this template, choose Tools | Templates   * and open the template in the editor.   */   package DemoLwuitLcdui;   import com.sun.lwuit.events.ActionEvent; import javax.microedition.midlet.*; import javax.microedition.lcdui.*;   /**   * @author Administrator   */ public class DemoLwuitLcduiMIDlet extends MIDlet implements CommandListener {        private boolean midletPaused = false;        //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|      private Command exitCommand;      private Command switchToLwuitFormCommand;      private Form lcduiForm;      private StringItem stringItem;      //</editor-fold>//GEN-END:|fields|0|        private com.sun.lwuit.Display lwuitDisplay;      private javax.microedition.lcdui.Display lcduiDisplay;      /**       * The DemoLwuitLcduiMIDlet constructor.       */      public DemoLwuitLcduiMIDlet() {      }        //<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, getLcduiForm());//GEN-LINE:|3-startMIDlet|1|3-postAction          //lwuitDisplay = com.sun.lwuit.Display.getInstance();          lcduiDisplay = Display.getDisplay(this);          // 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 == lcduiForm) {//GEN-BEGIN:|7-commandAction|1|19-preAction              if (command == exitCommand) {//GEN-END:|7-commandAction|1|19-preAction                  // write pre-action user code here                  exitMIDlet();//GEN-LINE:|7-commandAction|2|19-postAction                  // write post-action user code here              } else if (command == switchToLwuitFormCommand) {//GEN-LINE:|7-commandAction|3|23-preAction                  // write pre-action user code here                  com.sun.lwuit.Display.init(this);                  com.sun.lwuit.Form lwuitForm = new com.sun.lwuit.Form("LWUIT FORM");                  lwuitForm.addComponent(new com.sun.lwuit.Label("Thi is a Lwuit Form."));                  lwuitForm.addComponent(new com.sun.lwuit.Label("Press Lcdui Form Command to "));                  lwuitForm.addComponent(new com.sun.lwuit.Label("switch to Lcdui Form."));                  com.sun.lwuit.Command switchToLcduiFormCommand = new com.sun.lwuit.Command("Lcdui Form");                  lwuitForm.addCommand(switchToLcduiFormCommand);                  lwuitForm.addCommandListener(new com.sun.lwuit.events.ActionListener() {                        public void actionPerformed(ActionEvent ae) {                          if (ae.getCommand().getCommandName().equals("Lcdui Form")) {                              lcduiDisplay.setCurrent(lcduiForm);                          }                      }                  });                  lwuitForm.show(); //GEN-LINE:|7-commandAction|4|23-postAction                  // write post-action user code here              }//GEN-BEGIN:|7-commandAction|5|7-postCommandAction          }//GEN-END:|7-commandAction|5|7-postCommandAction          // write post-action user code here      }//GEN-BEGIN:|7-commandAction|6|      //</editor-fold>//GEN-END:|7-commandAction|6|        //<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>        //<editor-fold defaultstate="collapsed" desc=" Generated Getter: lcduiForm ">//GEN-BEGIN:|14-getter|0|14-preInit      /**       * Returns an initiliazed instance of lcduiForm component.       * @return the initialized component instance       */      public Form getLcduiForm() {          if (lcduiForm == null) {//GEN-END:|14-getter|0|14-preInit              // write pre-init user code here              lcduiForm = new Form("LCDUI Form", new Item[] { getStringItem() });//GEN-BEGIN:|14-getter|1|14-postInit              lcduiForm.addCommand(getExitCommand());              lcduiForm.addCommand(getSwitchToLwuitFormCommand());              lcduiForm.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit              // write post-init user code here          }//GEN-BEGIN:|14-getter|2|          return lcduiForm;      }      //</editor-fold>//GEN-END:|14-getter|2|        //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem ">//GEN-BEGIN:|16-getter|0|16-preInit      /**       * Returns an initiliazed instance of stringItem component.       * @return the initialized component instance       */      public StringItem getStringItem() {          if (stringItem == null) {//GEN-END:|16-getter|0|16-preInit              // write pre-init user code here              stringItem = new StringItem("This is a Lcdui Form", "Press Lwuit Form Command to switch to lwuit form");//GEN-LINE:|16-getter|1|16-postInit              // write post-init user code here          }//GEN-BEGIN:|16-getter|2|          return stringItem;      }      //</editor-fold>//GEN-END:|16-getter|2|      //</editor-fold>        //<editor-fold defaultstate="collapsed" desc=" Generated Getter: switchToLwuitFormCommand ">//GEN-BEGIN:|22-getter|0|22-preInit      /**       * Returns an initiliazed instance of switchToLwuitFormCommand component.       * @return the initialized component instance       */      public Command getSwitchToLwuitFormCommand() {          if (switchToLwuitFormCommand == null) {//GEN-END:|22-getter|0|22-preInit              // write pre-init user code here              switchToLwuitFormCommand = new Command("LwuitForm", Command.OK, 0);//GEN-LINE:|22-getter|1|22-postInit              // write post-init user code here          }//GEN-BEGIN:|22-getter|2|          return switchToLwuitFormCommand;      }      //</editor-fold>//GEN-END:|22-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 ();          }          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) {      }  

    }


    最新回复(0)