J2ME MIDP Currency Converter Tutorial for NetBeans IDE 4.0
j2me MIDP Currency Converter Tutorial for NetBeans IDE 4.0
Feedback http://www.netbeans.org/kb/articles/tutorial-currencyconverter-40.html
Feedback
The Currency Converter application you will build in this tutorial shows you how to:
start a j2me MIDP project
code a working j2me MIDP application, or MIDlet, using the IDE
create project configurations to test the application's performance on two different device emulators
The Currency Converter application converts amounts from one currency to two others. You can choose to display three different currencies: euros, yen, or dollars. You can also enter a value in one currency to be converted into the other selected currencies.
There are three Java source code files for the sample application:
ConverterMIDlet.java. The code for the MIDlet class.
Converter.java. A MIDP form that defines the main screen of the application as it appears on a mobile device.
Currencies Selector.java. A MIDP list that maintains the currencies and rates.
The first part of this tutorial will show you how to quickly install, run, and test the Currency Converter application, which is available as a sample project included in the IDE. In the second part of the tutorial, you will create a new project and add code to create and test the application yourself.
This tutorial should take approximately an hour to complete.
Requirements
You must have NetBeans IDE 4.0 and the NetBeans Mobility Pack 4.0 installed before you can start j2me MIDP development. See the j2me MIDP Development Downloadpage for instructions on downloading and installing the complete environment.
Installing and Testing the Sample Currency Converter Project
In this first section, you will see how quickly you can install and run a sample project on two different emulator devices.
Creating the Project Choose File > New Project. Under Categories, select Samples > Mobile. Under Projects, select Currency Converter. Click Next.
The Project Name and Location page sets the name and location of the project folder, and gives you the option of setting the project as the main project. Click Next to accept the defaults.
The Platform selection page sets the default execution environment, the emulator platform, for the project. Note that the default emulator platform is the j2me Wireless Toolkit, and the default device is the DefaultColorPhone, a generic mobile device. Click Finish to complete the wizard.
The Currency Converter displays in the Projects window.
Running the Project Choose Run > Run Main Project.
The Currency Converter displays in the DefaultColorPhone device emulator.
Now you're ready to test the application in the device emulator.
Testing the Application In the DefaultColorPhone screen, click the button under the word "Launch."
Select the currency you want to convert by clicking the up and down arrow keys
on the Select button. You can select Dollars, Euros, or Yen.
Enter the currency amount to convert by clicking the emulator’s numeric keys.
The application makes the conversion calculations and displays the results.
Click the button underneath the word “Exit” to exit the application.
Click the red button in the upper right corner to close the emulator.
Changing the Default Emulator Device You can create different project configurations to test your MIDlet on different emulator platforms, or simply change the device for the default configuration.
Choose File > "Currency Converter" Properties from the Main menu. In the Properties dialog, choose the Platform node. You can change the device for the default configuration.
Click the Device dropdown menu and choose QwertyDevice. Click OK.
Choose Run > Run Main Project Run the application again, and the application runs in the QwertyDevice emulator.
In the next part of this tutorial, you will start over, creating a new project. This will give you an opportunity to learn more about the code behind the application and how you can use the IDE to code and test your own applications.
Creating the Currency Converter Application
Creating the Project Choose File > New Project. Under Categories, select Mobile. Under Projects, select Mobile Application. Click Next.
In the Project Name and Location page, name the project NewCurrencyConverter, and accept the default for Project Home. Leave the Set as Main Project check box checked, as you'll want this project to be your main project Click Next.
Accept the defaults on the Platform page by clicking Finish.
The NewCurrencyConverter application displays in the Projects window.
Creating the converterMIDlet.java MIDlet
Choose File > New File. Under Categories, choose MIDP. Under File Types, choose MIDlet. Click Next.
In the Name and Location page, Enter Currency Converter for the MIDlet name, ConverterMIDlet for the MIDP Class Name, and myconverter for the package name.
Coding the MIDlet
You can write the code for a MIDlet in one of two ways: either by directly entering code in the Source Editor or by using the IDE to add methods, fields, constructors, initializers, classes, and interfaces. Typically, you use the IDE to add new fields and methods to a class, or modify existing fields and methods, and then later fine-tune the code directly in the Source Editor.
The following procedure shows you how to use the tool and the Source Editor to enter or change code. However, to save time and effort, you can also copy the converter code from the example you installed.
Coding the ConverterMIDlet.java MIDlet In the Source Editor, add the following import statements to ConverterMIDlet.java:
import j
推荐文章 |
