Trang Chủ
Upload
Bạn đang xem file : Vinh.txt
Ngày upload:
25.06.2016 15:40:40
Mới tải về:
10.02.2022 15:57:53
Upload bởi:
VinhVIP
<font color="green"><p align="center">6. [LT] Form và 1 số đối tượng trên Form</p></font> <div style="border-style:ridge;border-width:1px;border-color:#FF3366;margin:1px;padding:1px" align="left"><font color="#FFFFCC">Bài này sẽ hướng dẫn các bạn tìm hiểu cách sử dụng một số đối tượng trên Form. Nó không thể hiển thị độc lập nên phải add nó lên 1 đối tượng nào đó cấp cao hơn thì mới hiển thị được. Bắt đầu thôi: <br/> <font color="blue">1. StringItem: In văn bản lên Form</font><br/> -Khái niệm: StringItem: là chuỗi mà ta gán lên form và cho hiển thị ra ngoài màn hình.<br/> -Cấu trúc:<br/> <font color="red">StringItem si=new StringItem("tên","nội dung cần hiển thị");</font></br/> -Các thao tác với StringItem:<br/> :icon-126:si.getText(); lấy nội dung của nó.<br/> :icon-126:si.setText("nội dung"); đặt nội dung cho nó.<br/> :icon-126:si.getLabel(); lấy tiêu đề.<br/> :icon-126:si.setLabel("tiêu đề mới");đặt tiêu đề cho nó.<br/> -StringItem khá đơn giản, bạn chỉ cần thực hành vài lần là hiểu.<br/> <font color="green"><p align="center">stringitem.java</p></font><br/> <div style="border-style:ridge;border-width:1px;border-color:#FF3366;margin:1px;padding:1px" align="left"><table width="100%"><tr><td><font color="#FFCC99">import javax.microedition.midlet.*;<br/> import javax.microedition.lcdui.*;<br/> public class stringitem extends MIDlet{<br/> Form f=new Form("tên form");<br/> StringItem si=new StringItem("chuỗi","Văn bản");<font color="green">//Khai báo StringItem</font><br/> Display d=Display.getDisplay(this);<br/> public void startApp(){<br/> f.append("\nDòng này viết ra mà không cần StringItem");<br/> f.append(si);<font color="green">//add StringItem lên Form f</font><br/> d.setCurrent(f);<br/> }<br/> public void pauseApp(){}<br/> public void destroyApp(boolean uncon){}<br/> }<br/> </font></td></tr></table></div> -Projects: <a href="http://u.wtai.cf/down.php?id=82">stringitem.zip</a><br/><hr color="green"> <font color="blue">2. TextField: Khung nhập văn bản</font><br/> -Khái niệm: TextField: Hiểu đơn giản nó là 1 khung nhập văn bản, tương tự như khung đăng nhập vậy.<br/> -Cấu trúc: <br/> <font color="red">TextField tf=new TextField("tên","nội dung",số kí tự tối đa, kiểu);</font><br/> -Một số thao tác với TextField:<br/> :icon-126:tf.getString(); lấy nội dung của TextField<br/> :icon-126:tf.setString("nội dung mới"); đặt nội dung TextField<br/> -Các kiểu TextField:<br/> 0 : Nhập gì cũng được.<br/> 1 : Chỉ nhập dạng email.<br/> 2 : Chỉ nhập được số.<br/> 3 : Nhập SĐT.<br/> 4 : Nhập URL.<br/> 5 : Chỉ nhập số thập phân.<br/> 65536: Nhập vào dạng password.<br/> 131072 : Nhập vào nhưng không sửa được.<br/> -TextField cũng khá đơn giản, bạn nên thực hành vài lần là hiểu.<br/> <font color="green"><p align="center">textfield.java</p></font><br/> <div style="border-style:ridge;border-width:1px;border-color:#FF3366;margin:1px;padding:1px" align="left"><table width="100%"><tr><td><font color="#FFCC99">import javax.microedition.midlet.*;<br/> import javax.microedition.lcdui.*;<br/> public class textfield extends MIDlet{<br/> Form f = new Form("tên form");<br/> TextField tf = new TextField("Nhập văn bản:", null, 500, 0);<font color="green">//null là không có gì(trống), số kí tự tối đa là 500 và nhập gì cũng được </font><br/> Display d = Display.getDisplay(this);<br/> public void startApp(){<br/> f.append(tf);<font color="green">//gán TextField lên Form</font><br/> d.setCurrent(f);<font color="green">//Hiển thị Form f</font><br/> }<br/> public void pauseApp(){}<br/> public void destroyApp(boolean uncon){}<br/> }<br/> </font></td></tr></table></div> -Projects: <a href="http://u.wtai.cf/down.php?id=83">textfield.zip</a><br/><hr color="red"> <font color="blue">3. Ticker - Dòng chữ chạy ngang</font><br/> -Khái niệm: Ticker : là 1 dòng chữ chạy ngang từ phải qua trái. Lưu ý là dù đặt nó ở vị trí nào trên form thì nó vẫn chạy ở phía trên màn hình(trên các items khác).<br/> -Cấu trúc: <font color="red">Ticker tk = new Ticker("nội dung");</font><br/> -Khác với StringItem và TextField sử dụng lệnh append() để gắn lên form, Ticker sử dụng lệnh setTicker() để gắn lên form. Nguyên do thì hỏi nhà sản xuất nhá.<br/> -Một số thao tác với Ticker:<br/> :icon-126:tk.getString(); lấy nội dung của Ticker tk. :icon-126:tk.setString("nội dung mới"); đặt nội dung mới cho Ticker tk.<br/> -Ticker rất đơn giản, chỉ cần làm 1 lần là hiểu rồi.<br/> <font color="green"><p align="center">ticker.java</p></font><br/> <div style="border-style:ridge;border-width:1px;border-color:#FF3366;margin:1px;padding:1px" align="left"><table width="100%"><tr><td><font color="#FFCC99"> import javax.microedition.midlet.*;<br/> import javax.microedition.lcdui.*;<br/> public class ticker extends MIDlet {<br/> Form f = new Form("tên form");<br/> Ticker tk = new Ticker("Nội dung");<font color="green">Khai báo Ticer</font><br/> Display d = Display.getDisplay(this);<br/> public void startApp() {<br/> f.setTicker(tk);<font color="green">gắn Ticker lên Form</color><br/> f.append("Dù bạn gắn Ticker lên vị trí nào trên Form thì nó vẫn luôn chạy ở phía trên màn hình");<br/> d.setCurrent(f);<br/> }<br/> public void pauseApp() { }<br/> public void destroyApp(boolean unconditional) { }<br/> }<br/> </font></td></tr></table></div> -Projects: <a href=http://u.wtai.cf/down.php?id=84"">ticker.zip</a><br/><hr color="red"> <font color="blue">4. Command - nút bấm</font><br/> -Khái niệm: Command : được hiểu đơn giản là khi nhấn phím thì sẽ có hành động xảy ra do ta cài đặt từ trước. Command chỉ giới hạn trong 1 số phím nhất định chứ không được như ketPressed(), keyReleased(),...thao tác được trên toàn bộ bàn phím. Vấn đề này đến những bài sau chúng ta sẽ tìm hiểu. Bây giờ chú tâm vào Command cái đã:<br/> -Cấu trúc: <font color="red">Command ten=new Command("tên nút", kiểu, vị trí);</font><br/> -Các kiểu Command: Số trong ngoặc đó là dạng viết tắt.<br/> :icon-126: Command.SCREEN (1)<br/> :icon-126: Command.BACK (2)<br/> :icon-126: Command.CANCEL (3)<br/> :icon-126: Command.OK (4)<br/> :icon-126: Command.HELP (5)<br/> :icon-126: Command.STOP (6)<br/> :icon-126: Command.EXIT (7)<br/> :icon-126: Command.ITEM (8)<br/> -Các lệnh:<br/> :icon-126: f.addCommand(tên Command); <font color="green">//Thêm Command vào Form</font><br/> :icon-126: f.removeCommand(tên Command); <font color="green">//Xóa Command khỏi form</font><br/> -Command hơi phức tạp, bạn nên luyện tập nhiều lần cho đến khi thành thạo. Để hiểu rõ hơn, tôi sẽ hướng dẫn viết 1 đoạn code ngắn sử dụng: 2 Form và 3 Command. Ở Form 1 ta gắn Command thoát app và Command next form(từ form 1 chuyển sang form 2) và 1 Command back form(từ form 2 về form 1). Code như sau:<br/> <font color="green"><p align="center">command.java</p></font> <div style="border-style:ridge;border-width:1px;border-color:#FF3366;margin:1px;padding:1px" align="left"><table width="100%"><tr><td><font color="#FFCC99"> import javax.microedition.midlet.*;<br/> import javax.microedition.lcdui.*;<br/> public class command extends MIDlet implements CommandListener {<br/><font color="green">Muốn sử dụng được Command thì khi khai báo class bắt buộc phải <font color="red">implements CommandListener</font></font><br/> Form f1 = new Form("Form 1");<br/> Form f2 = new Form("Form 2");<br/> Command ok = new Command("Next", 4, 0);<font color="green">//Command ở phím giữa (phím OK)</font><br/> Command back = new Command("Back", 2, 0);<font color="green">//Command ở phím mền phải</font><br/> Command exit = new Command("Thoát", 7, 1);<font color="green">//Command này cũng ở phím mền phải</font><br/> Display d = Display.getDisplay(this);<br/> public void startApp() {<br/> f1.append("Đây là form 1, nhấn phím OK để qua form 2, nhấn phím mền phải để thoát.");<br/> f1.addCommand(ok);<br/> f1.addCommand(exit);<br/><font color="green">//Gắn Command ok và exit lên Form f1</font><br/> f1.setCommandListener(this);<font color="green">//lệnh cho phép Command hoạt động ở Form f1, không có lệnh này thì Command ở f1 sẽ không sử dụng được.</font><br/> f2.append("Đây là form 2, nhấn phím mền phải để về form 1");<br/> f2.addCommand(back);<br/> f2.setCommandListener(this); <font color="green">//Gắn Command back lên Form f2 và cho phép nó hoạt động</font><br/> d.setCurrent(f1);<font color="green">//Hiển thị Form f1 lên mhình</font><br/> }<br/> public void pauseApp() { }<br/> public void destroyApp(boolean unconditional) {<br/> notifyDestroyed();<font color="green">//Thoát và giải phóng bộ nhớ</font><br/> }<br/> public void commandAction(Command cm, Displayable da) {<br/><font color="green">//Đây là hàm điều khiển Command</font><br/> if (cm == ok) d.setCurrent(f2);<font color="green">//Nếu nhấn Command ok thì hiển thị Form f2</font><br/> if (cm == exit) destroyApp(true);<font color="green">//Nếu nhấn Command exit thì thoát app</font><br/> if (cm == back) d.setCurrent(f1);<font color="green">//Nếu nhấn Command back thì hiển thị Form f1</font><br/> }<br/> }<br/> </font></td></tr></table></div> -Giải thích về Displayable. Đây là tham số trong hàm commandAction(), có thể hiểu Displayable là màn hình hiện tại. Vì trong ví dụ trên mỗi Command chỉ gắn duy nhất trên 1 Form nên ta không cần quan tâm cái này. Nhưng nếu add lên nhiều Form thì phải sử dụng để tránh bị lỗi khi test app. Ví dụ:<br/> if(cm==ok && da==f1)d.setCurrent(f2); //nếu nhấn Command ok và màn hình hiện tại ở Form f1 thì hiển thị Form f2.<br/> Tương tự:<br/> if(cm==back && da==f1)d.setCurrent(f2);<br/> -Projects: <a href="http://u.wtai.cf/down.php?id=85">command.zip</a><hr color="red"><br/> <font color="blue">5. Image - Lớp xử lí hình ảnh</font><br/> -Khái niệm: Image dùng để hiển thị hình ảnh, nó có thể dùng cho Form hoặc Canvas.<br/> -Cấu trúc khai báo:<br/> <font color="red">Image im;</font><br/> sau đó mới dùng khối lệnh try...catch..:<br/> <font color="red">try{<br/> im=Image.createImage("/icon.png");<br/> }<br/> catch(Exception e){}</font><br/> -Lưu ý: /icon.png ở đây là đường dẫn tới ảnh. Ảnh phải được nén vào cùng với file class mà thư mục META-INF. Nếu trong file game ta nén đó, có thêm 1 thư mục bất kì như "anh" và hình ảnh đó ta để vào thư mục này thì ta chỉ cần khai báo đường dẫn tới ảnh là: /anh/icon.png<br/> -Đoạn code dưới sẽ giúp bạn hiểu rõ hơn. Tuy nhiên, ảnh ở Form không thể điều chỉnh căn lề nên bạn chỉ cần xem để biết thôi, nó cũng không có tác dụng gì nhiều khi ở trên Form.<br/> <font color="green"><p align="center">image.java</p></font><br/> <div style="border-style:ridge;border-width:1px;border-color:#FF3366;margin:1px;padding:1px" align="left"><table width="100%"><tr><td><font color="#FFCC99"> import javax.microedition.midlet.*;<br/> import javax.microedition.lcdui.*;<br/> public class image extends MIDlet {<br/> Form f = new Form("Logo");<br/> Image im;<br/> Display d = Display.getDisplay(this);<br/> public image() {<br/> try {<br/> im = Image.createImage("/icon.png");<br/> }<br/> catch(Exception e) { }<br/> }<br/> public void startApp() {<br/> f.append(im);<font color="green">//gắn ảnh lên Form f</font><br/> f.append("Đây là main logo của wap!");<br/> d.setCurrent(f);<br/> }<br/> public void pauseApp() { }<br/> public void destroyApp(boolean unconditional) { }<br/> }<br/> </font></td></tr></table></div><br/> -Projects: <a href="http://u.wtai.cf/down.php?id=86">image.zip</a><br/><hr color="red"><br/> -Trên đây mình đã hướng dẫn về Form và 1 số đối tượng trên Form. Vẫn còn 1 số đối tượng khác nữa nhưng các bạn tạm thời chỉ cần tìm hiểu mấy cái trên là được rùi...<br/> </div></font>
Trang
WAP UPload
và chia sẻ file miễn phí. Coppyright 2015 Вy
VIETUP.NET