简单的运算器代码(——j2me编程——)

作者在 2010-04-06 21:01:04 发布以下内容
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;
 
public class Calculate extends MIDlet implements CommandListener{
 
 private Command cmdExit;
 private Command cmdOK;
 private Command cmdadd;
 private Command cmdsubstract;
 private Command cmdmultiply;
 private Command cmddevide;
 private TextField tx1;
 private TextField tx2;
 private TextBox total;
 private Form form;
 //private float total;
 
 public Calculate() {
  // TODO 自动生成构造函数存根
  cmdExit=new Command("退出",Command.EXIT,1);
  cmdOK=new Command("选择",Command.OK,1);
  cmdadd=new Command("加",Command.OK,1);
  cmdsubstract=new Command("减",Command.OK,1);
  cmdmultiply=new Command("乘",Command.OK,1);
  cmddevide=new Command("除",Command.OK,1);
  
  tx1=new TextField("第一个数","",15,TextField.DECIMAL);
  tx2=new TextField("第二个数","",15,TextField.DECIMAL);
  
  total=new TextBox("","  ",15,0);
   form=new Form(null);
  
   form.addCommand(cmdadd);
   form.addCommand(cmdsubstract);
   form.addCommand(cmdmultiply);
   form.addCommand(cmddevide);
form.addCommand(cmdExit);
   total.addCommand(cmdOK);
   total.setCommandListener(this);
   form.setCommandListener(this);
   form.append(tx1);
   form.append(tx2);
 }
 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  // TODO 自动生成方法存根
 }
 protected void pauseApp() {
  // TODO 自动生成方法存根
 }
 protected void startApp() throws MIDletStateChangeException {
  // TODO 自动生成方法存根
  //Display.getDisplay(this).setCurrent(tb1);
  Display.getDisplay(this).setCurrent(form);

 }
 public void commandAction(Command c, Displayable d){
  // TODO 自动生成方法存根
  /*if(c==cmdExit){
   destroyApp(true);
   notifyDestroyed();
  }*/
  float tt1=Float.parseFloat(tx1.getString());
  float tt2=Float.parseFloat(tx2.getString());
  float tt3 = 0;
  if(c==cmdadd){
   tt3=tt1+tt2;
   total.setString(tt3+"");
   Display.getDisplay(this).setCurrent(total);
  }
  if(c==cmdsubstract){
   tt3=tt1-tt2;
   total.setString(tt3+"");
   Display.getDisplay(this).setCurrent(total);
  }
  if(c==cmdmultiply){
   tt3=tt1*tt2;
   total.setString(tt3+"");
   Display.getDisplay(this).setCurrent(total);
  }
  if(c==cmddevide){
   
  if(tt2 == 0){
   Alert a =new Alert("警告","除数不能为零",null,AlertType.INFO);    
    a.setTimeout(5000);
    Display.getDisplay(this).setCurrent(a);
  }
   tt3=tt1/tt2;
  }
  
 }
}
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;



public class Calculate extends MIDlet implements CommandListener{
    
    private Command cmdExit;
    private Command cmdOK;
    private Command cmdadd;
    private Command cmdsubstract;
    private Command cmdmultiply;
    private Command cmddevide;
    private TextField tx1;
    private TextField tx2;
    private TextBox total;
    private Form form;
    //private float total;
    
    public Calculate() {
        // TODO 自动生成构造函数存根
        cmdExit=new Command("退出",Command.EXIT,1);
        cmdOK=new Command("选择",Command.OK,1);
        cmdadd=new Command("加",Command.OK,1);
        cmdsubstract=new Command("减",Command.OK,1);
        cmdmultiply=new Command("乘",Command.OK,1);
        cmddevide=new Command("除",Command.OK,1);
        
        tx1=new TextField("第一个数","",15,TextField.DECIMAL);
        tx2=new TextField("第二个数","",15,TextField.DECIMAL);
        
        total=new TextBox("","  ",15,0);
         form=new Form(null);
        
         form.addCommand(cmdadd);
         form.addCommand(cmdsubstract);
         form.addCommand(cmdmultiply);
         form.addCommand(cmddevide);
form.addCommand(cmdExit);
            total.addCommand(cmdOK);

            total.setCommandListener(this);
            form.setCommandListener(this);
            form.append(tx1);
            form.append(tx2);
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // TODO 自动生成方法存根

    }

    protected void pauseApp() {
        // TODO 自动生成方法存根

    }

    protected void startApp() throws MIDletStateChangeException {
        // TODO 自动生成方法存根

        
//Display.getDisplay(this).setCurrent(tb1);
        Display.getDisplay(this).setCurrent(form);


    }

    public void commandAction(Command c, Displayable d){
        // TODO 自动生成方法存根
        /*if(c==cmdExit){
            destroyApp(true);
            notifyDestroyed();
        }
*/
        float tt1=Float.parseFloat(tx1.getString());
        float tt2=Float.parseFloat(tx2.getString());
        float tt3 = 0;
        if(c==cmdadd){
            tt3=tt1+tt2;
            total.setString(tt3+"");
            Display.getDisplay(this).setCurrent(total);
        }
        if(c==cmdsubstract){
            tt3=tt1-tt2;
            total.setString(tt3+"");
            Display.getDisplay(this).setCurrent(total);
        }
        if(c==cmdmultiply){
            tt3=tt1*tt2;
            total.setString(tt3+"");
            Display.getDisplay(this).setCurrent(total);
        }
        if(c==cmddevide){
            
        if(tt2 == 0){
            Alert a =new Alert("警告","除数不能为零",null,AlertType.INFO);                
                a.setTimeout(5000);
                Display.getDisplay(this).setCurrent(a);
        }
            tt3=tt1/tt2;
        }
        
    }

}

默认分类 | 阅读 710 次
文章评论,共0条
游客请输入验证码
文章分类
文章归档
最新评论