Minggu, 04 Maret 2012

Aplikasi sederhana J2ME

Langkah-langkah untuk membuat aplikasi pada data diri pada mobile :

1. masuk pada aplikasi java netbeans anda.
2. buat project baru dengan cara klik file > new project > pilih J2ME > mobile aplication, lalu tekan next.
3. Setelah itu anda masukkan script nya.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hello ;

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;

public class tussss extends MIDlet implements CommandListener {

     //inisialisasi properti
        private Display display;
        private Form fmDataPribadi;
        private Form fmHasil;
        private Command cmOk;
        private Command cmExit;
        private Command cmBack;
        private TextField tfNama;
        private TextField tfAlamat;
        private TextField tfNIM;
        private TextField tfTglLahir;
        private TextField tfTmptLahir;
        private ChoiceGroup cgProdi;
        private int choiceGroupIndex;
        private StringItem siNama, siNIM, siAlamat, siTmptLhr, siTglLhr, siProdi;

        private boolean midletPaused = false;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">
    //</editor-fold>

    /**
     * The tugas constructor.
     */
    public Midlet() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {
        // write pre-initialize user code here

        // write post-initialize user code here
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {
        // write pre-action user code here

        // write post-action user code here
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {
        // write pre-action user code here

        // write post-action user code here
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
        // write pre-switch user code here
        Display coba = getDisplay();
        if (alert == null) {
            coba.setCurrent(nextDisplayable);
        } else {
            coba.setCurrent(alert, nextDisplayable);
        }
        // write post-switch user code here
    }
    //</editor-fold>

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

     //memanggil manager aplikasi untuk memulai MIDlet
        public void startApp(){
                display = Display.getDisplay(this);

                //membuat tombol
                cmExit = new Command("Exit", Command.EXIT, 0);
                cmOk = new Command("Ok", Command.SCREEN, 0);
                cmBack = new Command("Back", Command.BACK, 0);

                //textfield untuk data pribadi
                tfNama = new TextField("Nama: ", "", 30, TextField.ANY);
                tfAlamat = new TextField ("Alamat :", "", 30, TextField.ANY);
                tfNIM = new TextField("NIM :", "", 30, TextField.ANY);
                tfTglLahir = new TextField("Tanggal Lahir: ", "", 30, TextField.ANY);
                tfTmptLahir = new TextField("Tempat Lahir: ", "", 30, TextField.ANY);

                //pemilihan Prodi
                cgProdi = new ChoiceGroup("Prodi", Choice.EXCLUSIVE);

                //meng-append pilihan atau choice
                cgProdi.append("TKK", null);
                cgProdi.append("MIF", null);
                cgProdi.append("", null);


                //membuat form dan memasukkan komponen
                fmDataPribadi = new Form("Data Pribadi");
                fmDataPribadi.addCommand(cmExit);
                fmDataPribadi.addCommand(cmOk);
                fmDataPribadi.append(tfNama);
                fmDataPribadi.append(tfAlamat);
                fmDataPribadi.append(tfNIM);               
                fmDataPribadi.append(tfTmptLahir);
                fmDataPribadi.append(tfTglLahir);
                choiceGroupIndex = fmDataPribadi.append(cgProdi);
                fmDataPribadi.setCommandListener(this);

                //membuat form hasil input user
                fmHasil = new Form("Profile Anda");

                //membuat string item untuk menampilkan text dan pilihan yang diisi
                siNama = new StringItem("Nama: ", null);
                siAlamat = new StringItem ("Alamat : ", null);
                siNIM = new StringItem ("NIM : ", null);              
                siTmptLhr = new StringItem("Tempat Lahir: ", null);
                siTglLhr = new StringItem("Tanggal Lahir: ", null);
                siProdi = new StringItem("Prodi: ", null);

                //menampilkan StringItem yang nanti akan diisi oleh data hasil input user
                fmHasil.append(siNama);
                fmHasil.append(siAlamat);
                fmHasil.append(siNIM);
                fmHasil.append(siTmptLhr);
                fmHasil.append(siTglLhr);
                fmHasil.append(siProdi);

                //menambahkan command
                fmHasil.addCommand(cmBack);
                fmHasil.setCommandListener(this);

                //menampilkan form DataPribadi sebagai tampilan awal
                display.setCurrent(fmDataPribadi);
        }
         public void pauseApp(){

        }

        public void destroyApp(boolean unconditional){

        }

        public void commandAction(Command c, Displayable d){

            //variable string untuk menampung inputan user
                String nama,nim,alamat,tmptlhr,tgllhr,prodi = null;

                //jika tombol/command OK ditekan
                if(c == cmOk){
                        //mendapatkan inputan user
                        nama = tfNama.getString();
                        alamat = tfAlamat.getString();
                        nim = tfNIM.getString();
                        tmptlhr = tfTmptLahir.getString();
                        tgllhr = tfTglLahir.getString();
                        prodi = cgProdi.getString(cgProdi.getSelectedIndex());

                        //memasukkan hasil input ke StringItem
                        siNama.setText(nama);
                        siAlamat.setText(alamat);
                        siNIM.setText(nim);
                        siTmptLhr.setText(tmptlhr);
                        siTglLhr.setText(tgllhr);
                        siProdi.setText(prodi);

                        //menampilkan form hasil setelah cmOk ditekan
                        display.setCurrent(fmHasil);

                } else if(c == cmExit){ //jika tombol/command Exit ditekan
                        destroyApp(true);
                        notifyDestroyed();

                } else if(c == cmBack){ //jika tombol/command Back ditekan
                        //menampilkan kembali form DataPribadi sebagai tampilan default/awal
                        display.setCurrent(fmDataPribadi);
                }
        }
}

1 komentar: