LWUIT:需要帮助 `Back` 命令无法正常工作

Posted

技术标签:

【中文标题】LWUIT:需要帮助 `Back` 命令无法正常工作【英文标题】:LWUIT: Need help `Back` command is not working properly 【发布时间】:2011-12-13 08:03:53 【问题描述】:

我们使用表单显示 SPLASH 屏幕,然后是 MENU 表单,其中包含许多按钮,例如关于我们、登录、反馈等。 现在我转到包含menuback 命令的LOGIN 表单。我的问题是当back 以这种形式单击命令时,它会转到SPLASH 屏幕。

预期的行为是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();
        

    

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

【问题讨论】:

有很多未知的和场景会导致这个问题?请提供您的代码 sn-p 以获得更多帮助? 感谢重播,我在这里添加了一些代码!请帮助我 我编辑了my answer。那里的问题解决说明。 非常欢迎,如果你能解决你的问题,请标记答案和accept it,以帮助这篇文章的未来读者。 【参考方案1】:

知道了!

MainMenu(...) 构造函数中,您将SPLASH 屏幕的引用存储在类变量frm_Main 中,您将这个类变量传递给MainMenu.actionPerformed(...) 中的Login(...) 构造函数,因此您正在有效地传递引用Splash 表单对象。您需要将当前类 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`*/);

【讨论】:

@HarshadaDhamapurkar 如果您认为它有助于解决您的问题,请标记并接受此答案。

以上是关于LWUIT:需要帮助 `Back` 命令无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

LWUIT 本地化菜单栏

如何在 LWUIT 中的表单之间切换

LWUIT:将 GIF 图像添加到标签

如何从 LWUIT 中删除返回命令

LWUIT 形式的左右命令菜单

LWUIT 桂生成器