Java me Java ME:ActionEvent,如超出范围/多个表单未正确设置

Java me Java ME:ActionEvent,如超出范围/多个表单未正确设置,java-me,Java Me,前言:JavaME、Lwiit、内存受限(

前言:JavaME、Lwiit、内存受限(<4mb,甚至一个容器/盒子布局也超过了内存限制)

给定:多个表单,每个表单包含唯一的操作(通过按钮和/或命令)

任务:如何确定从哪个表单动作出现(比如:?(currentForm==formX))

问题:一个不存在的源(以给定的当前形式)像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
        }
        ...
    }
    ...
}
这是我在这一点上的肮脏解决方案:

  • 按用户流排序事件,并使用if/else构造
  • 使用“if或switch/case(badeidea==x)”并确定我是以哪种方式生成的表单(在表单发生更改时设置“badeidea=currentID”)
  • 使用以下命令:

    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 
               ...
            }
        });