J2ME Learning Notes

Source: Internet
Author: User
Tags exit implement interface
Notes | procedures | Detailed installation of the development environment, the next thing to do is to write a HelloWorld a try!

Open the eclipse--->file--->new--->project---->j2me--->j2me midlet suite--->next---> Input project name---> Next---> Choose the available platforms (we choose midp2.0)--->finish

Right-click the new project in the package window--->new--->other--->j2me--->j2me midlet--->next---> Input helloworld

--->finish

The Helloworld.java file contains the basic framework of the J2ME program. All J2ME applications are derived from the MIDlet class, similar to applets and servlet. All subclasses of the inherited MIDlet class implement the 3 Methods of Startapp (), Pauseapp (), and Destroyapp (). These 3 methods correspond to the 3 states of the application: Run, stop, and perish.

When the application management software has created this MIDlet instance, it is in the stopped state. The Startapp () method is invoked, and the Destroyapp () or Pauseapp () method is called in the running state, and it enters the extinct or stopped state.

In order to be able to exit the program at any time, we also need to let the HelloWorld class implement a command listener interface Commandlistener. This interface has only one method, and the prototype is void Commandaction (Command, displayable). You can handle the command here.

The specific code is as follows:


--------------------------------------------------------------------------------

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

public class HelloWorld extends MIDlet implements commandlistener{

Private Form Form=null;
Private Command Exitcmd=null;
Private String str= "Hello World";

Public HelloWorld () {
Super ();
Form=new Form ("Hello World");
Exitcmd=new Command ("Exit", command.exit,1);
Form.append (str);
Form.addcommand (Exitcmd);

To add a command listener to a form
Form.setcommandlistener (this);
}

protected void startApp () throws Midletstatechangeexception {

Display the form to the current screen
Display.getdisplay (This). Setcurrent (form);
}

protected void Pauseapp () {
}

protected void Destroyapp (Boolean arg0) throws Midletstatechangeexception {
}

public void Commandaction (Command arg0, displayable arg1) {
try {
Destroyapp (FALSE);
Notifydestroyed ();
catch (Midletstatechangeexception e) {
E.printstacktrace ();
}
}
}


--------------------------------------------------------------------------------

You can now run it by selecting Run as emulated J2ME MIDlet in the eclipse's Run menu!

Here are a few of the classes you want to use in this applet:

Form class: Is a container class that inherits from screen. You can put any component or image into the form, such as: various text boxes, selection boxes, and so on. You can also consider it as a component manager. You can add, delete, insert, and set these components by using the form.

Command class: Defines all keystroke actions and component actions. Can be intercepted and processed by Commandlistenter.

Display class: Used to manage systems for displaying devices and input devices. Only one display is allowed for each midlet at the same time.





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.