Chủ đề: [J2ME] Viết ứng dụng Máy tính bỏ túi
![]() | ![]() ![]() ![]() 19:41 / 04.08.2015 |
Lâu nay ta học các đối tượng trên Form một cách rời rạc,có thì cũng chỉ liên kết vài cái rất vô bổ.Đây là mã nguồn ứng dụng MTBT rất hay nhé:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class NTHpro extends MIDlet implements CommandListener{ TextField tf1, tf2, tf3;
Form F;
Command exit, xoa, cong, chia; Alert alr;
Display dp;
public void startApp() { dp.setCurrent(F); }
public void pauseApp() { } public void destroyApp(boolean unconditional) { notifyDestroyed(); }
public NTHpro() {
F = new Form("CALCULATOR"); tf1 = new TextField("a:", "0", 500,5); tf2 = new TextField("b:", "0", 500,5);
tf3 = new TextField("Kết quả","0", 500, 5);
exit = new Command("exit", 7, 0); xoa = new Command("xóa", 4, 0);
cong = new Command("Cộng", 1,0);
chia = new Command("Chia", 1, 3);
F.append(tf1); F.append(tf2);
F.append(tf3); F.addCommand(exit); F.addCommand(xoa); F.addCommand(cong); F.addCommand(chia); F.setCommandListener(this); dp = Display.getDisplay(this); alr = new Alert("ERROR","Học lớp mấy mà lại đi chia cho 0 thê?", null, AlertType.ERROR); }
public void commandAction(Command Cm, Displayable Da) {
String str_kq_cong, str_kq_chia; double a, b, cg, ch;
a = Double.parseDouble(tf1.getString()); b = Double.parseDouble(tf2.getString()); cg = a b;
ch = a / b; str_kq_cong = String.valueOf(newDouble(cg)); str_kq_chia = String.valueOf(new Double(ch)); if (Cm == chia && b == 0.0) { dp.setCurrent(alr); } if (Cm == xoa) { tf1.setString(null); tf2.setString(null); } if (Cm == cong) { tf3.setString(str_kq_cong); } if (Cm == chia && b != 0.0) { tf3.setString(str_kq_chia); } if (Cm == exit) { destroyApp(true); } }}
Phép so sánh a khác b thì viết là a != b
Lệnh cài string cho txtfield(setString) và lấy string từ txtfield(getString) thì bài TextField đã nói rồi. Nói chung các bạn cần học kĩ từ đầu theo danh mục từ trên xuống mà mình đã sắp xếp.Có thể viết khai báo cho class theo nhiều cách mà tác dụng không thay đổi.
Riêng mình vì sao code hơi khác như bài trước? Vì mình mất code gốc nên đây là code dịch ngược nhé (decom).
Hết.
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class NTHpro extends MIDlet implements CommandListener{ TextField tf1, tf2, tf3;
Form F;
Command exit, xoa, cong, chia; Alert alr;
Display dp;
public void startApp() { dp.setCurrent(F); }
public void pauseApp() { } public void destroyApp(boolean unconditional) { notifyDestroyed(); }
public NTHpro() {
F = new Form("CALCULATOR"); tf1 = new TextField("a:", "0", 500,5); tf2 = new TextField("b:", "0", 500,5);
tf3 = new TextField("Kết quả","0", 500, 5);
exit = new Command("exit", 7, 0); xoa = new Command("xóa", 4, 0);
cong = new Command("Cộng", 1,0);
chia = new Command("Chia", 1, 3);
F.append(tf1); F.append(tf2);
F.append(tf3); F.addCommand(exit); F.addCommand(xoa); F.addCommand(cong); F.addCommand(chia); F.setCommandListener(this); dp = Display.getDisplay(this); alr = new Alert("ERROR","Học lớp mấy mà lại đi chia cho 0 thê?", null, AlertType.ERROR); }
public void commandAction(Command Cm, Displayable Da) {
String str_kq_cong, str_kq_chia; double a, b, cg, ch;
a = Double.parseDouble(tf1.getString()); b = Double.parseDouble(tf2.getString()); cg = a b;
ch = a / b; str_kq_cong = String.valueOf(newDouble(cg)); str_kq_chia = String.valueOf(new Double(ch)); if (Cm == chia && b == 0.0) { dp.setCurrent(alr); } if (Cm == xoa) { tf1.setString(null); tf2.setString(null); } if (Cm == cong) { tf3.setString(str_kq_cong); } if (Cm == chia && b != 0.0) { tf3.setString(str_kq_chia); } if (Cm == exit) { destroyApp(true); } }}
Phép so sánh a khác b thì viết là a != b
Lệnh cài string cho txtfield(setString) và lấy string từ txtfield(getString) thì bài TextField đã nói rồi. Nói chung các bạn cần học kĩ từ đầu theo danh mục từ trên xuống mà mình đã sắp xếp.Có thể viết khai báo cho class theo nhiều cách mà tác dụng không thay đổi.
Riêng mình vì sao code hơi khác như bài trước? Vì mình mất code gốc nên đây là code dịch ngược nhé (decom).
Hết.
Thu nhập 1 tỷ/tháng