当前页面: 开发资料首页 → Netbeans 专题 → 在 NetBeans IDE 中使用 Java Web Start(1)
摘要: The Currency Converter application you will build in this tutorial shows you how to:start a J2ME MIDP project
The Currency Converter application you will build in this tutorial shows you how to:
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.
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.
In this first section, you will see how quickly you can install and run a sample project on two different emulator devices.
You can create different project configurations to test your MIDlet on different emulator platforms, or simply change the device for the default configuration.
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.
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.converterMIDlet.java
MIDletCurrency Converter
for
the MIDlet name, ConverterMIDlet
for the MIDP Class Name,
and myconverter
for the package name. 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.
ConverterMIDlet.java
MIDletConverterMIDlet.java
:import java.io.*;
import javax.microedition.rms.*;
storedDataStr
to the MIDlet. The storedDataStr
string contains
the name of the
RMS stored record.
storedDataStr
, in the Name box and selectstoredDataStr
to "ConverterData".The field is added to the code in the Source Editor window.
Add the following fields to the ConverterMIDlet.java
class using
the Source Editor.
You can use the Add Field dialog box, copy the text from this page,
or from the installed Currency Converter application,
and paste it in the Source Editor. Be careful, however, not to change
the package name from myconverter
.
public class ConverterMIDlet extends javax.microedition.midlet.MIDlet {
private static String storedDataStr = "ConverterData"; public String[] currencies = new String[] { "US $", "Yen \u00a5", "Euro \u20ac" };
public boolean[] selected = new boolean[] { true, true, true, true };
public long[][] rates = {{ 1000000, 117580000, 911079 },
{ 8504, 1000000, 7749 }, { 1097600, 129056000, 1000000 }};
private RecordStore storedData;
startApp()
.
This method is called when the application is started. It loads all
the data (currencies, selected currencies, and exchange rates) from
persistent storage
and initially displays the Converter form. The method should look like
this:public void startApp() {
try { storedData = RecordStore.openRecordStore(storedDataStr, true); if (storedData.getNumRecords() > 0) { DataInputStream in = new DataInputStream(new ByteArrayInputStream(storedData.getRecord(1)));
try {
int size = in.readInt();
currencies = new String[size];
selected = new boolean[size];
rates = new long[size][];
for (int i=0; i<size; i++) {
currencies[i] = in.readUTF();
selected[i] = in.readBoolean();
rates[i] = new long[size];
for (int j=0; j<size; j++) {
rates[i][j] = in.readLong(); }
} in.close();
} catch (IOException ioe) {
}
}
} catch (RecordStoreException e) {
}
notifySettingsChanged();
}
destroyapp()
method is called when the application
is finished, or destroyed. Add the following code to complete the
destroyApp()
method:public void destroyApp(boolean unconditional) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(bytes);
try {
out.writeInt(currencies.length);
for (int i=0; i<currencies.length; i++) {
out.writeUTF(currencies[i]);
out.writeBoolean(selected[i]);
for (int j=0; j<currencies.length; j++) {
out.writeLong(rates[i][j]);
}
}
out.close();
if (storedData.getNumRecords() > 0)
storedData.setRecord(1, bytes.toByteArray(), 0, bytes.size());
else
storedData.addRecord(bytes.toByteArray(), 0, bytes.size());
} catch (IOException ioe) {
ioe.printStackTrace();
}
} catch (RecordStoreException e) {
e.printStackTrace();
}
notifyDestroyed();
}
showSettings()
public void showSettings() { Display.getDisplay(this).setCurrent(new CurrenciesSelector(this)); }
notifySettingsChanged()
public void notifySettingsChanged() {
Display.getDisplay(this).setCurrent(new Converter(this));
}
longconvert()
fridx
and toidx
values are the indexes of the source
and target currencies.public long convert(long frval, int fridx, int toidx) {8. Save the ConverterMIDlet by choosing File > Save.
return (frval * rates[fridx][toidx]) / 1000000;