J2ME Learning Notes - Switching of Mobile Phone Screen

xiaoxiao2021-03-06  51

Learning information on J2Medev.com online, today, I have made one, too good ~ Features: There are multiple buttons in the main program, each button corresponds to a function, each function is different screen (element) Explain. 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);} }

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

New Post(0)