RSS

Laporan Praktikum 2 Membuat aplikasi J2ME dengan banyak kelas

Download versi  offline-nya disini
Download projectnya disini
Langkah-langkahnya:
  • Buka NetBeans
  • Buat project dengan nama “Prak2”, lalu buat package dengan nama “pkgprak2”. Jangan lupa buat juga MIDletnya. Namai Midlet tersebut yaitu “Prak2Midlet.java”.
  • Buat konstruktor berikut untuk membuat FromInput, FormShow1, FormShow 2
private FormInput frmInput;private FormShow1 frmShow1;
private FormShow2 frmShow2;
private Command cmShow1, cmShow2, cmBack, cmExit;
  • Klik tanda seru untuk membuat class FormInput, FormShow1, dan FormShow2 secara otomatis.
  • Tambahkan script “implements CommandListener “ dibelakang public class promvisMidlet extends MIDlet. Klik tanda seru dan pilih “implements All...” untuk mengimplementasikan ke semua method secara otomatis.
  • Masih pada class Prak2Midlet.java. Tambahkan script dibawah ini.
private Command cmShow1, cmShow2, cmBack, cmExit;
    public void startApp() {
        if (frmInput == null)
            frmInput = new FormInput();
        if (frmShow1 == null)
            frmShow1 = new FormShow1();
        if (frmShow2 == null)
            frmShow2 = new FormShow2();
       
        cmShow1 = new Command("Show 1", Command.OK, 0 );
        cmShow2 = new Command("Show 2", Command.OK, 1 );
        cmBack = new Command("Kembali", Command.BACK, 0 );
        cmExit = new Command("Keluar", Command.EXIT, 0 );
       
        frmInput.addCommand(cmShow1);
        frmInput.addCommand(cmShow2);
        frmInput.addCommand(cmExit);
       
        frmShow1.addCommand(cmBack);
        frmShow2.addCommand(cmBack);
       
        frmInput.setCommandListener(this);
        frmShow1.setCommandListener(this);
        frmShow2.setCommandListener(this);
       
        Display.getDisplay(this).setCurrent(frmInput);
    }
   
    public void pauseApp() {
    }
   
    public void destroyApp(boolean unconditional) {
    }
    public void commandAction(Command c, Displayable d) {
       
        if (d == frmInput) {
            if (c == cmExit) {
                destroyApp(true);
                notifyDestroyed();
            }
           
            if (c == cmShow1) {
                frmShow1.setNama(frmInput.getNama());
                frmShow1.setEmail(frmInput.getEmail());
                Display.getDisplay(this).setCurrent(frmShow1);
            }
           
            if (c == cmShow2) {
                frmShow2.setTextBox(frmInput.getNama() + " " + frmInput.getEmail());
                Display.getDisplay(this).setCurrent(frmShow2);
            }}
       
        if (d == frmShow1) {
            if ( c == cmBack) {
                Display.getDisplay(this).setCurrent(frmInput);
          }}
       
        if (d == frmShow2) {
            if ( c == cmBack) {
                Display.getDisplay(this).setCurrent(frmInput);
            }}}}
  • Tambahkan script di bawah ini di bawah “public void commandAction(Command c, Displayable d) {“ dan hapus script “throw new UnsupportedOperationException("Not supported yet.");”
if (d == frmInput) {
            if (c == cmExit) {
                destroyApp(true);
                notifyDestroyed();
            }
           
            if (c == cmShow1) {
                frmShow1.setNama(frmInput.getNama());
                frmShow1.setEmail(frmInput.getEmail());
                Display.getDisplay(this).setCurrent(frmShow1);
            }
           
            if (c == cmShow2) {
                frmShow2.setTextBox(frmInput.getNama() + " " + frmInput.getEmail());
                Display.getDisplay(this).setCurrent(frmShow2);
            }
        }
       
        if (d == frmShow1) {
            if ( c == cmBack) {
                Display.getDisplay(this).setCurrent(frmInput);
            }
        }
       
        if (d == frmShow2) {
            if ( c == cmBack) {
                Display.getDisplay(this).setCurrent(frmInput);
            } 
        }     
    }
}
  • Copy kan skrip dibawah ini pada FormInput.java
public class FormInput extends Form {
   
    private TextField tfNama, tfEmail;
   
    public FormInput() {
        super("Input Data");
        tfNama = new TextField("Nama Anda", null, 15, TextField.ANY);
        tfEmail = new TextField("Email", null, 15, TextField.EMAILADDR);
        append(tfNama);
        append(tfEmail);
    }
   
    public String getNama() {
        return tfNama.getString();
    }
   
    public String getEmail() {
        return tfEmail.getString();
    }   
}
  • Copy skrip di bawah ini pada ForShow1.java.
public class FormShow1 extends Form {
   
    private StringItem stNama, stEmail;
   
    public FormShow1() {
        super("Tampil 1");
       
        stNama = new StringItem("Nama:", null);
        stEmail = new StringItem("Email", null);
       
        append(stNama);
        append(stEmail);
    }
   
    public void setNama(String nama) {
        stNama.setText(nama);
    }
   
    public void setEmail(String email) {
        stEmail.setText(email);
    }
   
}
  • Salin skrip di bawah ini pada FormShow2.java.
public class FormShow2 extends TextBox {
   
    public FormShow2() {
        super("Tampil 2", null, 30, 0);
    }
   
    public void setTextBox(String isi) {
        this.setString(isi);
    }
   
}
  • Jalankan aplikasi tersebut. Maka akan tampil pada emulator sebagai berikut.
Form Input

Command pada Menu

Show 1

Show 2

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

2 komentar:

Unknown mengatakan...

ada contoh program login dengan dtabase mysql mas....?

ardhianugroho mengatakan...

maaf belum tersedia mas asep

Posting Komentar