站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Netbeans 专题在 NetBeans IDE 中使用 Java Web Start(2)

在 NetBeans IDE 中使用 Java Web Start(2)

摘要: The Currency Converter application you will build in this tutorial shows you how to:start a J2ME MIDP project

Creating a MIDP Form

Now that you have completed the code for the MIDlet, you will create the application’s graphical interface. A form is a Java class that can contain an arbitrary mixture of items, including images, read-only and editable text fields, editable date fields, gauges, choice groups, and custom items. The form you create here will specify a text box for each selected currency and specify the ItemStateListener()method to monitor and reflect typed values and perform conversions.

Coding the Converter.java MIDP Form

  1. In the Projects window, right-click the myconverter package. Choose File > New File/Folder.
    The New File wizard opens.
  2. Under Categories, expand MIDP, then expand MIDP Forms. Under File Types choose MIDP Form. Click Next.
  3. In the Name and Location page, enter Converter for the class name. Click Finish.
    A MIDP form is created and added to the myconverter package.
  4. In the Source Editor, add the following fields to the code below the public class Converter declaration:
    private ConverterMIDlet midlet;
    private int[] translate;
  5. Add the following code to complete the constructor, so it looks like the sample below:
    public Converter(ConverterMIDlet midlet) {
    super("Currency Converter"); this.midlet = midlet;
    this.translate = new int[midlet.currencies.length];
    int current = 0;
    for (int i=0; i<translate.length; i++) {
    if (midlet.selected[i]) {
    translate[current++] = i;
    append(new TextField(midlet.currencies[i], "", 12, TextField.NUMERIC));
    }
    }
    try {
    // Set up this form to listen to command events
    setCommandListener(this);
    // Set up this form to listen to changes in the internal state of its interactive items
    setItemStateListener(this);
    // Add the Currencies command
    addCommand(new Command("Currencies", Command.OK, 1));
    // Add the Exit command
    addCommand(new Command("Exit", Command.EXIT, 1)); } catch(Exception e) {
    e.printStackTrace();
    } }
  6. Add the following code to complete the method commandAction(), so it looks like the sample below:
  7.  public void commandAction(Command command, Displayable displayable) {
    if (command.getCommandType() == Command.EXIT) {
    midlet.destroyApp(true);
    } else if (command.getCommandType() == Command.OK) {
    midlet.showSettings();
    } }
  8. Add the following code to complete the itemStateChanged() method, so it looks like the sample below:
  9. public void itemStateChanged(Item item) {
    try {
    long value = Long.parseLong(((TextField)item).getString());
    int from = 0;
    while (get(from) != item) from++;
    from = translate[from];
    for (int i=0; i<size(); i++) {
    int to = translate[i];
    if (from != to) {
    ((TextField)get(i)).setString(String.valueOf(midlet.convert(value, from, to)));
    }
    }
    } catch (NumberFormatException nfe) {
    for (int i=0; i<size(); i++) {
    ((TextField)get(i)).setString("");
    }
    }
    }
    This completes the Converter.java form file.

Creating a MIDP List

The final piece of the Currency Converter application is the CurrenciesSelector.java list file, which defines the currencies that can be
selected for display.

Coding the CurrenciesSelector.java MIDP List

  1. In the Projects window, right-click the myconverter package. Choose File > New File/Folder.
    The New File wizard opens.
  2. Under Categories, expand MIDP, then expand MIDP Forms. Under File Types choose MIDP List. Click Next.
  3. In the Name and Location page, enter CurrenciesSelector for the class name. Click Finish.
    A MIDP list file is created and added to the myconverter package.
  4. After the line public class CurrenciesSelector extends List implements CommandListener {, declare a field:
    private ConverterMIDlet midlet;
  5. Add the following code to complete the constructor, so it looks like the sample below:
       public CurrenciesSelector(ConverterMIDlet midlet) {
    super("Select Currencies", List.MULTIPLE, midlet.currencies, null);
    this.midlet = midlet;
    setSelectedFlags(midlet.selected);
    try {
    // Set up this list to listen to command events
    setCommandListener(this);
    // Add the Save command
    addCommand(new Command("Save", Command.OK, 1));
    } catch(Exception e) {
    e.printStackTrace();
    }
    }
  6. Add the following code to complete the method commandAction(), so it looks like the sample below:
  7.     public void commandAction(Command command, Displayable displayable) {
    if (command.getCommandType() == Command.OK) {
    getSelectedFlags(midlet.selected);
    midlet.notifySettingsChanged();
    }
    }
    This completes the CurrenciesSelector.java list file.

Testing Your Application

Now that you have created your application, you can test it with different emulator devices, as you did with the sample Currency Converter project you first installed. However, instead of switching the emulator device in the default configuration, this time you will create a second project configuration for the QwertyDevice device emulator.

Creating a New Project Configuration

  1. Choose File > "NewCurrencyConverter" Properties.
  2. Click the Manage Configurations button.
    The Project Configuration Manager opens.
  3. Click the Add Button.
    The Add Configuration button displays.
  4. Name the new configuration QwertyDevice. Click Ok.
  5. Click Close to close the Project Configuration Manager.

You now have a second configuration, QwertyDevice, that has the same properties as the DefaultConfiguration.

Changing the Device Property

  1. While still in the Project Properties, choose Platform from the tree menu in the left pane of the window.
  2. If it QwertyDevice is not shown as the active project configuration, choose it from the Project Configuration dropdown menu.
  3. So that you can choose new values for this configuration, Uncheck the "Use Values from DefaultConfiguration" check box.
  4. Choose QwertyDevice from the Device dropdown menu. Click OK.

Running the Application on Both Configurations

  1. Make sure that the Configuration dropdown menu on the toolbar lists DefaultConfiguration as the active project configuration.
  2. Choose Run > Run Main Project.
    The Currency Converter opens in the DefaultColorPhone Device emulator screen.
  3. Choose QwertyDevice from the Configuration dropdown menu in the toolbar.
  4. Choose Run > Run Main Project.
    The Currency Converter opens in the QwertyDevice Device emulator screen.
  5. Now you can test and compare the application's performance on different devices at the same time.

↑返回目录
前一篇: 在 NetBeans IDE 中使用 Java Web Start(1)
后一篇: J2ME MIDP Development for NetBeans IDE 4.0 Emulators