You are on page 1of 4

JME Sample Program

How to use Display, Form, List & Command


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

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

/**
* @author Rukman - 0942004
*/
public class Tugas1 extends MIDlet implements CommandListener{
private Display dsp;
int status=0 , index=0;
private List menu = new List("Aplikasi",List.IMPLICIT);
private List daftar = new List("Daftar Nama",List.IMPLICIT);
private List lingk = new List("Hasil",List.IMPLICIT);
private Form form1 = new Form("");
private Form form2 = new Form("Lingkaran");
private Command keluar = new Command("Keluar",Command.EXIT,0),
tambah = new Command("Tambah",Command.OK,1),
ubah = new Command("Ubah",Command.OK,2),
hapus = new Command("Hapus",Command.OK,3),
ok = new Command("Ok",Command.OK,0),
pilih = new Command("Pilih",Command.OK,1),
hitung = new Command("Hitung",Command.OK,1),
cancel = new Command("Batal",Command.CANCEL,0),
kembali = new Command("Kembali",Command.BACK,0);
private TextField nama;

public void startApp() {


dsp = Display.getDisplay(this);
menu.append("Data Operation", null);
menu.append("Hitung Lingkaran", null);
menu.addCommand(keluar);
menu.addCommand(pilih);
menu.setCommandListener(this);
dsp.setCurrent(menu);
}

public void pauseApp() {


}

public void destroyApp(boolean unconditional) {


}

public void dataOp(int i){


form1.deleteAll();
if(i==1){
form1.setTitle("Tambah Nama");
nama = new TextField("Input Nama : ", "", 20, TextField.ANY);
form1.append(nama);
status = 1;
}
else if(i==2){
form1.setTitle("Ubah Nama");
index = daftar.getSelectedIndex();
String s = ("Nama yang diubah : " + daftar.getString(index));
form1.append(s);
nama = new TextField("Input Nama : ", "", 20, TextField.ANY);
form1.append(nama);
status = 2;
}
else if(i==3){
form1.setTitle("Hapus Nama");
index = daftar.getSelectedIndex();
String s = ("Nama yang dihapus : " + daftar.getString(index));
form1.append(s);
status = 3;
}
form1.addCommand(cancel);
form1.addCommand(ok);
form1.setCommandListener(this);
dsp.setCurrent(form1);
}

public void tampilData(){


daftar.addCommand(kembali);
daftar.addCommand(tambah);
daftar.addCommand(ubah);
daftar.addCommand(hapus);
daftar.setCommandListener(this);
dsp.setCurrent(daftar);
}

public void hitungLing(){


form2.deleteAll();
nama = new TextField("Ukuran Jari-jari : (cm)", "", 20, TextField.NUMERIC);
form2.append(nama);
form2.addCommand(kembali);
form2.addCommand(hitung);
form2.setCommandListener(this);
dsp.setCurrent(form2);
}

public void tampilHitung(String s1, String s2){


lingk.deleteAll();
lingk.append("Luas Lingkaran : " + s1 + "cm",null);
lingk.append("Keliling Lingkaran : " + s2 + "cm",null);
lingk.addCommand(ok);
status = 4;
lingk.setCommandListener(this);
dsp.setCurrent(lingk);
}

public void commandAction(Command c, Displayable d) {


int j = daftar.size();
if(c==keluar){
destroyApp(true); notifyDestroyed();
}
else if(c == pilih){
int i = menu.getSelectedIndex();
if (i==0) tampilData();
else if(i==1) hitungLing();
}
else if(c==tambah)dataOp(1);
else if(c==ubah && j!=0) dataOp(2);
else if(c==hapus && j!=0) dataOp(3);
else if(c==hitung){
if(nama.size()!=0){
int i = Integer.parseInt(nama.getString());
float phi = 3.14f;
String luas = Float.toString(phi*(i*i));
String kel = Float.toString(2*phi*i);
tampilHitung(luas, kel);
}
}
else if(c==ok){
if(status==1){
daftar.append(nama.getString(), null);
tampilData();
}
else if(status==2){
if(nama.size()!=0){
daftar.delete(index);
daftar.append(nama.getString(), null);
}
tampilData();
}
else if(status==3){
daftar.delete(index);
tampilData();
}
else if(status==4){
hitungLing();
}
}
else if(c==cancel) tampilData();
else if(c==kembali){
menu.deleteAll();
startApp();
}
}
}

You might also like