Quá trình thứ hai: Tạo một lớp MIDlet

Một phần của tài liệu LẬP TRÌNH ỨNG DỤNG MOBILE BẰNG JAVA (Trang 133 - 139)

Bây giờ ta tạo một lớp kế thừa lớp MIDlet. Để tạo mới một Class mới ta vào File>New…

Hình 31:Tạo mới một MIDlet.

Tiếp theo, chúng ta chọn MIDP MIDlet giống như hình bên dưới. Hình 32:Hiển thị cách tạo mới một MIDlet.

Bước tiếp theo, ta nhập vào tên gói (pakage) và tên lớp (class) ví dụ như hình bên dưới.

Hình 33:Nhập tên lớp kế thừa từ lớp MIDlet.

Nhấp nút Next để tiếp tục quá trình tạo MIDlet, nút Finish để hoàn tất quá trình tạo MIDlet và nút Cancel để hủy bỏ việc tạo MIDlet.

Hình 35:Kết thúc việc tạo một lớp.

Khi đó chương trình sẽ tạo ra 2 tập tin: MainFrame.java và KeyCode.java

Đây là đoạn code của tập tin MainFrame.java:

package keycode;

import javax.microedition.lcdui.*;

public class MainFrame extends Form implements CommandListener { /** Constructor */ public MainFrame() { super("Displayable Title"); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } }

/**Component initialization*/

private void jbInit() throws Exception {

// Set up this Displayable to listen to command events setCommandListener(this);

// add the Exit command

addCommand(new Command("Exit", Command.EXIT, 1)); }

/**Handle command events*/

public void commandAction(Command command, Displayable displayable) {

/** @todo Add command handling code */

if (command.getCommandType() == Command.EXIT) { // stop the MIDlet

KeyCode.quitApp(); }

} }

Và bên dưới là đoạn code của tập tin KeyCode.java:

package keycode;

import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class KeyCode extends MIDlet { private static KeyCode instance;

private MainFrame displayable = new MainFrame(); /** Constructor */

public KeyCode() { instance = this; }

/** Main method */ public void startApp() {

}

/** Handle pausing the MIDlet */ public void pauseApp() {

}

/** Handle destroying the MIDlet */

public void destroyApp(boolean unconditional) { }

/** Quit the MIDlet */

public static void quitApp() { instance.destroyApp(true); instance.notifyDestroyed(); instance = null;

} }

Một phần của tài liệu LẬP TRÌNH ỨNG DỤNG MOBILE BẰNG JAVA (Trang 133 - 139)

Tải bản đầy đủ (PDF)

(156 trang)