Ini merupakan salah satu contoh program menggunakan Java Mobile, yang saya pelajari dari Mata Kuliah Pemrograman Dasar dan berikut langkah-langkahnya :
- Pilih New Project – Mobile – Mobile Application.
- hapus centang pada set as main project dengan create xxx.
- klik next dan centang pada CLDC-1.0 dan MIDP-1.0
- finish.
- klik kanan pada default package – visual midlet.
- pada jendela screens, klik Form lalu tarik kedalam Frame.
- pada jendela Commands, tarik ok Commands dan exit Commands kedalam form.
Gambar :
Berikut adalah source codenya :
package hello; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** * @author babycorn */ public class VisualMIDlet extends MIDlet implements CommandListener { private Display display; private Form frmDP,fmHas; private Command Ok, Exit, Back; private TextField tfNama,tfNIM,tfTglLahir,tfAlamat; private ChoiceGroup cgProdi; private ChoiceGroup choicePopup; private ChoiceGroup choiceMul; private int choiceGroupIndex,popGroupIndex; private StringItem siNama, siNIM, siAlamat, siTglLhr, siProdi,siPop; private boolean midletPaused = false; Ticker tfTicker; Image img; //// public VisualMIDlet() { } //// ///** * 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 public Display getDisplay () { return Display.getDisplay(this); } public void startApp() { display = Display.getDisplay(this); try { img=Image.createImage("/hello/poltek.png"); } catch (Exception e){} //membuat tombol Ok = new Command("Ok", Command.SCREEN, 0); Exit = new Command("Exit", Command.EXIT, 0); Back = new Command("Back", Command.BACK, 0); //textfield untuk data pribadi tfTicker = new Ticker("Welcome To My Application"); tfNama = new TextField("Nama: ", "", 30, TextField.ANY); tfNIM = new TextField("NIM :", "", 30, TextField.ANY); tfAlamat = new TextField ("Alamat :", "", 30, TextField.ANY); tfTglLahir = new TextField("Tanggal Lahir: ", "", 30, TextField.ANY); //pemilihan Prodi cgProdi = new ChoiceGroup("Kelamin", Choice.EXCLUSIVE); cgProdi.append("Laki-laki", null); cgProdi.append("Perempuan", null); choicePopup = new ChoiceGroup("Prodi", Choice.POPUP); choicePopup.append("Manajemen Informatika", null); choicePopup.append("Teknik Komputer", null); choicePopup.append("Rekam Medik", null); choicePopup.append("Gizi Klinik", null); choicePopup.append("Manajemen Agribisnis", null); choicePopup.append("Manajemen Industri", null); choicePopup.append("Bahasa Inggris", null); //membuat form dan memasukkan komponen frmDP = new Form("Data Pribadi"); frmDP.setTicker(tfTicker); frmDP.append(img); frmDP.addCommand(Exit); frmDP.addCommand(Ok); frmDP.append(tfNama); frmDP.append(tfNIM); frmDP.append(tfAlamat); frmDP.append(tfTglLahir); choiceGroupIndex = frmDP.append(cgProdi); popGroupIndex = frmDP.append(choicePopup); frmDP.setCommandListener(this); //membuat form hasil input user fmHas = new Form("Profile Anda"); siNama = new StringItem("Nama : ", null); siNIM = new StringItem ("NIM : ", null); siAlamat = new StringItem ("Alamat : ", null); siTglLhr = new StringItem("Tanggal Lahir : ", null); siProdi = new StringItem("Kelamin : ", null); siPop = new StringItem("Prodi : ", null); //menampilkan StringItem yang nanti akan diisi oleh data hasil input user fmHas.append(img); fmHas.append(siNama); fmHas.append(siNIM); fmHas.append(siAlamat); fmHas.append(siTglLhr); fmHas.append(siProdi); fmHas.append(siPop); //menambahkan command fmHas.addCommand(Back); fmHas.setCommandListener(this); //menampilkan form DataPribadi sebagai tampilan awal display.setCurrent(frmDP); } 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,tgllhr,kelamin,prodi = null; //jika tombol/command OK ditekan if(c == Ok){ //mendapatkan inputan user nama = tfNama.getString(); nim = tfNIM.getString(); alamat = tfAlamat.getString(); tgllhr = tfTglLahir.getString(); kelamin = cgProdi.getString(cgProdi.getSelectedIndex()); prodi = choicePopup.getString(choicePopup.getSelectedIndex()); //memasukkan hasil input ke StringItem siNama.setText(nama); siNIM.setText(nim); siAlamat.setText(alamat); siTglLhr.setText(tgllhr); siProdi.setText(kelamin); siPop.setText(prodi); //menampilkan form hasil setelah cmOk ditekan display.setCurrent(fmHas); } else if(c == Exit){ //jika tombol/command Exit ditekan destroyApp(true); notifyDestroyed(); } else if(c == Back){ //jika tombol/command Back ditekan //menampilkan kembali form DataPribadi sebagai tampilan default/awal display.setCurrent(frmDP); } } }display
instance is taken fromgetDisplay
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; ifnull
, thennextDisplayable
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 } //
Ya semoga bermanfaat :)
i like you
ReplyDeletei love you forever... :*
ReplyDelete