| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 806 人关注过本帖
标题:J2ME计算器源码(高层界面)
取消只看楼主 加入收藏
lyhuangpeng
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-12-7
收藏
 问题点数:0 回复次数: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;
    }
}
搜索更多相关主题的帖子: 计算器 源码 界面 高层 
2010-12-07 09:49
快速回复:J2ME计算器源码(高层界面)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032015 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved