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

Source: Internet
Author: User
Tags constructor
Note When you write a mobile program, you often have to switch between screens. Don't know how to do at first, these two days saw an example, is very simple, but very can explain the problem. Sum up. If there is something wrong, please correct the master!
Function: There are multiple buttons in the main program, each button corresponds to a function, each function to a different screen (element) display.
Realize:
One, the main program must define a display pair, such as private display display, it represents the current screen. There are some displayable for the image. such as Form,textfield, etc. are displayable subclasses. In the main program through the Dipslay.sercurrent (displayable instance name); You can add a displayable instance to the current screen. The following programs:
private display display;
Private form form = new Form ("a form");
public void startApp ()
{
Display.setcurrent (form);
}
The effect is to add the form to the current screen.
Second, to the screen between the switch, as long as you want to show the Dongdong to the main program display object can be. A display is defined in the main program, the display of the main program is referenced in another screen (which I would call the target screen).

Development environment: Win2000 server+eclipse+wtk2.1

Use the following code to illustrate:

Mainmidlet.java: The main program, a standard midlet.

Import Javax.microedition.midlet.MIDlet;
Import javax.microedition.lcdui.*;

public class Mainmidlet extends MIDlet implements Commandlistener {

private display display;
Private form form = new Form ("wellcome!!");
Private command Okcommand = new command ("OK", command.ok,1);/select OK, swap to next screen
Private Form NS;
Private Stringitem si = new Stringitem ("A-screen", "~_~");
Public Mainmidlet ()
{
Form.addcommand (Okcommand);
Form.append (SI);
}
public void startApp () {
display = Display.getdisplay (this);

Display.setcurrent (form);
Form.setcommandlistener (this);//join Commandlistener to form

}
public void Pauseapp () {

}
public void Destroyapp (Boolean b) {

}
public void Commandaction (Command c,displayable s)
{
if (C==okcommand)
{
NS = new Nextscreen (display,form);//The most critical place is here:)
Display.setcurrent (NS);
}

}

}
In this midlet, a display pair is defined as display.
And a two Displayable object form form and Stringitem si. It is displayed in the screen after running.
There is also a command Okcommand, which is the function of triggering the next screen.
You can see in the public void Commandlistener that a Nextscreen object is initialized when the currently pressed button is Okcommand
NS = new Nextscreen (display,form);
The display and form are passed in, and the function is to switch the screen.
Here is the implementation of the Nextscreen:
Nextscreen.java code for the second screen
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 (Backcommand);
Setcommandlistener (this);

}
public void Commandaction (Command c,displayable s)
{
Return to previous screen
if (C==backcommand)
{
Display.setcurrent (parent);
}
}

}
It inherits from the form class.
A display display is also defined in Nextscreen, which will be used to identify which screen the current element is displayed in.
A form form, a Stringitem si. This is the current screen to display the Dongdong:)
The function of super ("Secondscreen") in a constructor is to enable Nextscreen to directly tune functions in its parent class form.
The Backcommand function is to return to the previous screen.
Adding Form,si and Backcommand to the Nextscreen, a Nextscreen instance is completed. In the main program (Mainmidlet), the NS.

Next, the most important place is in the mainmidlet of this sentence: Display.setcurrent (NS);
is to display the NS in the current screen! This allows you to see the various elements defined in the Nextscreen (FORM,SI)!

And then want to return to the original screen, how to do? Then the Backcommand in the Nextscreen was working.
Look at these two sentences carefully:
In the Mainmidlet.java:
NS = new Nextscreen (display,form);
It also passes in the form. What's the use of it?
In the Nextscreen constructor:
Dispaly =d;
This sentence actually equals: Nextscreen.display = Mainmidlet.display;
In this way, the nextscreen gets the current screen, it is free to put dongdong on it.
parent = p;
This sentence actually equals: Nextscreen.parent = Mainmidlet.form;
The literal meaning is not difficult to understand, the original is the main program form as parent (parents), so that the current screen of the previous screen to show the content!!
Then in the commandaction, if the Backcommand is pressed, execute display.sercurrent (parent), and then show the original screen again:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.