命令不在canvas j2me程序中工作

时间:2015-09-12 21:00:32

标签: java java-me

我制作了一个画布J2ME程序,我使用按键和键码来完成程序!现在我有两个屏幕命令的大问题! 我需要使用命令标签" Ersal"和" Virayesh"如下面的代码,但命令代码不起作用!我可以使用密钥代码(-6),但我不会在屏幕上显示命令标签。 那么你的解决方案是什么? 我可以在屏幕上添加两个标签而不是命令吗?! 或者我怎么能激活这些命令呢!

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

2 个答案:

答案 0 :(得分:0)

尝试

this.addCommand(...)

在你的构造函数

答案 1 :(得分:0)

我遇到了问题! 我的画布没有使用命令监听器! 我添加了这个并解决了:

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