当前位置: 代码迷 >> Java相关 >> J2ME计算器源码(高层界面)
  详细解决方案

J2ME计算器源码(高层界面)

热度:157   发布时间:2010-12-07 09:49:36.0
J2ME计算器源码(高层界面)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* @author Administrator
*/
public class CalculatorMIDlet  extends MIDlet implements CommandListener{
   private final Command exitCommand  = new Command("退出", Command.EXIT, 2);
    private final Command calcCommand = new Command("计算", Command.SCREEN, 1);
    /** 接收第一个参数. */
    private final TextField arg1 = new TextField("第一个参数", "", 20, TextField.DECIMAL);
    /** 接收第二个参数*/
    private final TextField arg2 = new TextField("第二个参数", "", 20, TextField.DECIMAL);
    /** 显示结果*/
    private final TextField ret = new TextField("结果", "", 20, TextField.UNEDITABLE);
    /** 运算符的下拉列表框 */
    private final ChoiceGroup operator = new ChoiceGroup("运算符", ChoiceGroup.POPUP,
            new String[] {"加", "减", "乘", "除"}, null);
    /** 计算错误警告*/
    private final Alert alert = new Alert("错误", "", null, AlertType.ERROR);
    /** 首次运行标志 */
    private boolean first= false;
    protected void startApp() {
        if (first) {
            return;
        }
        Form myForm= new Form("计算器");
        myForm.append(arg1);
        myForm.append(operator);
        myForm.append(arg2);
        myForm.append(ret);
        myForm.addCommand(calcCommand);
        myForm.addCommand(exitCommand);
        myForm.setCommandListener(this);
        Display.getDisplay(this).setCurrent(myForm);
        first = true;
    }
    protected void destroyApp(boolean unconditional) {}
    protected void pauseApp() {}
    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
            return;
        }
        double result = 0.0;
        if (c == calcCommand) {
        try {
            double d1 = str2double(arg1);
            double d2 = str2double (arg2);
            switch (operator.getSelectedIndex()) {
              case 0: result = d1 + d2; break;
              case 1: result = d1 - d2; break;
              case 2: result = d1 * d2; break;
              case 3: result = d1 / d2; break;
              default:
            }
        } catch (NumberFormatException e) {
            return;
        } catch (ArithmeticException e) {
            alert.setString("除数为零");
            Display.getDisplay(this).setCurrent(alert);
            return;
        }
        String str = Double.toString(result);
        ret.setString(str);
        }
    }
    private double str2double(TextField t)
            throws NumberFormatException {
        String s = t.getString();
        if (s.length() == 0) {
            alert.setString("No  Argument");
            Display.getDisplay(this).setCurrent(alert);
        }
        double n = 0.0;
        try {
            n = Double.parseDouble(s);
        } catch (NumberFormatException e) {
            alert.setString("操作数超过取值范围");
            Display.getDisplay(this).setCurrent(alert);
        }
        return n;
    }
}
搜索更多相关的解决方案: 计算器  源码  界面  高层  

----------------解决方案--------------------------------------------------------
好……
----------------解决方案--------------------------------------------------------
支持 楼主加油
----------------解决方案--------------------------------------------------------
看不懂。努力学习
----------------解决方案--------------------------------------------------------