Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
trang trong đó một loạt các hồ sơ cung cấp cơ sở vật chất còn thiếu có thể được cung cấp, trong một hình thức thích hợp cho lớp thiết bị mà mỗi hồ sơ được thiết kế. Thông tin hồ sơ thiết bị di động, hoặc MIDP cho ngắn, là một trong những hồ sơ, được sử dụng trên các thiết bị nhỏ gọn với một giao diện người dùng hạn chế | J2ME in a Nutshell public class TextBox2MIDlet extends TextBoxMIDlet implements CommandListener Exit command private static final Command EXIT COMMAND new Command Exit Command.EXIT 0 OK command private static final Command OK_COMMAND new Command OK Command.OK 0 Clear text box content private static final Command CLEAR_COMMAND new Command Clear Command.SCREEN 1 Reverse the content of the text box private static final Command REVERSE_COMMAND new Command Reverse Command.SCREEN 1 protected void startApp boolean firstTime started super.startApp If this is the first execution of startApp install commands if firstTime textBox.addCommand OK_COMMAND textBox.addCommand EXIT_COMMAND textBox.addCommand CLEAR_COMMAND textBox.addCommand REVERSE_COMMAND textBox.setCommandListener this Command implementations. public void commandAction Command c Displayable d if c EXIT_COMMAND destroyApp true notifyDestroyed else if c OK_COMMAND System.out.println OK pressed else if c CLEAR_COMMAND textBox.setString null else if c REVERSE_COMMAND String str textBox.getString if str null StringBuffer sb new StringBuffer str textBox.setString sb.reverse .toString Notice that this example is implemented by deriving it directly from the TextBoxMIDlet class from the previous example. Of course you wouldn t normally have to do this in the real world but here it serves to show how easy it is to add command handling to an existing class and you don t need to replicate code that you saw earlier The four Commands are defined as static class members for example 99 J2ME in a Nutshell private static final Command EXIT COMMAND new Command Exit Command.EXIT 0 Since Commands are simply constant-valued objects you can usually define them in this way and then reuse them wherever you need to which would include adding the same instance to more than one screen if necessary. You can see from Example 4-2 that the EXIT and OK commands use the standard types Command.EXIT and Command.OK respectively which allows the .