บทที่ 4 ภาคปฏิบัติ
บทที่ 4 ภาคปฏิบัติ
จัดเตรียมเครื่องมือ
สำหรับเครื่องมือ ที่จะเป็นเครื่องมือ หลังในหนังสือเล่มนี้คือ J2ME
Wireless Toolkits(WTK) ซึ่งในขณะที่ผมเขียนบทความอยู่นี้ ได้มีการการออกตัว
Version 2 ออกมาแล้ว ซึ่งรุ่นใหม่นี้ จะรองรับกับ MIDP 2.0 และความสามารถอื่นๆที่เพิ่มเข้ามา
คุณสามารถทำการ Download โปรแกรมนี้ได้จาก http://java.sun.com/products/j2mewtoolkit/index.html
สำหรับการติดตั้งและใช้งาน J2ME Wireless Toolkits(WTK)
คุณสามารถกลับไปอ่านได้จาก ในบทที่ 3
หากคุณต้องการ โปรแกรมอื่นๆ ที่จะช่วยในการพัฒนา คุณสามารถอ่าน เพิ่มเติมได้จาก
บทที่ 4 ครับ
เริ่มต้นสร้าง โปรแกรมใหม่
- ในการเริ่มต้นสร้าง โปรแกรมใหม่ จะมีขั้นตอน และวิธีการเช่นเดียวกับ
ในหัวข้อ การติดตั้งและใช้งาน J2ME Wireless
Toolkits(WTK) ของบทที่ 3
- กำหนด MIDlet Class Name ตามชื่อ ของไฟล์ตัวอย่าง เช่นไฟล์ตัวอย่าง
ชื่อว่า HelloComman.java เราจะกำหนด MIDlet Class Name
เป็น HelloCommand
- ทำการแก้ไข ค่าของ MicroEdition-Profile เป็น MIDP-1.0 (เพราะต้องการ
ใช้งานกับ MIDP-1.0 และทดสอบกับมือถือที่รองรับ เช่น Nokia 3650 และ 7650)
- สร้างไฟล์ตัวอย่าง บันทึกไว้ในโฟล์ C:\WTK20\apps\HelloCommand\src
- ทดสอบโปรแกรมโดยกดปุ่ม Run ของโปรแกรม J2ME Wireless Toolkits
ตัวอย่างที่ 1 Command Example
HelloCommand.java
Download Code
/* * HelloCommand.java * * Created on 13 ??????? 2546, 16:38 ?. */
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* An example MIDlet with simple "Hello" text and an Exit command.
* Refer to the startApp, pauseApp, and destroyApp
* methods so see how each handles the requested transition.
*
* @author sup98
* @version
*/
public class HelloCommand extends MIDlet implements CommandListener {
private Command exitCommand; // The exit command
private Display display; // The display for this MIDlet
public HelloCommand() {
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.SCREEN, 2);
}
/**
* Start up the Hello MIDlet by creating the TextBox and associating
* the exit command and listener.
*/
public void startApp() {
TextBox t = new TextBox("Hello MIDlet", "Test string",
256, 0);
t.addCommand(exitCommand);
t.setCommandListener(this);
display.setCurrent(t);
}
/**
* Pause is a no-op since there are no background activities or
* record stores that need to be closed.
*/
public void pauseApp() {
}
/**
* Destroy must cleanup everything not handled by the garbage collector.
* In this case there is nothing to cleanup.
*/
public void destroyApp(boolean unconditional) {
}
/*
* Respond to commands, including exit
* On the exit command, cleanup and notify that the MIDlet has been destroyed.
*/
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
}
ผลการ Run
รูปที่ 34 แสดงหน้าต่าง Emulator ที่
run MIDlet ที่เขียนขึ้น
อธิบาย: ส่วนของโค้ดจะเริ่มต้นจาก การสั่ง import ชุดคำสั่งที่จำเป็นในการทำงาน
และมีการประกาศสร้าง class ซึ่งเป็น MIDlet ของเรานั้นเอง
โดยทำการสืบทอดจาก MIDlet พร้อมทั้งมีการสั่ง implement CommandListener
เพื่อให้สามารถรับ Event (เหตุการณ์) ของ ปุ่ม (Command)
ได้
ต่อจากนั้น จะเป็นการประกาศตัวแปร สำหรับแสดงผล (display) และ ปุ่ม
(HelloCommand.jad)
เมื่อเริ่มสร้าง โปรแกรม (อยู่ใน public HelloCommand() ) จะมีการสร้าง
Object สำหรับแสดงผล (display) และ ปุ่ม (exitCommand)
เมื่อเริ่มต้น โปรแกรม (อยู่ใน public void startApp() ) จะมีการสร้าง
Object ของ TextBox สำหรับกรอกข้อความ พร้อมทั้ง นำ exitCommand
ไปไว้กับ TextBox โดยใช้คำสั่ง addCommand หลังจากนั้น ก็ใช้คำสั่ง
setCommandListener เพื่อให้ exitCommand ส่งเหตุการณ์ (Event)
การคลิก ได้
สุดท้าย ก็ทำการแสดง TextBox ออกที่หน้าจอภาพ
เมื่อมีผู้ใช้ทำการคลิกที่ปุ่ม จะมีการเรียกใช้งาน public void commandAction(Command
c, Displayable s) โดยมีการตรวจสอบ ว่าเป็นการคลิกปุ่ม exitCommand
หรือไม่ หากใช่แล้ว จะมีการเรียกคำสั่ง เพื่อจบโปรแกรม
หลักจาก คลิกที่ปุ่ม Build หรือ Run ของ โปรแกรม J2ME
Wireless Tookits แล้ว เราจะได้ไฟล์ HelloCommand.jad ซึ่งอยู่ใน
โฟล์เดอร์ ที่ชื่อ Bin ที่เราจะสามารถส่งเข้าไปยัง เครื่องมือถือ
ที่เราได้ต้องการ และทดสอบการทำงานกับเครื่อง ได้ทันที
ตัวอย่างที่ 2 MultipleCommand
MultipleCommand.java
Download Code
/* * MultipleCommand.java * * Created on 13 ??????? 2546, 16:38 ?. */ import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** * An example MIDlet with simple "Hello" text and an Exit command. * Refer to the startApp, pauseApp, and destroyApp * methods so see how each handles the requested transition. * * @author sup98 * @version */ public class MultipleCommand extends MIDlet implements CommandListener { private Command exitCommand; // The exit command private Command infoCommand, buyCommand; private Display display; // The display for this MIDlet public MultipleCommand() { display = Display.getDisplay(this); exitCommand = new Command("Exit", Command.SCREEN, 1); infoCommand = new Command("Info", Command.SCREEN, 2); buyCommand = new Command("buy", Command.SCREEN, 2); } /** * Start up the Hello MIDlet by creating the TextBox and associating * the exit command and listener. */ public void startApp() { TextBox t = new TextBox("Hello MIDlet", "Test string", 256, 0); t.addCommand(exitCommand); t.addCommand(infoCommand); t.addCommand(buyCommand); t.setCommandListener(this); display.setCurrent(t); } /** * Pause is a no-op since there are no background activities or * record stores that need to be closed. */ public void pauseApp() { } /** * Destroy must cleanup everything not handled by the garbage collector. * In this case there is nothing to cleanup. */ public void destroyApp(boolean unconditional) { } /* * Respond to commands, including exit * On the exit command, cleanup and notify that the MIDlet has been destroyed. */ public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } } }
ผลการ Run
รูปที่ 35 แสดงหน้าต่าง Emulator ที่
run MIDlet ที่เขียนขึ้น
อธิบาย: โค้ดข้างต้น
เป็นตัวอย่างในการเพิ่มปุ่ม (Command) เข้าไปยัง TextBox เพื่อให้เลือกเป็นเมนูต่างๆ
ตัวอย่างที่ 3 TextBox Demo
TextBoxDemo.java
Download Code
/* * TextBoxDemo.java * * Created on 13 ??????? 2546, 16:38 ?. */ import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** * An example MIDlet with simple "Hello" text and an Exit command. * Refer to the startApp, pauseApp, and destroyApp * methods so see how each handles the requested transition. * * @author sup98 * @version */ public class TextBoxDemo extends MIDlet implements CommandListener { private Command exitCommand, okCommand; // The exit command private Display display; // The display for this MIDlet private TextBox t; public TextBoxDemo() { display = Display.getDisplay(this); t = new TextBox("Enter Phone Number","018116855",10,TextField.PHONENUMBER);
exitCommand = new Command("Exit", Command.EXIT, 2);
okCommand = new Command("OK", Command.OK,2);
}
/**
* Start up the Hello MIDlet by creating the TextBox and associating
* the exit command and listener.
*/
public void startApp() {
t.addCommand(exitCommand);
t.addCommand(okCommand);
t.setCommandListener(this);
display.setCurrent(t);
}
/**
* Pause is a no-op since there are no background activities or
* record stores that need to be closed.
*/
public void pauseApp() {
}
/**
* Destroy must cleanup everything not handled by the garbage collector.
* In this case there is nothing to cleanup.
*/
public void destroyApp(boolean unconditional) {
}
/*
* Respond to commands, including exit
* On the exit command, cleanup and notify that the MIDlet has been destroyed.
*/
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
}
ผลการ Run
รูปที่ 36 แสดงหน้าต่าง Emulator ที่
run MIDlet ที่เขียนขึ้น
อธิบาย: โค้ดข้างต้น
เป็นตัวอย่างในการป้อนหมายเลขโทรศัพท์ เข้าไปยัง TextBox พร้อมปุ่ม OK และ
Exit
ตัวอย่างที่ 4 List Demo
ListDemo.java
Download Code
/* * ListDemo.java * * Created on 13 ??????? 2546, 16:38 ?. */
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* An example MIDlet with simple "Hello" text and an Exit command.
* Refer to the startApp, pauseApp, and destroyApp
* methods so see how each handles the requested transition.
*
* @author sup98
* @version
*/
public class ListDemo extends MIDlet implements CommandListener {
private Command exitCommand, backCommand;
private Display display; // The display for this MIDlet
private List menu, l1, l2, l3;
public ListDemo() {
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.EXIT, 1);
backCommand = new Command("Back", Command.BACK,2);
}
/**
* Start up the Hello MIDlet by creating the TextBox and associating
* the exit command and listener.
*/
public void startApp() {
menu = new List("List Types",Choice.IMPLICIT);
menu.append("Implicit",null);
menu.append("Multiple",null);
menu.append("Exclusive",null);
menu.addCommand(exitCommand);
menu.setCommandListener(this);
l1 = new List("Language",Choice.IMPLICIT);
l1.append("Java",null);
l1.append("VB",null);
l1.append("C#",null);
l1.append("C++",null);
l1.addCommand(backCommand);
l1.setCommandListener(this);
l2 = new List("Plateform",Choice.MULTIPLE);
l2.append("Linux",null);
l2.append("Solaris",null);
l2.append("NT",null);
l2.addCommand(backCommand);
l2.setCommandListener(this);
l3 = new List("Country",Choice.EXCLUSIVE);
l3.append("Thailand",null);
l3.append("Malaysia",null);
l3.append("Japan",null);
l3.append("Korea",null);
l3.addCommand(backCommand);
l3.setCommandListener(this);
display.setCurrent(menu);
}
/**
* Pause is a no-op since there are no background activities or
* record stores that need to be closed.
*/
public void pauseApp() {
}
/**
* Destroy must cleanup everything not handled by the garbage collector.
* In this case there is nothing to cleanup.
*/
public void destroyApp(boolean unconditional) {
}
/*
* Respond to commands, including exit
* On the exit command, cleanup and notify that the MIDlet has been destroyed.
*/
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}else if (c == backCommand){
display.setCurrent(menu);
}else if (c == List.SELECT_COMMAND){
if(s == menu){
switch(((List) s).getSelectedIndex()){
case 0 : display.setCurrent(l1);
break;
case 1 : display.setCurrent(l2);
break;
case 2 : display.setCurrent(l3);
break;
}
}
}
}
}
ผลการ Run
รูปที่ 37 แสดงหน้าต่าง Emulator ที่
run MIDlet ที่เขียนขึ้น
อธิบาย: โค้ดข้างต้น
เป็นตัวอย่างที่แสดงถึง คุณสมบัติของ List แต่ละประเภท
ตัวอย่างที่ 5 Alert & Ticker Demo
AlertDemo.java
Download Code
/* * AlertDemo.java * * Created on 13 ??????? 2546, 16:38 ?. */
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* An example MIDlet with simple "Hello" text and an Exit command.
* Refer to the startApp, pauseApp, and destroyApp
* methods so see how each handles the requested transition.
*
* @author sup98
* @version
*/
public class AlertDemo extends MIDlet implements CommandListener {
private Command exitCommand, okCommand;
private Display display; // The display for this MIDlet
private TextBox t;
public AlertDemo() {
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.SCREEN, 2);
okCommand = new Command("OK", Command.SCREEN,2);
}
/**
* Start up the Hello MIDlet by creating the TextBox and associating
* the exit command and listener.
*/
public void startApp() {
t = new TextBox("","8116585", 10, TextField.PHONENUMBER);
Ticker t1 = new Ticker("Enter Your Office Phone Number");
t.addCommand(exitCommand);
t.addCommand(okCommand);
t.setTicker(t1);
t.setCommandListener(this);
display.setCurrent(t);
}
/**
* Pause is a no-op since there are no background activities or
* record stores that need to be closed.
*/
public void pauseApp() {
}
/**
* Destroy must cleanup everything not handled by the garbage collector.
* In this case there is nothing to cleanup.
*/
public void destroyApp(boolean unconditional) {
}
/*
* Respond to commands, including exit
* On the exit command, cleanup and notify that the MIDlet has been destroyed.
*/
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
} if (c == okCommand){
Alert alert = new Alert("Infomation");
alert.setString("Your phone number is" +t.getString());
alert.setTimeout(2000);
display.setCurrent(alert);
}
}
}
ผลการ Run
รูปที่ 38 แสดงหน้าต่าง Emulator ที่
run MIDlet ที่เขียนขึ้น
อธิบาย: โค้ดข้างต้น
เป็นตัวอย่างที่แสดงถึง การทำงานของ Alert และ Ticker
ตัวอย่างที่ 6 Form Demo
FormDemo.java
Download Code
/* * FormDemo.java * * Created on 13 ??????? 2546, 16:38 ?. */
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* An example MIDlet with simple "Hello" text and an Exit command.
* Refer to the startApp, pauseApp, and destroyApp
* methods so see how each handles the requested transition.
*
* @author sup98
* @version
*/
public class FormDemo extends MIDlet implements CommandListener, ItemStateListener {
private Command exitCommand, okCommand, backCommand;
private Display display; // The display for this MIDlet
private List menu;
private TextField tf1, tf2;
private Form fr1, fr2, fr3;
public FormDemo() {
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.SCREEN, 1);
backCommand = new Command("Back", Command.SCREEN, 2);
okCommand = new Command("OK", Command.SCREEN,2);
}
/**
* Start up the Hello MIDlet by creating the TextBox and associating
* the exit command and listener.
*/
public void startApp() {
menu = new List("Selection", List.IMPLICIT);
menu.append("TextField",null);
menu.append("Date",null);
menu.append("Gauge",null);
menu.addCommand(exitCommand);
menu.setCommandListener(this);
fr1 = new Form("Enter name");
fr1.append("First Name");
tf1 = new TextField("", "", 15, TextField.ANY);
fr1.append(tf1);
fr1.append("Last Name");
tf2 = new TextField("", "", 15, TextField.ANY);
fr1.append(tf2);
fr1.addCommand(backCommand);
fr1.setCommandListener(this);
fr2 = new Form("Change Date");
java.util.Date now = new java.util.Date();
DateField dateItem = new DateField("Today's date:", DateField.DATE);
dateItem.setDate(now);
fr2.append(dateItem);
fr2.addCommand(backCommand);
fr2.addCommand(okCommand);
fr2.setItemStateListener(this);
fr3 = new Form("Gauge Demo");
Gauge g = new Gauge("Level", true, 10, 0);
fr3.append(g);
fr3.addCommand(backCommand);
fr3.setCommandListener(this);
display.setCurrent(menu);
}
/**
* Pause is a no-op since there are no background activities or
* record stores that need to be closed.
*/
public void pauseApp() {
}
/**
* Destroy must cleanup everything not handled by the garbage collector.
* In this case there is nothing to cleanup.
*/
public void destroyApp(boolean unconditional) {
}
/*
* Respond to commands, including exit
* On the exit command, cleanup and notify that the MIDlet has been destroyed.
*/
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
} else if (c == backCommand){
display.setCurrent(menu);
} else if ((c == List.SELECT_COMMAND)&&(s == menu)){
switch (((List)menu).getSelectedIndex()){
case 0: display.setCurrent(fr1);
break;
case 1: display.setCurrent(fr2);
break;
case 2: display.setCurrent(fr3);
break;
}
}
}
public void itemStateChanged(Item item) {
java.util.Calendar cal = java.util.Calendar.getInstance(java.util.TimeZone.getDefault());
cal.setTime(((DateField) item).getDate());
}
}
ผลการ Run

รูปที่ 39-41 แสดงหน้าต่าง Emulator
ที่ run MIDlet ที่เขียนขึ้น
อธิบาย: โค้ดข้างต้น
เป็นตัวอย่างที่แสดงถึง การทำงานของ Form ร่วมกับ Control อื่นๆ
|