在lWUIT中,如何通过单击后退命令来调用主MIDlet类?

时间:2011-07-04 13:11:46

标签: java-me lwuit

我的问题是如何通过单击MIDlet来调用主command课程? 假设MainMIDlet.java此类扩展Form并实现ActionListenerAboutus.java此类扩展还包括Form和实现ActionListener。在这堂课中,我没有创造形式的对象。那么在本课程中,如何在点击MainMIDlet后退按钮时调用Commmand类?

1 个答案:

答案 0 :(得分:2)

调用Aboutus.java时传递MainMIDlet表单实例。 例如,

<强> MainMIDlet.java

 public class MainMIDlet extends MIDlet implements ActionListener {
    Form form = new form();
    ...
    ...

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

            if (cmdname.equals("Aboutus"))
            {
                 Aboutus aboutus = new Aboutus(form); // pass the current form
                 aboutus.show();
            }
        }
}

<强> Aboutus.java

public class Aboutus extends Form implements ActionListener {

Form mainform;

 public Aboutus(Form form) {
   this.mainform = form;
   ...
   ...
   Command backCommand = new Command("Back",null,1);
   this.setBackCommand(backCommand);
 }
    ...
    ...

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

            if (cmdname.equals("Back"))
            {
                 mainform.showBack(); // show the Main Midlet form here
            }
        }
}