基于宝蓝JBUILDER所提供的MIDP编程框架使开发J2ME应用变成一件所见即所得的简单事情:(本文所实现的MIDP应用直接使用了浮点数运算,必须于支持CLDC 1.1的环境下运行。对于使用CLDC 1.0实现浮点数运算的类库,请与作者公司联系【北京讯岚 TEL:010-51833115】,象征性有偿索取)
package jmobile;
import javax.microedition.midlet.*; import javax.microedition.lcdui.*;
public class MIDletMain extends MIDlet { static MIDletMain instance; Displayable1 displayable = new Displayable1(); Alert alert1; public MIDletMain() { instance = this; try { jbInit(); } catch(Exception e) { e.printStackTrace(); } }
public void startApp() { Display.getDisplay(this).setCurrent(this.alert1,displayable); }
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
public static void quitApp() { instance.destroyApp(true); instance.notifyDestroyed(); instance = null; } private void jbInit() throws Exception { alert1 = new Alert(""); alert1.setType(AlertType.INFO); alert1.setTimeout(2000); alert1.setString("北京讯岚 2004"); alert1.setTitle(""); alert1.setImage(Image.createImage("/jmobile/Untitled-1.png")); }
} public class Displayable1 extends Form implements CommandListener { ChoiceGroup choiceGroup1; TextField textField1; Alert alert1; public Displayable1() { super("QuickLine"); try { jbInit(); } catch (Exception e) { e.printStackTrace(); } }
private void jbInit() throws Exception { // Set up this Displayable to listen to command events String[] str = { "北京", "上海", "深圳", "除北京、上海和深圳的其他地方"}; choiceGroup1 = new ChoiceGroup("", ChoiceGroup.EXCLUSIVE, str, null); textField1 = new TextField("", "", 15, TextField.ANY); alert1 = new Alert(""); alert1.setType(AlertType.INFO); textField1.setLabel("您欲计算的收入"); textField1.setConstraints(TextField.DECIMAL); choiceGroup1.setLabel("选择您所在的城市"); choiceGroup1.setFitPolicy(Choice.TEXT_WRAP_OFF); choiceGroup1.setPreferredSize(145, 20); this.setCommandListener(new Displayable1_this_commandAdapter(this)); setCommandListener(this); // add the Exit command addCommand(new Command("计算", Command.OK, 1)); addCommand(new Command("Exit", Command.EXIT, 2)); this.append(textField1); this.append(choiceGroup1);
}
public void commandAction(Command command, Displayable displayable) { /** @todo Add command handling code */
if(command.getCommandType()==Command.OK){ try{ String str=col(this.textField1.getString()); this.textField1.setString(""); this.alert1.setTimeout(Alert.FOREVER); this.alert1.setString("您应缴纳个人所得税 "+str+" 元");
Display.getDisplay(MIDletMain.instance).setCurrent(this.alert1,this); } catch(Exception e) { this.textField1.setString(""); } }
if ((command.getCommandType() == Command.EXIT) || (command.getCommandType() == Command.STOP)) { // stop the MIDlet MIDletMain.quitApp();
} }
void this_commandAction(Command command, Displayable displayable) {
}
private String col(String input) { try{ float v1=800;
switch(this.choiceGroup1.getSelectedIndex()) { case 0: v1=1000; break; case 1: v1=1000; break; case 2: v1=1600; break; }
float v2 = java.lang.Float.valueOf(input).floatValue(); if (java.lang.Float.isNaN(v2)) { return "ERROR"; }
float v3 = v2 - v1; if (v3 <= 0) { v3 = 0; return java.lang.Float.toString(v3);
} if (v3 <= 500) { return java.lang.Float.toString(v3 * 0.05f);
} if (v3 <= 2000) { return java.lang.Float.toString(v3 * 0.1f - 25);
} if (v3 <= 5000) { return java.lang.Float.toString(v3 * 0.15f - 125);
} if (v3 <= 20000) { return java.lang.Float.toString(v3 * 0.2f - 375);
} if (v3 <= 40000) { return java.lang.Float.toString(v3 * 0.25f - 1375);
} if (v3 <= 60000) { return java.lang.Float.toString(v3 * 0.3f - 3375);
} if (v3 <= 80000) { return java.lang.Float.toString(v3 * 0.35f - 6375);
} if (v3 <= 100000) { return java.lang.Float.toString(v3 * 0.4f - 10375);
} return java.lang.Float.toString(v3 * 0.45f - 15375); } catch(Exception e){ return "ERROR";
}
}
class Displayable1_this_commandAdapter implements javax.microedition.lcdui.CommandListener { Displayable1 adaptee;
Displayable1_this_commandAdapter(Displayable1 adaptee) { this.adaptee = adaptee;
}
public void commandAction(Command command, Displayable displayable) { adaptee.this_commandAction(command, displayable); } } }

|