当前页面: 开发资料首页 → Netbeans 专题 → 在 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
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.
Converter.java MIDP
            FormConverter for the class name. Click Finish.myconverter package.public class Converter declaration:private ConverterMIDlet midlet;
private int[] translate;
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();
  }
}
commandAction(),
  so it looks like the sample below: public void commandAction(Command command, Displayable displayable) {
    if (command.getCommandType() == Command.EXIT) {
        midlet.destroyApp(true);
    } else if (command.getCommandType() == Command.OK) {
        midlet.showSettings();
    }
 }
itemStateChanged() method,
  so it looks like the sample below: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.
      The final piece of the Currency Converter application is the CurrenciesSelector.java  list file, which defines the currencies that can be
selected for display.
CurrenciesSelector.java          MIDP
            ListCurrenciesSelector            for the class name. Click Finish.myconverter package.public class CurrenciesSelector extends List
              implements CommandListener {, declare a field:private ConverterMIDlet midlet;
   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();
        }
    }
          commandAction(),
            so it looks like the sample below:    public void commandAction(Command command, Displayable displayable) {
        if (command.getCommandType() == Command.OK) {
            getSelectedFlags(midlet.selected);
            midlet.notifySettingsChanged();
        }
    }
          This completes the CurrenciesSelector.java list file.
        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.
You now have a second configuration, QwertyDevice, that has the same properties as the DefaultConfiguration.