Gerencia de varios forms

1 resposta
M

Olá pessoal,
Estou apanhando para gerenciar a exibição de Forms na tela. Quando mando executar não dá erro mas também nao aparece nada na tela. Será que tem relação com a forma que estou usando Display?

Tem alguma forma melhor para tratar o CommandListener? Criar uma nova classe fora do midlet???

Sou novato na área e estou tendo muita dificuldade com o J2ME.

Obrigado

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.*;
import javax.microedition.rms.RecordStore;

/**
 * @author Marcos
 */
public class Midlet extends MIDlet {
    public FmMain fmmain;
    public ProxPg proxform;
    public Armazenamento armazen;
    public Display display;
    public RecordStore basedados = null;

    public void startApp() {
//
//        tentativa de criar um banco global
//        try {
//            basedados = RecordStore.openRecordStore("Lista", true);
//        } catch (RecordStoreException ex) {
//            ex.printStackTrace();
//        }
    if (display.equals(null)) {
            fmmain= new FmMain("Principal", this);
            display = Display.getDisplay(this);
        }
        display.setCurrent(fmmain);
    }


    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}
import java.io.IOException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

/**
 * @author Marcos
 */
public class FmMain extends Form implements CommandListener {

    private Command voltar, sair, fazerAcao, proxPg;
    private Midlet midlet;
    private StringItem nomdig;
    Display display = null;
    TextField nome = null;

    public FmMain(String title, Midlet midlet) {

        super(title);


        this.midlet = midlet;
        midlet.startApp();
        display.getDisplay(midlet);
        display.setCurrent(this);
        sair = new Command("Sair", Command.EXIT, 3);
        voltar = new Command("Voltar", Command.OK, 2);

        this.addCommand(sair);
        this.addCommand(voltar);
        //fmMain = new Form("Descrição");
        append("Você esta na Universidade de Itauna");
        append(nome);
        append(nomdig);
        addCommand(sair);
        addCommand(fazerAcao);
        addCommand(proxPg);
        addCommand(voltar);
        setCommandListener(this);
    }

    public void commandAction(Command c, Displayable d) {
        if (d == midlet.fmmain) {
            if (c == sair) {
                midlet.destroyApp(true);
                midlet.notifyDestroyed();
            }
            if (c == fazerAcao) {
                nomdig.setText("\n" + nome.getString());
            }
            if (c == proxPg) {
                try {
                    midlet.proxform = new ProxPg("ProxPg", midlet);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                display.setCurrent(midlet.proxform);

            }
        }
    }
}
import java.io.IOException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;

public class ProxPg extends Form implements CommandListener {

    private Command voltar, armazenamento, sair;


    private Midlet midlet;

    public ProxPg(String title, Midlet midlet) throws IOException {

        super(title);

        this.midlet = midlet;

        sair = new Command("Sair", Command.EXIT, 3);
        armazenamento = new Command("Armazenamento", Command.OK, 1);
        voltar = new Command("Voltar", Command.OK, 2);

        this.addCommand(sair);
        this.addCommand(voltar);
        this.addCommand(armazenamento);
        append(Image.createImage("/brasil.png"));
        setCommandListener(this);
    }

    public void commandAction(Command c, Displayable s) {
        if (c == voltar) {
            this.deleteAll();
            midlet.display.setCurrent(midlet.fmmain);
        }
        if (c == armazenamento) {
            midlet.armazen = new Armazenamento("Armazenamento",midlet);

            midlet.display.setCurrent(midlet.armazen);
        }
        if (c == sair) {
            midlet.destroyApp(false);
            midlet.notifyDestroyed();
        }

}
}
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 * @author Marcos
 */
public class Armazenamento extends Form implements CommandListener {

    private Command cmExit, fazerAcao, voltar, deletar, adicionar;
    private StringItem nomdig;
    private Alert msg;
    private Midlet midlet;
    Display display = null;
    TextField nome = null;
    TextField nomeuser = null;
    TextField curso = null;
    TextField ID = null;
    private StringItem dados = null;
    ChoiceGroup opcoes = null;
    private RecordStore basedados = null;
    public String todosdados;

    public Armazenamento(String title, Midlet midlet) {
        super(title);
        this.midlet = midlet;
        cmExit = new Command("Sair", Command.EXIT, 3);
        voltar = new Command("Voltar", Command.OK, 2);

        nome = new TextField("Insira seu nome", "sem nenhum texto", 50, 1);
        nomeuser = new TextField("Nome do usuário", " ", 50, 1);
        curso = new TextField("Curso", " ", 50, 1);
        ID = new TextField("ID (referente ao ID a ser deletado)", " ", 50, 1);
        dados = new StringItem("Dados Armazenados", "");

        setCommandListener(this);
        append(nomeuser);
        append(opcoes);
        append(curso);
        append(ID);
        append(dados);
        addCommand(cmExit);
        addCommand(voltar);
        addCommand(adicionar);
        addCommand(deletar);

    }
 public void consultarbanco() throws RecordStoreNotOpenException, RecordStoreException {
        RecordEnumeration re = basedados.enumerateRecords(null, null, false);

        try {
            while (re.hasNextElement()) {
                byte[] data = re.nextRecord();
                dados.setText(data.toString());
            }
        } catch (Exception e) {
            System.out.println("Erro " + e);
        }


    }

    private void adicionarRegistro() throws RecordStoreNotOpenException, RecordStoreException {

        todosdados = nome.getString() + opcoes.getString(opcoes.getSelectedIndex()) + curso.getString();
        System.out.println(todosdados);
        byte[] registro = todosdados.getBytes();

        try {
            basedados = RecordStore.openRecordStore("Lista", true);
            basedados.addRecord(registro, 0, registro.length);
            basedados.closeRecordStore();
            msg.setString("Registro adicionado com sucesso");
            Display.getDisplay(midlet).setCurrent(msg);
        } catch (Exception e) {
            System.out.println("Erro " + e);
        }
        consultarbanco();
    }
    public void deletar() throws RecordStoreException{
        try{
            RecordStore rs = RecordStore.openRecordStore("Lista", true);
            rs.deleteRecord(Integer.parseInt(ID.getString()));
            rs.closeRecordStore();
        }catch(RecordStoreException rse){
            System.out.println("Erro " + rse);
        }
        consultarbanco();
    }
    public void commandAction(Command c, Displayable d) {
        if (c == cmExit) {
            midlet.destroyApp(true);
            midlet.notifyDestroyed();
        }
        if (c == voltar) {
            this.deleteAll();
            midlet.display.setCurrent(midlet.proxform);
        }
        if (c == adicionar) {
            try {
                adicionarRegistro();
            } catch (RecordStoreNotOpenException ex) {
                ex.printStackTrace();
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
            }
        }
        if (c== deletar){
            try {
                deletar();
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
            }
        }

    }
}

1 Resposta

M

talves ajude, essa é a saída do netbeans:
o que é isso “com.sun.kvem.midletsuite.InvalidJadException: Reason = 22”???

Warning: TP3.jad modified in the future. Copying 1 file to F:\NetBeansProjects\TP3\dist\DefaultColorPhone\nbrun1575434906381189797 Warning: TP3.jar modified in the future. Copying 1 file to F:\NetBeansProjects\TP3\dist\DefaultColorPhone\nbrun1575434906381189797 Jad URL for OTA execution: http://localhost:8082/servlet/org.netbeans.modules.mobility.project.jam.JAMServlet/F%3A/NetBeansProjects/TP3/dist/DefaultColorPhone/TP3.jad Starting emulator in execution mode [b]com.sun.kvem.midletsuite.InvalidJadException: Reason = 22[/b] The manifest or the application descriptor MUST contain the attribute: MIDlet-1 ricoh-run: semc-icon-assembly: semc-ppro-emulator: semc-do-run: semc-run: savaje-run: sjmc-run: nokiaS80-run: nsicom-run: bdj-run: run: CONSTRUÍDO COM SUCESSO (tempo total: 3 segundos)

Criado 16 de outubro de 2009
Ultima resposta 16 de out. de 2009
Respostas 1
Participantes 1