0

Preface: Java ME, LWUIT, restricted memory (< 4mb, even a Container/BoxLayout exceeds memory limit)

Given: more then one form, each form contains unique actions (by buttons and/or commands)

Quest: How to determine from which form action appears (like: ?(currentForm == formX))

Problem: a not existing source (at a given, current form) is passing by like true

public class expMIDlet extends MIDlet implements ActionListener {
    ...
    Form expTimerForm;
    Form presetForm;
    Form slimForm;
    ...
    public void actionPerformed(ActionEvent ae) {
        ...
        if (ae.getSource() == calcButton) {
            ...
        }
        if (ae.getCommand() == command2) {
            ...
            // this will be EVEN executed in a Form that do not contain command2
            // if there was some event in that Form
        }
        ...
    }
    ...
}

this are my dirty solutions at that point:

  1. order the events by user flow AND use an if/else construct
  2. use "if or switch/case (badIdea == x)" and determine that way in which form i am (by setting "badIdea = currentID" ever if the form is changed)
4

1 回答 1

0

用这个:

public class expMIDlet extends MIDlet implements ActionListener {
...
Form expTimerForm;
Form presetForm;
Form slimForm;
...

public void actionPerformed(ActionEvent ae) {


    if(evt.getSource() instanceof Command){
        Command command = (Command) evt.getSource();
        if (command == command2) {}
        ...
    }
    if(evt.getSource() instanceof Component)
    {  
        Component component = (Component) evt.getSource();
        //known in which this component is contained
        if(component.getParent() == presetForm){
          if (component == calcButton) {
           ...
          }
          ...
        }
        if(component.getParent() == slimForm){...}
        if(component.getParent() == expTimerForm){...}
    }

}

您可以对任何组件或命令使用匿名 actionListener。喜欢:

calcButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
           //Do something 
           ...
        }
    });
于 2013-08-20T19:34:47.040 回答