2013-08-20 31 views
0

前言:Java ME的,LWUIT,限制存儲器(< 4MB,即使一個集裝箱/ BoxLayout的超過存儲器限制)的Java ME:ActionEvent的像超出範圍/多種形式不能很好地設置

給定:一個以上的形式,每種形式含有獨特的動作(由按鈕和/或命令)

任務:如何確定從顯示形式操作(例如:(currentForm == formX))

問題:一個不存在源(在給定的,當前的形式)正在經過像真的

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

這是我在這一點骯髒的解決方案:

  1. 順序由用戶流事件和使用if/else結構
  2. 使用「if或switch /箱(badIdea == X) 「並確定以何種方式形成我是(通過設置‘badIdea = currentID’如果有的話的形式被改變)

回答

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

.getSource(),然後.getParent() - 謝謝! –