Author: Natsuhiko end
Next, let's introduce this package and its related content. Everyone knows that in J2se we use AWT or SWING to develop user interfaces, but in J2ME, the development user interface is not the above AWT or SWING, we use it is The new LCDUI package designed for mobile devices (ie Limited Configuration Device User Interface).
The following figure is a brief structure of the LCDUI package:
In MIDP, the callback function related to the user interface is four: (1) javax.microedition.lcdui.Command class; (2) javax.microedition.lcdui.canvas class (3) When the screen is redrawn, Painting events, at this time, the PAINT () method of the Canvas class will be called, and it is passed to a Call of a Graphics object; (4) When calling the CallSerially () method of the Display class, it will be introduced into the class of Runnable interface, where The run () method will be called. Let's introduce the class of javax.microedition.lcdui.command, which applies to advanced APIs, and is used for low-level APIs. The COMMAND class has two constructor, one has three parameters, another One is four parameters. The difference is that the first parameter of the former is displayed on the screen. The second parameter is the type of command. The third parameter is priority, indicated in the form of natural numbers, such as: 1, 2, 3 ..., the smaller priority High, representatives displayed on the screen. The latter is a long command to add a long command based on the former, which is located between the first and second parameters of the former, and the specific models are different from the manufacturer's implementation. The types of commands previously are divided into 8 types: command.back, ommand.exit, command.cancel, command.help, command.item, command.ok, command.screen, command.stop. Example: Command C = New Command ("Cancel", Command.cancel, 1); Command a = New Command ("This is a long command", "this is short command", command.back, 1); on the screen It is not necessary to show these without any meaning, we must also combine the Command class and javax.microedition.lcdui.commandListener to react to the user's actions, because setcommandListener () is defined in DisplayAble, so CommandListener is the same as Command. General with advanced API and low-level API event processing interfaces.
It should be noted that CommandListener is a Unicast mechanism, that is, only one time handler can be registered at the same time.
Below is a general case of using Command and CommandListener in MIDlet:
Public Class Example1 Extends MIDlet ImmancelliStener
{
Public void startApp ()
{
.........
Command exp1 = new command ("command 1"
Command.ok, 1);
.........
}
Public void pauseApp ()
{
.........
}
Public void destroyApp (Boolean Exp)
{
.........
}
Public void CommandAction (Command C, Displayable S)
{
.........
}
}
Introduce the Command class, let's introduce the Ticker class. In MIDP 1.0, it can only be used for SCREEN's subclasses, and after MIDP2.0, it is already available for all subclats of Displayable. It is a class similar to running the horse, which is usually seen, we usually use setticker () to set the Ticker on the screen, or use getticker () to get the Ticker object on the screen, setticker () The strings in the dual quotes in parentheses are displayed in the running horse.
Here is an example in detail the usage of several classes, as the summary of this chapter.
Import javax.microedition.lcdui. *;
Import javax.microedition.midlet. *;
Public Class DisplayAbletestMIDlet Extends Midlet ImmitionListener
{
Public void startApp ()
{
Display = display.getdisplay (this);
}
Public void pauseApp ()
{
f = new form ("Displayable Test");
Command test1 = new Command ("Start Ticker", Command.Screen, 1);
Command test2 = new Command ("Stop Ticker", Command.Screen, 1);
Command test3 = new command ("Exit", command.exit, 1);
f.addCommand (Test1);
F.addCommand (TEST2);
f.addCommand (TEST3);
F.setCommandListener (this);
Display.setcurrent (f);
}
Public void destroyApp (Boolean Unconditional)
{
}
Public void CommandAction (Command C, Displayable S)
{
String cmd = c.getlabel ();
IF (cmd.equals ("Start Ticker"))))
{
F.setticker (New Ticker));
}
IF (cmd.equals ("stop Ticker")))
{
F.setticker (NULL);
}
}
Private display display;
Form f;
}
The Screen class has four related subclasses, which are Alert, List, TextBox, Form. These four subclasses can be subdivided into two categories. The top three belong to the classes that encapsulate more complex user interfaces, we can only take simple To use, the composition structure of its internal cannot be modified; relative to the top three, FORM is more free, it is the default default, there is no user interface, which is similar to a container, which can accommodate the subclass of the Item class. To constitute a complex graphical user interface.
Let's take a detailed introduction to these four subclasses:
According to the definition in the Choice interface, the List component is divided into choice.exclusive, choice.multiple, choice.implicit, the difference between them is also very simple, you only need to read from the literal, as a radio The biggest difference between type, multi-selected type, simple radio, Implicit and Exclusive is that the list of choice.Imlicit types will trigger the time immediately after the user selection, and use list.select_command as the CommandAction () function. A parameter is incoming, via a secondary discrimination, we can know if the event is triggered by a list, because this type of list is only one option at the same time will be selected. The Choice interface provides a mechanism called Fit Policy. The text will automatically arrange the next row, while choice.text_wrap_off automatically truncated, and Default is based on the manufacturer's default. The code demonstration is as follows:
Import javax.microedition.lcdui. *;
Import javax.microedition.midlet. *;
Public Class ImplicitListwithfitPolicymidlet Extends Midlet ImmmandListener
{
Private display display;
Public IMPLICISTWITPOLICYMIDLET ()
{
Display = display.getdisplay (this);
}
Public void startApp ()
{
Image img = null;
Try
{
IMG = image.createImage ("/ a.png");
} catch (Exception E)
{
System.out.println (e);
}
List L = New List ("List Test", choice.implicit;
System.out.println ("Default Fit Policy:" L.GetFitPolicy ());
L.Append ("12345678901234567890", IMG);
L.Append ("AbcDefgabcDefg", IMG);
Command C1 = New Command ("WRAP_OFF", Command.ok, 1);
L.Addcommand (C1);
Command C2 = New Command ("Wrap_on", Command.ok, 1);
L.ADDCOMMAND (C2);
Command C3 = New Command ("Wrap_Default", Command.ok, 1);
L.ADDCommand (C3);
L.SETCOMMANDLISTENER (THIS);
Display.SetCurrent (L);
}
Public void CommandAction (Command C, Displayable S)
{
String cmd = c.getlabel ();
IF (cmd.equals ("wrap_off")))
{
List TMP = (List) S;
TMP.SETFITPOLICY (choice.text_wrap_off);
IF (cmd.equals ("wrap_on")))
{
List TMP = (List) S;
Tmp.SetFitPolicy (choice.text_wrap_on);
}
IF (cmd.equals ("wrap_default")))
{
List TMP = (List) S;
Tmp.SetFitPolicy (choice.text_wrap_default);
}
}
Public void pauseApp ()
{
}
Public void destroyApp (Boolean Unconditional)
{
}
}
Let's talk about TextBox again, when the user needs to enter text, TextBox will send it, and the TextBox definition is as follows: TextBox TB = New TextBox ("Text Input Test", "Name", 9, TextField.Any);
Where 'input test' is Title, and 'name' is our content, the third parameter is the number of characters we have to limit, and the fourth parameter is a further limit for the input content, Any refers to allowing any character or numbers to be entered. Similar to EmailAddr refers to allowing input email addresses, and the like (see API documentation).
Let's introduce AlertType, which is a tool class that it is unable to physically provide several definition AlertType to assist the Alert class. For example: ALARM (Alert), Confirmation, etc., Alert is a more special Screen class. When we use Display.SetCurrent (), it will send a sound first. Then display himself on the screen, after a while, jump back the previous screen, so please note that we must make the system must have a picture, so that the Alert strike, otherwise, there will be an error, below Code prompts the use of Alert:
Import javax.microedition.lcdui. *;
Import javax.microedition.midlet. *;
Public class alertsplashscreenmidlet extends MIDLET
{
Private display display;
Public alertsplashscreenmidlet ()
{
Display = display.getdisplay (this);
}
Public void startApp ()
{
Alert a1 = new alert ("Application Start");
A1.SETTYPE (AlertType.info);
SetTimeout (5000);
A1.setString ("Please wait" in the application initialization ");
Form f = new form ("Main screen");
DSPLAY.SETCURRENT (A1, F);
}
Public void pauseApp ()
{
}
Public void destroyApp (Boolean Unconditional)
{
}
}
As for Form, it is a subclass of the ultimately required in this Screen class. It is the longest in the advanced graphical interface. We will continue to explore it in the following chapters ~
Article Source: http://www.j2medev.com/Article/showArticle.asp? ArticleId = 205 code demonstration is as follows:
Import javax.microedition.lcdui. *;
Import javax.microedition.midlet. *;
Public Class ImplicitListwithfitPolicymidlet Extends Midlet ImmmandListener
{
Private display display;
Public IMPLICISTWITPOLICYMIDLET ()
{
Display = display.getdisplay (this);
}
Public void startApp ()
{
Image img = null;
Try
{
IMG = image.createImage ("/ a.png");
} catch (Exception E)
{
System.out.println (e);
}
List L = New List ("List Test", choice.implicit;
System.out.println ("Default Fit Policy:" L.GetFitPolicy ());
L.Append ("12345678901234567890", IMG);
L.Append ("AbcDefgabcDefg", IMG);
Command C1 = New Command ("WRAP_OFF", Command.ok, 1);
L.Addcommand (C1);
Command C2 = New Command ("Wrap_on", Command.ok, 1);
L.ADDCOMMAND (C2);
Command C3 = New Command ("Wrap_Default", Command.ok, 1);
L.ADDCommand (C3);
L.SETCOMMANDLISTENER (THIS);
Display.SetCurrent (L);
}
Public void CommandAction (Command C, Displayable S)
{
String cmd = c.getlabel ();
IF (cmd.equals ("wrap_off")))
{
List TMP = (List) S;
Tmp.SetFitPolicy (choice.text_wrap_off);
}
IF (cmd.equals ("wrap_on")))
{
List TMP = (List) S;
Tmp.SetFitPolicy (choice.text_wrap_on);
}
IF (cmd.equals ("wrap_default")))
{
List TMP = (List) S;
Tmp.SetFitPolicy (choice.text_wrap_default);
}
}
Public void pauseApp ()
{
}
Public void destroyApp (Boolean Unconditional)
{
}
}
Let's talk about TextBox again, when the user needs to enter text, TextBox will send it, and the TextBox definition is as follows: TextBox TB = New TextBox ("Text Input Test", "Name", 9, TextField.Any);
Where 'input test' is Title, and 'name' is our content, the third parameter is the number of characters we have to limit, and the fourth parameter is a further limit for the input content, Any refers to allowing any character or numbers to be entered. Similar to EmailAddr refers to allowing input email addresses, and the like (see API documentation). Let's introduce AlertType, which is a tool class that it is unable to physically provide several definition AlertType to assist the Alert class. For example: ALARM (Alert), Confirmation, etc., Alert is a more special Screen class. When we use Display.SetCurrent (), it will send a sound first. Then display himself on the screen, after a while, jump back the previous screen, so please note that we must make the system must have a picture, so that the Alert strike, otherwise, there will be an error, below Code prompts the use of Alert:
Import javax.microedition.lcdui. *;
Import javax.microedition.midlet. *;
Public class alertsplashscreenmidlet extends MIDLET
{
Private display display;
Public alertsplashscreenmidlet ()
{
Display = display.getdisplay (this);
}
Public void startApp ()
{
Alert a1 = new alert ("Application Start");
A1.SETTYPE (AlertType.info);
SetTimeout (5000);
A1.setString ("Please wait" in the application initialization ");
Form f = new form ("Main screen");
DSPLAY.SETCURRENT (A1, F);
}
Public void pauseApp ()
{
}
Public void destroyApp (Boolean Unconditional)
{
}
}
As for Form, it is a subclass of the ultimately required in this Screen class. It is the longest in the advanced graphical interface. We will continue to explore it in the following chapters ~
Source: http://www.j2medev.com/article/showArticle.asp? ArticleId = 205