2015-09-13 52 views
1

我有一個畫布J2ME項目,我想刪除命令,當他們使用一次! 我寫了這個void,但它不起作用!從j2me的畫布中刪除命令

 public void removeCmd(){ 
      m_canvas.removeCommand(ersal); 
      m_canvas.removeCommand(virayesh); 
      m_disp.setCurrent(m_canvas); 
      repaint(); 
     } 

,這是我的完整代碼:

import javax.microedition.midlet.MIDlet; 
import javax.microedition.midlet.MIDletStateChangeException; 
import javax.microedition.lcdui.*; 
import com.sun.midp.io.j2me.comm.WAP; 
import com.sun.midp.io.j2me.comm.SDA; 

public class Demo extends MIDlet 
{ 
    Command ersal = new Command("Ersal", Command.STOP, 1); 
    Command virayesh = new Command("Virayesh", Command.SCREEN, 1); 

    private Canvas m_canvas = new DemoCanvas(); 
private Display m_disp; 
    int v = 0; 

    String a; 
    int step = -1; 
public Demo() { 
    // TODO Auto-generated constructor stub 
    m_disp = Display.getDisplay(this); 
    m_disp.setCurrent(m_canvas);   
} 
private class DemoCanvas extends Canvas implements CommandListener 
{ 

private String info = "Barname Estelam\n*:Meno Aval\n\nYek dokme ra\nfeshar dahid"; 

    public DemoCanvas(){ 

     } 

    public void paint(Graphics g) 
    { 

    g.setColor(0xFFFFFF); 
    g.fillRect(0, 0, this.getWidth(), this.getHeight()); 
    g.setColor(0);  
    g.drawString(info, 0, 5, Graphics.LEFT|Graphics.TOP); 

     } 
     public void removeCmd(){ 
      m_canvas.removeCommand(ersal); 
      m_canvas.removeCommand(virayesh); 
      m_disp.setCurrent(m_canvas); 
      repaint(); 
     } 
    protected void keyPressed(int keyCode) 
    { 
      if(keyCode==42 || step==-1){//star key 
       info="1:moshakhasat\nkhodro \n2:estelam taghib \n3:etelaat malek \n4:estelam khalafi"; 
       step=0; 
       a=""; 
       removeCmd(); ///Here i gonna remove the commands! or when "Ersal" Command put... 
       repaint(); 
       WAP.WAP_InputMethodContents(""); 
      } 
      String content = WAP.GetT9String(); 
      if(step==2){ 
       step=3; 
       //tayid T9 
       m_canvas.addCommand(ersal); //Here Commands added 
       m_canvas.addCommand(virayesh); 
       m_canvas.setCommandListener(new CommandListener() { 

    public void commandAction(Command c, Displayable d) { 
     String lbl = c.getLabel(); 
     if(lbl.equals("Ersal")){ 
     String m = WAP.GetT9String(); 
     SDA.SDS_SendMsg("20002",m); 
     removeCmd(); 
     info = WAP.GetT9String() + "\nersal shod" + "\n\n*:meno aval"; 
     repaint(); 
     }   else if(lbl.equals("Virayesh")){ 
      step=2; 
      WAP.WAP_InputMethodContents(""); 
      WAP.SwitchToT9InputMethod(0); 
     } 
     } 
}); 
      } 
    public void commandAction(Command c, Displayable d) {} 


    } 
protected void destroyApp(boolean unconditional) 
    throws MIDletStateChangeException { 
    // TODO Auto-generated method stub 
     notifyDestroyed(); 

} 

    protected void pauseApp() {} 
protected void startApp() throws MIDletStateChangeException { 
    // TODO Auto-generated method stub 
} 

回答

0

這裏是你如何能實現你想要的一個例子。我使用了一個Form,因爲Canvas擴展了Displayable,它的工作原理是一樣的。這段代碼刪除了命令行,同樣的邏輯可以應用於任意數量的命令。

Form myForm = new Form("Test form"); 
Command ok = new Command("OK",Command.OK,0); 
Command cancel = new Command("CANCEL",Command.CANCEL,1); 
myForm.addCommand(ok); 
myForm.addCommand(cancel);  
myForm.setCommandListener(new CommandListener(){ 
    public void commandAction(Command c, Displayable d) { 
     if(c.getLabel().equals("OK")){ 
      //Do something... 
      myForm.removeCommand(c);//remove the command ok 
     } 
    } 
});