Java Reference
In-Depth Information
the Form.append() method to add them to the Form components list.
They are then displayed with the Form . One important detail: the Image
object we use to create the ImageItem retrieves a PNG image from the
root of the JAR file. In order to replicate this behavior in your own project,
just copy a file called test.png to the res folder of the project.
After creating and appending the items, we add the Back command to
the Form ; the emulator's implementation maps it to one of the softkeys.
Finally, we set the CommandListener to be our own MIDlet, which
implements the commandAction() method (defined in the Command -
Listener interface) to handle user actions.
The rest of the constructor creates the other UI components, adds the
Back command to them and sets the MIDlet itself as their event listener.
The following lines create an instance of MenuCanvas and add the Exit
command to it:
canvas = new MenuCanvas();
canvas.addCommand(exitCommand);
canvas.setCommandListener(this);
This canvas is the main screen for our application.
The commandAction() method reacts to the events of the Back and
Exit buttons and the MenuCanvas , changing screens according to the
user's choices. The startApp() method simply sets the MenuCanvas
instance as the main display, using the setDisplayable() utility.
MenuCanvas Class
package example;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Graphics;
public class MenuCanvas extends Canvas {
private Command[] options = new Command[] {
new Command("Form",Command.OK,1),
new Command("List",Command.OK,2),
new Command("Canvas",Command.OK,3),
new Command("TextBox",Command.OK,4),
new Command("Alert",Command.OK,4)
} ;
public MenuCanvas() {
for(int i=0;i<options.length;i++) {
this.addCommand(options[i]);
}
}
protected void paint(Graphics g)
{
g.setColor(0,0,0);
g.fillRect(0,0,getWidth(),getHeight());
Search WWH ::




Custom Search