J2ME Learning Notes (1) - Realize the switch of the mobile phone screen

xiaoxiao2021-03-06  73

When writing a mobile phone program, switching between each screen is often done. I don't know how to do it at the beginning, I have seen an example in these two days, it is very simple, but it can explain the problem. in conclusion. If you have any case, please correct it! Features: There are multiple buttons in the main program, each button corresponds to a function, each function is different on the screen (element). Implementation: 1. The main program inevitably defines a Display Object, such as Private Display Display, which represents the current screen. There are also some Displayable objects. For example, Form, TextField, etc. are subclasses of Displayable. In the main program via DIPSLAY.SERCURRENT (Displayable instance name); you can add the current screen when the Displayable instance is added. The following procedure: private display display; private form form = new form ("a form"); public void startapp () {display.setCurrent (Form);} The role is to add Form to the current screen. Second, to switch between screens, just put it in the DISPLAY object you want to display until the main program. A Display is defined in the main program, then references the main program to the main program in another screen (I am calling it as the target screen). Development Environment: WIN2000 Server Eclipse WTK2.1 Description: MainmIdlet.java: Main program, a standard MIDlet. Import javax.microedition.midlet.midlet; import javax.microedition.lcdui. *;

Public class mainmidlet extends midlet imports commandListener {

Private Display Display; Private form form = new form ("Wellcome !!"); private command okcommand = new command ("ok", command.ok, 1); // Select OK, change to the next screen Private form ns; Private stringItem Si = new stringItem ("first screen", "~ _ ~"); public mainmidlet () {form.addcommand (OkCommand); form.append (si);} public void startapp () {display = display.getdisplay (this); Display.SetCurrent (form); form.setcommandlistener (this); // Add to CommandListener to Form

} Public void pauseapp () {

} Public void destroyApp (boolean b) {

} Public void command (Command C, Displayable S) {if (c == okcommand) {ns = new nextscreen (display, form); // The most critical place here :) Display.setCurrent (ns);}}

} In this MIDlet, a Display Object Display is defined. And a two Displayable object Form Form and StringItem Si. Displayed on the screen after running. There is also a Command OkCommand, and its role is to trigger the next screen. As you can see in Public Void CommandListener, when the current pressed button is OKCommand, an NexTScreen object ns = new nextscreen (Display, Form); incoming Display and Form, the role is to switch the screen. Here is the nextScreen of: NextScreen.java second screen code import javax.microedition.lcdui *; public class NextScreen extends Form implements CommandListener {private Display display; private Displayable parent; private Command backCommand = new Command ( "BACK". , Command.back, 1); Private StringItem Si = New StringItem ("Secondscrean", "~ _ ~"); public nextscreen (Display D, Displayable P) {Super ("NextScreen"); Display = D; Parent = P Append (si); addcommand; setcommandlistener (this);} public void commandion (Command C, Displayable S) {// Returns Previous Screen IF (C == BACKCOMMAND) {Display.setCurrent (PARENT);} }

It inherits from the FORM class. A Display Display has defined in NextScreen, which will use it to identify which screen is displayed in. A Form Form, a StringItem Si. This is the stuff to display in the current screen :) SUPER ("Secondscreen") in the constructor; the role is to make NextScreen can directly adjust the functions in its parent form. The role of BackCommand is to return to the previous screen. Add Form, Si, and BackCommand to NextScreen, a new instance of a NextScreen is complete. In the mainmidlet, NS is NS. Next, the most critical place is this sentence in the mainmidlet: Display.SetCurrent (ns); that is, it is displayed in the current screen! This will see the various elements defined in NexTScreen (Form, Si) ! Then I want to return to the original screen, what should I do? At this time, Backcommand in NextScreen works. Take a closer look: in mainmidlet.java: ns = new nextscreen (Display, Form); it also passed Form. What is it used? In NextScreen's constructor: dispealy = d; this sentence is actually equal to: nextscreen.display = mainmidlet.display; This, NextScreen gets the current screen, it will put it in the top of Dongdong. Parent = P; this sentence is actually equal to: NextScreen.Parent = mainmidlet.form; It is not difficult to understand from the literal meaning. It turned out that the protracitor's Form as a Parent (parent), so that the current screen is displayed in the previous screen of the current screen. Content !! Then in CommandAction, if BACKCOMMAND is pressed, execute display.sercurrent (PARENT); this, also put the original screen out :) Run results:

Mainmidlet.java

NextScreen.java

转载请注明原文地址:https://www.9cbs.com/read-87484.html

New Post(0)