J2ME Introduction to the HelloWorld

Source: Internet
Author: User
Introduction

The previous articles have introduced the installation and configuration of Eclipse and its work environment, and have completed all relevant preparations. So starting this article will tell you how to use the previously configured Eclipse development environment to create a J2ME project and start a true J2ME development journey. Since this is the first J2ME program to be created, according to the practice developed by the program, the first place is naturally non-"Hello world" belongs to. HelloWorld Although the function is simple, but small, perfectly formed, through the development of this little program, fully able to make the reader familiar with the J2ME program to develop the general process of the purpose.

   Create a J2ME project

Similar to the development of other language applications, it is necessary to create a new project (project) in the development of a J2ME program. All subsequent coding, debugging, and running are done in this project. After you launch eclipse, click the Project menu item that pops up on the new menu under the File menu. The New Project Wizard dialog box appears as follows:


You first need to specify what project is created, expand the J2ME project, and then go to the next step after you select the J2ME Midlet Suite subkey. The wizard page (shown below) is used to set the project name and the project storage path. Generally take the default path, the project name is naturally the classic "HelloWorld". After Setup is complete, continue with the next setup.






This wizard page needs to be specified for the MIDP version that the application supports. If you consider compatibility, you can choose J2ME Wireless Toolkit 2.1 MIDP 1.0 platform. You can also choose MIDP 2.0, but there are still a few phones that support MIDP 2.0. Click Next to define the Java build settings, which are usually the default values. After clicking the Finish button, Eclipseme will automatically set up the editing and running environment of the project. You can click the Helloworld.jad item in the Navigator view and you will be able to view the project structure generated by Eclipseme in the edit view on the right:



Creating J2ME applications

         

What you just created is MIDlet Suite, also known as the MIDlet application suite, which can contain one or more midlet, but only when you publish it in MIDlet Suite. The substantive work is done in the MIDlet. Therefore, you need to continue adding midlet to the project. Right-click on the Navigator, choose "New", "Other" menu item from the pop-up menu to start the dialog shown above, select the J2ME midlet subkey under J2ME and go to the next step:

           

You need to specify the package (or the default) and name, and Eclipseme will automatically generate the frame code when you click Done. You can add the necessary functional code (shaded parts) to it to achieve the purpose of the "Hello World" character display.

Package demo;
Import Javax.microedition.midlet.MIDlet;
Import javax.microedition.midlet.MIDletStateChangeException;
public class Helloworldmidlet extends MIDlet {
Private textbox textbox;
Public Helloworldmidlet () {
Super ();
TextBox = new TextBox ("Test Program", "Hello world!", 20, 0);
}
protected void startApp () throws Midletstatechangeexception {
Display.getdisplay (This). Setcurrent (textbox);
}
protected void Pauseapp () {
}
protected void Destroyapp (Boolean arg0) throws Midletstatechangeexception {
}
}

When you add the above function code, you will see that there is an error icon in front of it, which is caused by not importing the appropriate package. This is somewhat similar to the lack of a referenced header file in the C + + language. Unlike C + +, which needs to be manually added to the header file, in Eclipse you can click the Organize Import menu item under the Source menu, and Eclipseme will automatically add the required package to the project:

Import Javax.microedition.lcdui.Display;
Import Javax.microedition.lcdui.TextBox;

Run HelloWorld






The code has been written. Click "Run ..." under the "Run" menu to pop up the configuration interface as shown above. In the left configuration window, right-click on "Wireless Toolkit Emulator" and select the "New" menu item in the pop-up menu, the Run Configuration property page shown below will be displayed on the right, generally optional default setting. You can also specify the default device in the Emulation tab. Usually with the "Defaultclolorphone" item, it simulates the effect of running on a color-screen mobile phone. As the current support for Java mobile Phone general configuration is not very low, the vast majority of the color is also the configuration, so the "defaultclolorphone" option should be more popular. Of course, you can also set other items such as "Defaultgrayphone" to simulate the effect of running on a monochrome or other configured phone.

Finally, click the Run button to execute the first J2ME program we just created, which will pop up a window with a color screen phone pattern as shown below. This is the phone simulator, on which you can simulate all the keys of the phone, the function is the same as the real machine. Watch its screen and display the word "Hello world" as shown in the J2ME program you just wrote. Although only a few characters of the display, but it is marked that we have begun to enter the J2ME world. In the next article, the real-machine publishing process for the J2ME program is narrated to complete the production and release of a real mobile app.


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.