Domanda

programma J2ME semplice

ho scritto con LWUIT package.I hanno aggiunto una Form nel mio file di classe MIDlet. Supponiamo, l'utente preme un tasto, allora voglio mostrare un altro Form.But non potrei essere in grado di catturare evento chiave nella mia LWUIT Form.

Questo è il mio codice snippt

import javax.microedition.midlet.*;
import com.sun.lwuit.*;
import com.sun.lwuit.events.*;


public class MultipleForm extends MIDlet  implements ActionListener{

    private Form mFirstForm, mSecondForm;

    public void startApp()
 {
      if (mFirstForm == null) 
     {
         Display.init(this);

        mFirstForm = new Form("First Form");
        Button button = new Button("Switch");
        button.addActionListener(this);        
        mFirstForm.addComponent(button);

        mSecondForm = new Form("Second Form");
        Button button2 = new Button("Switch");
        button2.addActionListener(this);
        mSecondForm.addComponent(button2);

        mFirstForm.show();

      }
    }

    protected void keyPressed(int key)
    {
        System.out.println("Key Pressed");

        if(key==52)
        {
          Form current = Display.getInstance().getCurrent();
          if (current == mFirstForm)
          {
             mSecondForm.show();
          }
          else if(current==mSecondForm)
          {
             mFirstForm.show();
          }
        }
    }

    public void pauseApp() {}

    public void destroyApp(boolean unconditional) {}
}
È stato utile?

Soluzione

Per catturare la chiave evento in un Form LWUIT è necessario utilizzare Form.addGameKeyListener(here the key, here actionListener)

I tasti sono mappati usando Canvas come Canvas.FIRE per esempio.

Prova a farlo.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top