2011-12-13 102 views
0

我们展示使用形式的SPLASH屏幕,随后与MENU形成了保持像对美国,登录,反馈等 现在我谨许多按钮以LOGIN格式保存menuback命令。我的问题是,当back命令被点击在这种形式,它会去SPLASH屏幕。LWUIT:需要帮助`Back`命令不能正常工作

预期的行为是在LOGIN表单中的回拨命令必须回到MENU的形式而不是SPLASH的形式。

如何做到这一点?

Splash.java

public class Splash extends MIDlet implements ActionListener { 
    Image img_Splash; 
    Form frm_Main; 
    String str_URL; 
    int Width, Height; 
    Label lbl_Splash; 
    Command cmd_Cancel; 

    public void startApp() { 
     Display.init(this); 

     frm_Main = new Form(); 
     try { 
      //Resources theme = Resources.open("/44444.res"); 
      img_Splash = Image.createImage("/res/splash.png"); 
      Resources theme = Resources.open("/666666.res"); 
      UIManager.getInstance().setThemeProps(
        theme.getTheme(theme.getThemeResourceNames()[0])); 
     } catch (IOException io) { 
      io.printStackTrace(); 
      Dialog.show("Theme Exception", io.getMessage(), "Ok", null); 
     } 


     lbl_Splash = new Label(img_Splash); 
     lbl_Splash.getStyle().setBgColor(16777215); 

     frm_Main.addComponent(lbl_Splash); 
     frm_Main.setScrollable(true); 

     cmd_Cancel = new Command("Cancel"); 
     frm_Main.addCommand(cmd_Cancel); 
     frm_Main.addCommandListener(this); 

     frm_Main.show(); 

     // th 
     Thread splashThread = new Thread() { 

      public void run() { 
       try { 
        sleep(5000); 
       } catch (InterruptedException ex) { 
        ex.printStackTrace(); 

       } finally { 
        MainMenu mainmenu = new MainMenu(frm_Main); 
        mainmenu.show(); 
       } 
      } 
     }; 
     splashThread.start(); 
    } 

    public void pauseApp() { 
    } 

    public void destroyApp(boolean unconditional) { 
     //notifyDestroyed(); 
    } 

    public void actionPerformed(ActionEvent ae) { 
     Command con = ae.getCommand(); 
     String str_CmdName = con.getCommandName(); 

     if (str_CmdName.equals("Cancel")) { 
      notifyDestroyed(); 
     } 
    } 
} 

Mainmenu.java

class MainMenu extends Form implements ActionListener { 

    Form frm_Main; 
    //MainMidlet main; 
    public Button btn_main_Login, btn_main_NewUser, btn_main_Help; 
    public Label lbl_main_Login, lbl_main_NewUser, lbl_main_Help; 

    public Image img_main_Login, img_main_NewUser, img_main_Help; 
    public Command cmd_Exit, cmd_Select; 

    public MainMenu(Form form) { 
     this.frm_Main = form; 
     try { 
      img_main_Login = Image.createImage("/res/btn_main_login.png"); 
      img_main_NewUser = Image.createImage("/res/btn_main_newuser.png"); 
      img_main_Help = Image.createImage("/res/btn_main_help.png"); 
     } catch (IOException io) { 
      io.printStackTrace(); 
      //Dialog.show("Image not Found!", io.getMessage(), "Ok", null); 
      System.out.println("Image not found" + io.getMessage()); 
     } 


     //--------- Whole form define as BorderLayout with three Container. 
     BorderLayout bor_MainMenuLayout = new BorderLayout(); 
     setLayout(bor_MainMenuLayout); 
     setScrollable(false); 

     //----------- Define Container for Header On North 
     Container con_Header = new Container(); 
     lbl_Header = new Label(img_Header); 
     con_Header.addComponent(lbl_Header); 


     //----------- Define Container as GridLayout and place 
     //----------- on BorderLayout Center position 
     //---------  Define Container(CENTER) for search details 
     //---------  with BoxLayout 
     BoxLayout bx_CenterLayout = new BoxLayout(BoxLayout.Y_AXIS); 
     Container con_Center = new Container(bx_CenterLayout); 
     con_Center.setScrollableY(true); 
     con_Center.getStyle().setPadding(0, 10, 3, 3); 

     //---------  FlowLayout for Header Details 
     FlowLayout flw_MenuHdr = new FlowLayout(); 
     Container con_HdrDetails = new Container(flw_MenuHdr); 
     lbl_MenuHdr = new Label("Menu"); 
     lbl_MenuHdr.getStyle().setBgTransparency(0); 
     lbl_MenuHdr.getStyle().setPadding(0, 5, 10, 0); 

     con_HdrDetails.addComponent(lbl_MenuHdr); 

     con_Center.addComponent(con_HdrDetails); 

     //----------  GridLayout define 1 rows and 3 coloumn 
     //----------  First row 
     GridLayout grd_Row1Layout = new GridLayout(1, 3); 
     Container con_Row1Layout = new Container(grd_Row1Layout); 

     btn_main_Login = new Button(img_main_Login); 
     btn_main_Help = new Button(img_main_Help; 
     btn_main_NewUser = new Button(img_main_NewUser); 

     con_Row1Layout.addComponent(btn_main_Login); 
     con_Row1Layout.addComponent(btn_main_Help); 
     con_Row1Layout.addComponent(btn_main_NewUser); 

     con_Center.addComponent(con_Row1Layout); 



     //----------- Add Command on softkey 
     cmd_Exit = new Command("Exit"); 
     cmd_Select = new Command("Select"); 

     addCommand(cmd_Select); 
     addCommand(cmd_Exit); 
     addCommandListener(this); 

     addComponent(BorderLayout.NORTH, con_Header); 
     addComponent(BorderLayout.CENTER, con_Center); 

     //this.getStyle().setBgColor(12105912); 
     frm_Main.getStyle().setBgColor(12105912); 

    } 

    public void actionPerformed(ActionEvent ae) { 
     Command cmd = ae.getCommand(); 
     String str_CmdName = cmd.getCommandName(); 

     //-----------  When Click on Exit Button 
     if (str_CmdName.equals("Exit")) { 
      //System.exit(0); 
      //frm_Main.notifyAll(); 
     } 

     //-----------  When Click on Select Button 
     if (str_CmdName.equals("Select")) { 
      //-----------  When Click on Login 
      if (btn_main_Login.hasFocus()) { 
       String str_MemberId = GlobalClass.getInstance().getMemberId(); 

       if (!(str_MemberId == "" || str_MemberId == null)) { 
        SearchDiamond searchdiamond = new SearchDiamond(frm_Main); 
        searchdiamond.setTransitionInAnimator(
          Transition3D.createRotation(800, true)); 
        searchdiamond.show(); 
       } else { 
        Login login = new Login(frm_Main); 
        login.setTransitionInAnimator(
          Transition3D.createRotation(800, true)); 
        login.show(); 
       } 

      } 

      //-----------  When Click on New User 
      if (btn_main_NewUser.hasFocus()) { 
       //Dialog.show("New User", "NewUser", "Ok", null); 
       NewUser newuser = new NewUser(frm_Main); 
       //newuser.setTransitionInAnimator(
         CommonTransitions.createFade(1500)); 
       newuser.setTransitionInAnimator(
         Transition3D.createRotation(800, true)); 
       newuser.show(); 

      } 

      //-----------  When Click on Help 
      if (btn_main_Help.hasFocus()) { 
       Help help = new Help(frm_Main); 
       help.setTransitionInAnimator(
         Transition3D.createRotation(800, true)); 
       help.show(); 
      } 

      //-----------  When Click on Exit 
      if (btn_main_Exit.hasFocus()) { 
       System.exit(0); 
      } 

     } 
    } 
} 

Login.java

class Login extends Form implements ActionListener { 

    Form frm_Main; 
    Label lbl_Header, lbl_Footer, lbl_SepLine, lbl_HdrAlreadyMember, lbl_UserID, lbl_PWD, 
      lbl_Blank2, lbl_Blank3, lbl_HdrNewMember, lbl_Info; 
    TextField txtf_UserID, txtf_PWD; 
    Button btn_Login; 
    Command cmd_Back, cmd_AboutUs, cmd_Privacy; 

    public Login(Form form) { 
     this.frm_Main = form; 

     //--------- Whole form define as BorderLayout with three Container. 
     BorderLayout bor_LoginLayout = new BorderLayout(); 
     setLayout(bor_LoginLayout); 
     setScrollable(false); 

     //--------- Define Container(NORTH) for Header. 
     Container con_Header = new Container(); 
     lbl_Header = new Label(); 
     con_Header.addComponent(lbl_Header); 


     //---------  Define Container(CENTER) for search details 
     //---------  with BoxLayout 
     BoxLayout bx_CenterLayout = new BoxLayout(BoxLayout.Y_AXIS); 
     Container con_Center = new Container(bx_CenterLayout); 
     con_Center.setScrollableY(true); 
     con_Center.getStyle().setMargin(5, 5, 0, 0); 

     //---------  FlowLayout for Header Details 
     FlowLayout flw_HdrLayout = new FlowLayout(); 
     Container con_HdrDetails = new Container(flw_HdrLayout); 

     lbl_HdrAlreadyMember = new Label("Already a Member?"); 
     lbl_HdrAlreadyMember.setAlignment(Component.LEFT); 
     con_HdrDetails.addComponent(lbl_HdrAlreadyMember); 

     con_Center.addComponent(con_HdrDetails); 

     //---------  BoxLayout for Login details 
     BoxLayout bx_Login1Layout = new BoxLayout(BoxLayout.Y_AXIS); 
     Container con_Login1Details = new Container(bx_Login1Layout); 
     con_Login1Details.setScrollableY(true); 


     lbl_UserID = new Label("User Name:"); 


      txtf_UserID = new TextField(""); 
     txtf_UserID.setFocusable(true); 
     txtf_UserID.setFocus(true); 
     txtf_UserID.setConstraint(TextField.ANY); 
     txtf_UserID.setInputMode("abc"); 

     lbl_PWD = new Label("Password:"); 

     txtf_PWD = new TextField(""); 
     txtf_PWD.setConstraint(TextField.PASSWORD); 


     con_Login1Details.addComponent(lbl_UserID); 
     con_Login1Details.addComponent(txtf_UserID); 
     con_Login1Details.addComponent(lbl_PWD); 
     con_Login1Details.addComponent(txtf_PWD); 

     //---------  BoxLayout for Login Button 
     FlowLayout flw_LoginBtnLayout = new FlowLayout(CENTER); 
     Container con_LoginBtnDetails = new Container(
       flw_LoginBtnLayout); 
     con_LoginBtnDetails.setScrollableY(true); 

     btn_Login = new Button("Login"); 
     btn_Login.setPreferredSize(new Dimension(80, 25)); 
     btn_Login.setAlignment(Component.CENTER); 

     con_LoginBtnDetails.addComponent(btn_Login); 

     con_Login1Details.addComponent(con_LoginBtnDetails); 

     con_Center.addComponent(con_Login1Details); 

     //---------  BoxLayout for other Login details 
     BoxLayout bx_Login2Layout = new BoxLayout(BoxLayout.Y_AXIS); 
     Container con_Login2Details = new Container(bx_Login2Layout); 
     con_Login2Details.setScrollableY(true); 
     con_Login2Details.getStyle().setMargin(5, 5, 10, 10); 

     //---------  Login type 3 New Remember Me 
     chk_RememberMe = new CheckBox("Remember Me"); 
     chk_RememberMe.setTickerEnabled(false); 
     con_Login2Details.addComponent(chk_RememberMe); 

     //---------  Login type 3 New Forgot Password 
     btn_ForgotPWD = new Button("Forgot Password?"); 
     btn_ForgotPWD.setTickerEnabled(false); 
     btn_ForgotPWD.setAlignment(Component.LEFT); 
     btn_ForgotPWD.setTextPosition(Component.RIGHT); 

     con_Login2Details.addComponent(btn_ForgotPWD); 

     con_Center.addComponent(con_Login2Details); 
     con_Center.getStyle().setPadding(0, 0, 10, 10); 

     addComponent(BorderLayout.NORTH, con_Header); 
     addComponent(BorderLayout.CENTER, con_Center); 

     cmd_Back = new Command("Back"); 
     this.setBackCommand(cmd_Back); 
     addCommand(cmd_Back); 
     addCommandListener(this); 


     //---------- Add More Command for Menu item 
     cmd_AboutUs = new Command("About Us"); 
     cmd_Privacy = new Command("Privacy"); 

     addCommand(cmd_AboutUs); 
     addCommand(cmd_Privacy); 

     //-------  When Login Button is Clicked 
     btn_Login.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent evt) { 
        .... 
      } 
     }); 
    } 
    public void actionPerformed(ActionEvent ae) { 
     Command con = ae.getCommand(); 
     String str_CmdName = con.getCommandName(); 
     if (str_CmdName.equals("Back")) { 
      // here back code 
      frm_Main.setTransitionInAnimator(
         Transition3D.createRotation(800, true)); 
      frm_Main.showBack(); 
     } 

     .... 
     if (str_CmdName.equals("About Us")) { 
      AboutUs aboutus = new AboutUs(frm_Main); 
      aboutus.show(); 
     } 

     if (str_CmdName.equals("Privacy")) { 
      Privacy privacy = new Privacy(frm_Main); 
      privacy.show(); 
     } 

    } 
} 

编辑:添加了此表单的源代码

+0

有很多未知的和可能导致此问题的情况?请提供您的代码片段以获取更多帮助? – Vimal

+0

感谢您的重播,我在这里添加了一些代码! PLZ帮助我 –

+0

我编辑了[我的答案](http://stackoverflow.com/questions/8485998/lwuit-need-help-back-command-is-not-working-properly/8489653#8489653)。在那里的问题解决描述。 – Vimal

回答

2

知道了!

MainMenu(...)构造要存储SPLASH屏幕的类变量frm_Main基准,要传递到所述Login(...)构造在MainMenu.actionPerformed(...)如此有效地要传递飞溅形式的对象的参考这个类变量。您需要将当前课程的MainMenu's参考文件传递给Login(...)构造函数,而不是MainMenu.frm_Main

做到这一点,如果你不期望从MainMenu屏幕回到了Splash屏幕:

public MainMenu(Form form) 
{ 
    this.frm_Main = this; //by referring to `MainMenu.this` and not input 
          //parameter `form` which is referring to Splash 
          //object 

    ... 

如果你想保持MainMenu的屏幕比MainMenu.actionPerformed(...)

做到这一点后面的参考
Login login = new Login(/*global variable for `MainMenu.this`*/); 
+0

@HarshadaDhamapurkar请标记并接受这个答案,如果你觉得它有助于解决你的问题。 – Vimal