Java Tutorial/J2ME/Command
Содержание
Add Command to MIDlet
<source lang="java">
import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import javax.microedition.lcdui.rumand; import javax.microedition.lcdui.rumandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.TextBox; import javax.microedition.lcdui.TextField; import javax.microedition.lcdui.Ticker; import javax.microedition.midlet.MIDlet; public class TextBoxMIDlet extends MIDlet implements CommandListener {
private static final int MAX_TEXT_SIZE = 64; protected TextBox textBox; protected Display display; private static final Command EXIT_COMMAND = new Command("Exit", Command.EXIT,0); private static final Command OK_COMMAND = new Command("OK", Command.OK, 0); private static final Command CLEAR_COMMAND = new Command("Clear",Command.SCREEN, 1); private static final Command REVERSE_COMMAND = new Command("Reverse",Command.SCREEN, 1); protected void startApp() { String str = null; try { InputStream is = getClass().getResourceAsStream("resources/text.txt"); InputStreamReader r = new InputStreamReader(is); char[] buffer = new char[32]; StringBuffer sb = new StringBuffer(); int count; while ((count = r.read(buffer, 0, buffer.length)) > -1) { sb.append(buffer, 0, count); } str = sb.toString(); } catch (IOException ex) { str = "Failed to load text"; } textBox = new TextBox("TextBox Example", str, MAX_TEXT_SIZE, TextField.ANY); Ticker ticker = new Ticker("This is a ticker..."); textBox.setTicker(ticker); textBox.addCommand(OK_COMMAND); textBox.addCommand(EXIT_COMMAND); textBox.addCommand(CLEAR_COMMAND); textBox.addCommand(REVERSE_COMMAND); textBox.setCommandListener(this); display = Display.getDisplay(this); display.setCurrent(textBox); } protected void pauseApp() { } protected void destroyApp(boolean unconditional) { } 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()); } } }
}</source>
A MIDlet with simple text and a few commands.
<source lang="java">
import javax.microedition.lcdui.rumand; import javax.microedition.lcdui.rumandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.TextBox; import javax.microedition.midlet.MIDlet; public class FewCommandsMIDlet extends MIDlet implements CommandListener {
private Command exitCommand = new Command("A", Command.SCREEN, 1); private Command infoCommand = new Command("B", Command.SCREEN, 2); private Command aboutCommand = new Command("C", Command.SCREEN, 2); private Display display; public FewCommandsMIDlet() { display = Display.getDisplay(this); } public void startApp() { TextBox t = new TextBox("sdf", "Welcome to MIDP Programming", 256, 0); t.addCommand(infoCommand); t.addCommand(exitCommand); t.addCommand(aboutCommand); t.setCommandListener(this); display.setCurrent(t); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } }
}</source>
Exit Command
<source lang="java">
import javax.microedition.lcdui.rumand; import javax.microedition.lcdui.rumandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.TextField; import javax.microedition.midlet.MIDlet; public class TextFieldMIDlet extends MIDlet implements CommandListener {
private Command exitCommand = new Command("Exit", Command.EXIT, 1); private TextField aTextField = new TextField("Label", "Content", 4, TextField.ANY); private Display display = Display.getDisplay(this); public TextFieldMIDlet() { } public void startApp() { Form aForm = new Form("TextField"); aForm.append(aTextField); aForm.addCommand(exitCommand); aForm.setCommandListener(this); display.setCurrent(aForm); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } }
}</source>
Get command label and type
<source lang="java">
import javax.microedition.lcdui.rumand; import javax.microedition.lcdui.rumandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.TextBox; import javax.microedition.midlet.MIDlet; public class J2MECommandGetter extends MIDlet implements CommandListener {
private Command exitCommand = new Command("EXIT", Command.EXIT, 1); private Command infoCommand = new Command("info", Command.SCREEN, 1); TextBox t= new TextBox("Hello MIDP", "Welcome to MIDP Programming", 256, 0); private Display display; public J2MECommandGetter() { display = Display.getDisplay(this); } public void startApp() { t.addCommand(exitCommand); t.addCommand(infoCommand); t.setCommandListener(this); display.setCurrent(t); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } if (c == infoCommand) { ((TextBox) s).setString(infoCommand.getLabel() + " " + infoCommand.getCommandType() + infoCommand.getPriority()); } }
}</source>
Help command
<source lang="java">
import javax.microedition.lcdui.rumand; import javax.microedition.lcdui.rumandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.TextBox; import javax.microedition.midlet.MIDlet; public class J2MEOnlineHelp extends MIDlet implements CommandListener {
private Display display; private Command back = new Command("Back", Command.BACK, 2); private Command exit = new Command("Exit", Command.EXIT, 1); private Command help = new Command("Help", Command.HELP, 3); private Form form = new Form("Online Help Example"); private TextBox helpMesg; public J2MEOnlineHelp() { display = Display.getDisplay(this); helpMesg = new TextBox("Online Help","Press Back to return .",81, 0); helpMesg.addCommand(back); form.addCommand(exit); form.addCommand(help); form.setCommandListener(this); helpMesg.setCommandListener(this); } public void startApp() { display.setCurrent(form); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command command, Displayable displayable) { if (command == back) { display.setCurrent(form); } else if (command == exit) { destroyApp(false); notifyDestroyed(); } else if (command == help) { display.setCurrent(helpMesg); } }
}</source>
Start the MIDlet by creating a list of items and associating the exit command with it
<source lang="java">
/* License
* * Copyright 1994-2004 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistribution of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * * Redistribution in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems, Inc. or the names of contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of any * nuclear facility. */
import javax.microedition.lcdui.Choice; import javax.microedition.lcdui.rumand; import javax.microedition.lcdui.rumandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.List; import javax.microedition.lcdui.TextBox; import javax.microedition.lcdui.TextField; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class EventEx1 extends MIDlet implements CommandListener {
// display manager Display display = null; // a menu with items List menu = null; // main menu // textbox TextBox input = null; // command static final Command backCommand = new Command("Back", Command.BACK, 0); static final Command mainMenuCommand = new Command("Main", Command.SCREEN, 1); static final Command exitCommand = new Command("Exit", Command.STOP, 2); String currentMenu = null; // constructor. public EventEx1() { } /** * Start the MIDlet by creating a list of items and associating the exit * command with it. */ public void startApp() throws MIDletStateChangeException { display = Display.getDisplay(this); menu = new List("Menu Items", Choice.IMPLICIT); menu.append("Item1", null); menu.append("Item2", null); menu.append("Item3", null); menu.append("Item4", null); menu.addCommand(exitCommand); menu.setCommandListener(this); mainMenu(); } public void pauseApp() { display = null; menu = null; input = null; } public void destroyApp(boolean unconditional) { notifyDestroyed(); } // main menu void mainMenu() { display.setCurrent(menu); currentMenu = "Main"; } /** * a generic method that will be called when selected any of the items on the * list. */ public void prepare() { input = new TextBox("Enter some text: ", "", 5, TextField.ANY); input.addCommand(backCommand); input.setCommandListener(this); input.setString(""); display.setCurrent(input); } /** * Test item1. */ public void testItem1() { prepare(); currentMenu = "item1"; } /** * Test item2. */ public void testItem2() { prepare(); currentMenu = "item2"; } /** * Test item3. */ public void testItem3() { prepare(); currentMenu = "item3"; } /** * Test item4. */ public void testItem4() { prepare(); currentMenu = "item4"; } /** * Handle events. */ public void commandAction(Command c, Displayable d) { String label = c.getLabel(); if (label.equals("Exit")) { destroyApp(true); } else if (label.equals("Back")) { if (currentMenu.equals("item1") || currentMenu.equals("item2") || currentMenu.equals("item3") || currentMenu.equals("item4")) { // go back to menu mainMenu(); } } else { List down = (List) display.getCurrent(); switch (down.getSelectedIndex()) { case 0: testItem1(); break; case 1: testItem2(); break; case 2: testItem3(); break; case 3: testItem4(); break; } } }
}</source>