J2me java


J2ME Overview

J2ME (Java 2 Micro Edition) is a family of APIs that specifyapplication platforms that are typically implemented by embedded or handhelddevices. Handheld devices include PDAs (Personal Digital Assistants) và high-endmobile phones. These platforms are typically limited in memory capacity and I/Ocapabilities, although they vày have the ability to transfer data over lowbandwidth, intermittent wireless connections.

Bạn đang xem: J2me java

J2ME Stacks


CLDC Requirements:

monochrome 96x54 pixel display

2-way wireless network

input device (keypad, touch screen)

128 Kb for CLDC/MIDP classes + 32 Kb for KVM

CLDC Classes

Interfaces & exceptions not shown.


Object�� Boolean, Character�� Integer, Long, Short, Byte�� Class�� Math�� Runtime, System�� String, StringBuffer�� Thread�� Throwable


InputStream�� ByteArrayInputStream�� DataInputStreamOutputStream�� ByteArrayOutputStream�� DataOutputStream�� PrintStreamReader�� InputStreamReaderWriter�� OutputStreamWriter


CalendarDateHashTableRandomTimerTimerTaskTimeZoneVector�� Stack

MIDP Packages

javax.microedition.iojavax.microedition.lcdui (user interface)�� javax.microedition.lcdui.gamejavax.microedition.media (media player)�� javax.microedition.media.controljavax.microedition.midletjavax.microedition.pki (certification)javax.microedition.rms (persistence)


Motorola SDK


Java SDK

Competing Technologies



Windows CE

Quick Start: "Hello World"

We will be using the Sun/Java J2ME SDK. This SDK includesthe J2ME Wireless Toolkit. Here"s its GUI (called K Toolbar):


Creating a New Project và Midlet

Applications that run on a MIDP/CLDC platform are called midlets.By pressing the "New Project" button we create a new project named Examplescontaining a midlet defined in a tệp tin named HelloWorld.class:


The console window informs us that it has created a projectdirectory named Examples containing src, res, and libsubdirectories:


Other subdirectories have been created as well. We must saveour .java files in the Examplessrc subdirectory.


The tệp tin ExamplessrcHelloWorld.java is createdusing an ordinary text editor. It begins by importing several J2ME packages:

import javax.microedition.lcdui.*;import javax.microedition.midlet.*;

The declaration of the HelloWorld class follows:

public class HelloWorld extends MIDlet �� // Device"s display window & mainscreen:�� private Display theDisplay;�� private Screen mainScreen;�� public HelloWorld() ����� theDisplay =Display.getDisplay(this);����� mainScreen = new TextBox(�������� "Example 1","Hello World", 50, TextField.ANY);�� �� // Lifecycle methods:�� protected void startApp() ����� theDisplay.setCurrent(mainScreen);�� �� protected void destroyApp(booleanunconditional) �� protected void pauseApp()

Notice that HelloWorld extends MIDlet. Two instancevariables are declared and subsequently initialized by the constructor. TheDisplayrepresents the display window of the PDA or mobile phone. MainScreenrepresents the screen that theDisplay is supposed to display. This isaccomplished in startApp() method by the line:


startApp() is one of three lifecycle methods every midletmust define. In most of our examples the other two have empty bodies. We canthink of theDisplay as a mini website browser và mainWindow as atype of web page. In our example, mainPage is a text box titled"Example 1" and containing the text "Hello World".

Running HelloWorld

Pressing the Build button on the toolbar causes the WirelessToolkit lớn compile all .java files in the current project"s sryome.vnubdirectory. The corresponding .class files are placed in the classessubdirectory. Next, the .class files are pre-verified. If a .classfile passes various security checks, it is tagged as verified. All of theproject"s verified files are placed in an archive file called Examples.jad.This file is placed in the bin subdirectory.

Pressing the Run button on the toolbar causes the WirelessToolkit khổng lồ start a sản phẩm điện thoại phone simulator. The Examples applicationsuite is "downloaded" to lớn the "phone," which displays thesuite"s midlets in its window. Currently, HelloWorld is the only midlet.Launching this midlet causes the window to lớn display the midlet"s main page. Usethe red "hang up" button lớn terminate the midlet.



HelloWorld extended the MIDlet class & contains referencesto a Display object (theDisplay) và a TextBox object (mainScreen). TextBoxextends the Screen class, which is the base class for all high-level GUIs.Low-level displays, where the programmer must define his own graphiyome.vn, extendthe Canvas class.


Typically, a midlet creates a collection of displayableobjects-- screens and/or canvases:

Displayable<> displayables = new Displayable;for (int i = 0; i �� // create displayables}

The midlet responds khổng lồ user input commands by selecting adisplayable object from this collection, then asking the display to display itwith the command:


Here"s a more comprehensive class diagram showing the lcduipackage:


First notice that in addition to lớn text boxes, there are threeother types of screens: alerts (a message dialog that flashes on the display),forms (a control panel containing control items such as labels, text fields,and buttons), và lists.

A displayable can fire and handle commands. A command isfired when the user presses keypad buttons when the displayable is the currentwindow. When a command is fired, a command object is passed lớn thecommandAction() method of each registered command listener. Typically, themidlet is the command listener.

Xem thêm: So Sánh Ipad Mini Và Ipad Mini 2 : Cặp Song Sinh Cùng Trứng, Tư Vấn Ipad Mini 1 Và Ipad Mini 2

Form items also fire commands. When an thành công command isfired, a command object is passed lớn the commandAction() method of eachregistered cống phẩm command listener. Typically, the midlet or the parent form isthe thành quả command listener.

The J2ME Application Manager

The Application manager manages the lifecycles of midlets:


Commands và The Midlet as a Command Processor

To demonstrate commands and command processing, let"s địa chỉ cửa hàng anew midlet lớn the Examples project. Pressing the Settings button on theWireless Toolkit toolbar displays the Settings dialog. Pressing the địa chỉ buttonat the bottom of this dialog allows us to add a new project called navigatorcontained in the CommandProcessor.class file:


Running the Command Processor

Here are some screen shots of the Navigator running on thesimulator. Notice that the opening screen now shows the Examples suite containstwo midlets. Selecting the navigator midlet causes the midlet"s first screen toappear. The screen is titled "Screen 0" và displays the question"Shall we go to lớn screen 1?". That"s all Navigator allows users khổng lồ do:to navigate from screen i to lớn screen j.

Notice that the phone"s soft buttons are labeled Backand Menu. The Back button will allow us to lớn return to lớn the midlet"sprevious screen. The thực đơn button displays a menu of commands the current screencan fire:


Here"s what the thực đơn items do:

cmmd_0: displays anunimplemented warning screenOK: ���� displays suggested screen (screen1)Help: �� displays special helpscreen"Exit: �� terminates midletCancel:� Return to previous screen

Here are some shots of these other screens:



The command processor midlet has much in common withHelloWorld. Instead of a single screen, the constructor will create an array offive screens plus a help screen. The startApp() method will displayscreen<0> in theDisplay.

public class CommandProcessor extends MIDlet�� implements CommandListener �� // pre-defined commands:�� private final static Command CMD_EXIT����� = new Command("Exit",Command.EXIT, 1);�� private final static Command CMD_OK����� = new Command("OK",Command.OK, 1);�� private final static Command CMD_CANCEL����� = new Command("Cancel",Command.CANCEL, 1);�� private final static Command CMD_BACK����� = new Command("Back",Command.BACK, 1);�� private final static Command CMD_HELP����� = new Command("Help",Command.HELP, 1);�� // Device display window:�� private Display theDisplay;�� // screens:�� private Screen helpScreen;�� private static final int MAX_SCREENS =5;�� private int currentScreen = 0;�� private Screen<> screens = newScreen;�� // Screen factory method:�� private Screen makeScreen(int i) ...�� // create screens:�� public CommandProcessor() ... �� // Lifecycle methods:�� protected void destroyApp(booleanunconditional) �� protected void pauseApp() �� protected void startApp() ����� theDisplay.setCurrent(screens);�� �� // Command handler method:�� public void commandAction(Command c,Displayable d) ... // CommandProcessor

Creating Screens

Each screen is created in the constructor by calls to themakeScreen() helper method.

CommandProcessor() �� theDisplay = Display.getDisplay(this);�� for(int i = 0; i ����� screens = makeScreen(i);�� String helpText = "Commandprocessors navigate between screens";�� helpScreen = newAlert("Help", helpText, null, AlertType.INFO);�� ((Alert)helpScreen).setTimeout(Alert.FOREVER);�� helpScreen.addCommand(CMD_BACK);�� helpScreen.setCommandListener(this);

The makeScreen() method creates a type of screen called anAlert. An alert is a warning or confirmation that flashes on the display, onlyin our case we are setting the duration of the flash to lớn be forever (users willhave to get rid of the alert by navigating to another screen). Each screen isdeclared capable of firing five pre-defined commands (OK, CANCEL, BACK, HELP,and EXIT) as well as one custom command (cmmd_0, for example). Finally, themidlet is registered as the screens listener:

Screen makeScreen(int i) �� String title = "Screen " + i;�� String text ����� = "Shall we go khổng lồ screen "+ (i + 1) % MAX_SCREENS + "?";�� // Screen s = new TextBox(title, text,50, TextField.ANY);�� Screen s = new Alert(title, text,null, AlertType.INFO);�� ((Alert)s).setTimeout(Alert.FOREVER);�� s.addCommand(CMD_OK);�� s.addCommand(CMD_CANCEL);�� s.addCommand(CMD_BACK);�� s.addCommand(CMD_HELP);�� s.addCommand(CMD_EXIT);�� s.addCommand(newCommand("cmmd_" + i, Command.SCREEN, 1));�� s.setCommandListener(this);�� return s;

Handling Commands

The CommandProcessor implements the CommandListenerinterface. This means that it must implement a commandAction() method and thatit can subsequently be registered as a listener for the screens. A typicalcommandAction() implementation uses a switch statement that dispatches khổng lồ asub-handler based on the type of the đầu vào command. In most cases, a new screenis selected and is mix as theDisplay"s current displayable:

void commandAction(Command c, Displayable d) �� switch(c.getCommandType()) ����� case Command.BACK: // return to"previous" screen�������� if (0 ����������� theDisplay.setCurrent(screens<--currentScreen>);�������� else ����������� theDisplay.setCurrent(screens);�������� �������� break;����� case Command.OK: // go to"next" screen�������� if (currentScreen ����������� theDisplay.setCurrent(screens<++currentScreen>);�������� else ����������� currentScreen = 0;����������� theDisplay.setCurrent(screens);�������� �������� break;����� case Command.CANCEL: // return tomain screen�������� currentScreen = 0;�������� theDisplay.setCurrent(screens);�������� break;����� case Command.HELP: // display helpscreen�������� theDisplay.setCurrent(helpScreen);�������� break;����� case Command.ITEM: // handle formitem command����� case Command.SCREEN: // handlescreen-specific command�������� String gripe �������� �� ="Sorry, " + c.getLabel() + " not implemented";�������� Alert a ����������� = newAlert("Warning", gripe, null, AlertType.WARNING);�������� theDisplay.setCurrent(a);�������� break;����� case Command.EXIT: // terminatemidlet�������� destroyApp(false);�������� notifyDestroyed();�������� break;����� default: // how did we get here?�������� gripe = "Unrecognizedcommand: " + c.getLabel();�������� a = new Alert("Error",gripe, null, AlertType.ERROR);�������� theDisplay.setCurrent(a);�� } // switch}


Simple Menu

MIDP provides a screen & an i9tem for making choices. Bothimplement the Choice interface, which defines three different choice styles:Choice.IMPLICIT, Choice.MULTIPLE, và Choice.EXPLICIT:


Here"s a simple example of an implicit choice các mục titled"Main":


Using the arrows, the user can highlight an item, then pressthe select button or the soft button labeled "Main". An alert appearsmomentarily that displays the index of the nhà cửa selected.


The main thực đơn is created out of a các mục of string items. Themain thực đơn fires a command called CMD_MAIN. The command handler gets the indexof the selected tòa tháp from the main menu.

public class Cascade extends MIDlet�� implements CommandListener �� // main menu + items:�� private Display theDisplay;�� String<> items = "Item1", "Item 2", "Item 3";�� private danh sách mainMenu����� = new List("Main",Choice.IMPLICIT, items, null);�� // pre-defined commands:�� private final static Command CMD_EXIT����� = new Command("Exit",Command.EXIT, 1);�� private final static CommandCMD_MAIN����� = new Command("Main",Command.ITEM, 1);�� public Cascade() ����� theDisplay =Display.getDisplay(this);����� mainMenu = newList("Main", Choice.IMPLICIT, items, null);����� mainMenu.setSelectCommand(CMD_MAIN);����� mainMenu.addCommand(CMD_EXIT);����� mainMenu.setCommandListener(this);�� �� // lifecycle methods:�� protected void destroyApp(booleanunconditional) �� protected void pauseApp() �� protected void startApp() ����� theDisplay.setCurrent(mainMenu);�� �� // Command handler method:�� public void commandAction(Command c,Displayable d) ����� switch(c.getCommandType()) �������� case Command.ITEM:����������� String text = "Itemselected = ";����������� selection += items;����������� Alert a = �������������� newAlert("Warning", text, null, AlertType.INFO);����������� theDisplay.setCurrent(a);����������� break;�������� case Command.EXIT:����������� destroyApp(false);����������� notifyDestroyed();����������� break;����� // switch�� // commandAction()�

https://fb88.world/ | https://nhacai789bet.co/ | 567live XoiLac TV | Kubet