简体   繁体   中英

command doesn't work in a canvas j2me program

i made a canvas J2ME program and i used key press and key code to complete the program! now i have a big problem with two screen commands! I need to use the command labels "Ersal" and "Virayesh" as below code but the command code doesn't work! i could use the key Codes(-6) but then i don't have command labels in the screen. So whats your solution? can i just add two labels in the screen not command?! or how can i active these command void!

    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;

     public static final int IME_NOTIFY = -6;
     public static final int KEY_ASTERISK = 42;
     public static final int KEY_HASH = 35;

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

           /*
           if(step==-1){
               step=0;
           try {
                Thread.sleep(5000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }


      info = "1:moshakhasat\nkhodro \n2:estelam taghib \n3:etelaat malek \n4:estelam khalafi";
           repaint();   
           }
            * 
            */

          }

      protected void keyPressed(int keyCode)
      {
     /*
            if(IME_NOTIFY == keyCode){
                String m = WAP.GetT9String();
                SDA.SDS_SendMsg("20002",m);
                info = WAP.GetT9String() + "\nersal shod";
            }

     *
     */


              if(keyCode==42 || step==-1){
                  info="1:moshakhasat\nkhodro \n2:estelam taghib \n3:etelaat malek \n4:estelam khalafi";
                  step=0;
                  a="";
                  m_canvas.removeCommand(ersal);
                  m_canvas.removeCommand(virayesh);
                  //m_disp.setCurrent(m_canvas);
                  WAP.WAP_InputMethodContents("");
              }
              String content = WAP.GetT9String();
              if(step==2){
                  step=3;
                  //tayid T9
                  m_canvas.addCommand(ersal);
                  m_canvas.addCommand(virayesh);


                  String n = WAP.GetT9String();

                  if(a.equals("11") && n.length()>7)
                  info = "moshakhasat\nkhodro\n\npelak:\n" + pelakSp(content);

                  if(a.equals("12") && n.length()>7)
                  info = "moshakhasat\nkhodro\n\nshomare shasi:\n" + content;

                  if(a.equals("13") && n.length()>7)
                  info = "moshakhasat\nkhodro\n\nshomare motor:\n" + content;

                  if(a.equals("21") && n.length()>7)
                  info = "estelam taghib\n\npelak:\n" + pelakSp(content);

                  if(a.equals("31") && n.length()>7)
                  info = "etelaat malek\n\npelak:\n" + pelakSp(content);

                  if(a.equals("41") && n.length()>7)
                  info = "estelam khalafi\n\npelak:\n" + pelakSp(content);

                  repaint();
              }


              if(step==1){
                if(keyCode==49){
                step=2;
                a+="1";
                //pelak
                String c = info;

                WAP.WAP_InputMethodContentsLength(8);
                WAP.SwitchToT9InputMethod(0);
                //bazgasht T9
                String b = WAP.GetT9String();

                repaint();
                }
                if(keyCode==50 && a.equals("1")){
                step=2;
                a+="2";
                //shomare shasi0-9
                String c = info;

                WAP.WAP_InputMethodContentsLength(14);
                WAP.SwitchToT9InputMethod(0);
                //bazgasht T9
                String b = com.sun.midp.io.j2me.comm.WAP.GetT9String();
                }
                repaint();
                if(keyCode==51 && a.equals("1")){
                step=2;
                a+="3";
                //shomare motor
                String c = info;
                WAP.WAP_InputMethodContentsLength(10);
                WAP.SwitchToT9InputMethod(0);
                 //bazgasht T9
                String b = com.sun.midp.io.j2me.comm.WAP.GetT9String();    
                repaint();
                } 
              }
              if(step==0){
                if(keyCode==49){
                a="1";
                info="*Moshakhasat\nkhodro*\n1:Pelak\n2:Shomare Shasi\n3:Shomare Motor";
                step=1;
                }
                if(keyCode==50){
                a="2";
                info="*Estelam Taghib*\n1:Pelak";
                step=1;
                }
                if(keyCode==51){
                a="3";
                info="*Moshakhast Malek*\n1:Pelak";
                step=1;
                }
                if(keyCode==52){
                a="4";
                info="*Estelam Khalafi*\n1:Pelak";
                step=1;
                } 
              }   
              repaint();

               }

        public void commandAction(Command c, Displayable d) {
            String m = WAP.GetT9String();
                SDA.SDS_SendMsg("20002",m);
                info = WAP.GetT9String() + "\nersal shod";

          //  throw new UnsupportedOperationException("Not supported yet.");
        }


         }
     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

     }
         public String pelakSp(String a){
             //tafkike pelak
             String m = a.substring(0, 2) + " " + a.substring(2, 3) + " " + a.substring(3, 6) + "-" + a.substring(6, 8);
            return m;
         }
}

Try

this.addCommand(...)

in your constructor

i got the problem! i didn't used command listener for my canvas! i add this and it solved:

m_canvas.setCommandListener(new CommandListener() {
  public void commandAction(Command c, Displayable d) {
        String m = WAP.GetT9String();
        SDA.SDS_SendMsg("20002",m);
        info = WAP.GetT9String() + "\nersal shod";
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM