急!使用jb7+mobileSet3.0者请进来。。。。
j2sdk + wtk自代的例子,在j2sdk + wtk中运行正常,但放到我的jb7+mobileSet3.0环境下,发生很多错误。。为什么呢????我初学, 始终没该好,我把出错的地方注释了请大家测试后,修改后可以发到我邮箱(resintwo@sina.com),谢谢。。
package calculator;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
public final class CalculatorMIDlet extends MIDlet implements CommandListener {
private static final int NUM_SIZE=20;
private final Command exitCmd = new Command("Exit", Command.EXIT, 2);
private final Command calcCmd = new Command("Calc", Command.SCREEN, 1);
private final TextField t1 = new TextField(null, "", NUM_SIZE,
TextField.DECIMAL);//TextField.DECIMAL出错。。没有.DECIMAL方法
private final TextField t2 = new TextField(null, "", NUM_SIZE,
TextField.DECIMAL);//TextField.DECIMAL出错。。没有.DECIMAL属性
private final TextField tr = new TextField("Result", "", NUM_SIZE,
TextField.UNEDITABLE);//TextField.DECIMAL出错。。没有.DECIMAL属性
private final ChoiceGroup cg = new ChoiceGroup("", ChoiceGroup.POPUP,
new String[] {"add", "subtract", "multiply", "divide"}, null);
//ChoiceGroup.POPUP出错。。没.POPUP属性
private final Alert alert = new Alert("Error", "", null, AlertType.ERROR);
private boolean isInitialized = false;
protected void startApp() {
if (isInitialized) {
return;
}
Form f = new Form("FP Calculator");
f.append(t1);
f.append(cg);
f.append(t2);
f.append(tr);
f.addCommand(exitCmd);
f.addCommand(calcCmd);
f.setCommandListener(this);
Display.getDisplay(this).setCurrent(f);
alert.addCommand(new Command("Back", Command.SCREEN, 1));
isInitialized = true;
}
protected void destroyApp(boolean unconditional) {}
protected void pauseApp() {}
public void commandAction(Command c, Displayable d) {
if (c == exitCmd) {
destroyApp(false);
notifyDestroyed();
return;
}
double res = 0.0;
try {
double n1 = getNumber(t1, "First");//getNumber出错
double n2 = getNumber(t2, "Second");//getNumber出错
switch (cg.getSelectedIndex()) {
case 0: res = n1 + n2; break;
case 1: res = n1 - n2; break;
case 2: res = n1 * n2; break;
case 3: res = n1 / n2; break;
default:
}
} catch (NumberFormatException e) {
return;
} catch (ArithmeticException e) {
alert.setString("Divide by zero.");
Display.getDisplay(this).setCurrent(alert);
return;
}
String res_str = Double.toString(res);//Double.toString出错
if (res_str.length() > tr.getMaxSize()) {
tr.setMaxSize(res_str.length());
}
tr.setString(res_str);
}
private double getNumber(TextField t, String type)
throws NumberFormatException {
String s = t.getString();
if (s.length() == 0) {
alert.setString("No " + type + " Argument");
Display.getDisplay(this).setCurrent(alert);
throw new NumberFormatException();
}
double n;
try {
n = Double.parseDouble(s);//Dole.parseDouble出错。。
} catch (NumberFormatException e) {
alert.setString(type + " argument is out of range.");
Display.getDisplay(this).setCurrent(alert);
throw e;
}
return n;
}
}