Advertisers

Kirimkan Donasi Anda
$

JSS Tripler Dahsyat

Senin, 19 Maret 2012

Membuat Program Sederhana J2ME dengan Java

1. Buka aplikasi netbeans IDE 7.1
2. Buat project baru
3. Lalu pilih JavaME pilih project Mobile application
4. Tekan next.



Lalu masukkan source code berikut ini...

/*
* 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.*;
/**
* @author FAIRUS ANWAR
*/
public class Midlet 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 tfNIM;
private TextField tfAlamat;
private ChoiceGroup cgProdi;
private int choiceGroupIndex;
private StringItem siNama, siNIM, siAlamat, siProdi;
private boolean midletPaused = false;
//
//
/**
* The tugas constructor.
*/
public Midlet() {
}
//
//
//
/**
* Initilizes the application.
* It is called only once when the MIDlet is started. The method is called before the startMIDlet method.
*/
private void initialize() {
// write pre-initialize user code here

// write post-initialize user code here
}
//
//
/**
* 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
}
//
//
/**
* 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
}
//
//
/**
* Switches a current displayable in a display. The display instance is taken from getDisplay 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 null, then nextDisplayable is set immediately
* @param nextDisplayable the Displayable to be set
*/
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
// write pre-switch user code here
Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
// write post-switch user code here
}
//
/**
* 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);
tfNIM = new TextField("NIM :", "", 30, TextField.ANY);
tfAlamat = new TextField ("Alamat :", "", 30, TextField.ANY);
//pemilihan Prodi
cgProdi = new ChoiceGroup("Prodi", Choice.EXCLUSIVE);
//meng-append pilihan atau choice
cgProdi.append("Teknik Komputer", null);
cgProdi.append("Manajement Informatika", null);
//membuat form dan memasukkan komponen
fmDataPribadi = new Form("Data Pribadi");
fmDataPribadi.addCommand(cmExit);
fmDataPribadi.addCommand(cmOk);
fmDataPribadi.append(tfNama);
fmDataPribadi.append(tfNIM);
fmDataPribadi.append(tfAlamat);
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);
siNIM = new StringItem ("NIM : ", null);
siAlamat = new StringItem ("Alamat : ", null);
siProdi = new StringItem("Prodi: ", null);
//menampilkan StringItem yang nanti akan diisi oleh data hasil input user
fmHasil.append(siNama);
fmHasil.append(siNIM);
fmHasil.append(siAlamat);
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();
nim = tfNIM.getString();
alamat = tfAlamat.getString();
prodi = cgProdi.getString(cgProdi.getSelectedIndex());
//memasukkan hasil input ke StringItem
siNama.setText(nama);
siNIM.setText(nim);
siAlamat.setText(alamat);
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);
}
}
}

Maka akan tampil output seperti di bawah ini...

0 komentar:

Posting Komentar

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More