counterform.java
来自「手机在线系统 采用Java 中的J2ME, JSP 跟MySql 运行环」· Java 代码 · 共 390 行
JAVA
390 行
/**
* @(#)Counter.java 1.11 01/08/23
* Copyright (c) 2004-2005 wuhua of workroom Inc. All Rights Reserved.
* @version 1.0, 10/05/2004
* @author 饶荣庆
* @author
*/
package com.j2me.counter;
import com.j2me.about.*;
import com.j2me.language.*;
import com.j2me.main.*;
import javax.microedition.lcdui.*;
/**
*类Counter是用来实现简单的四则运算。
*/
public class CounterForm extends Form implements CommandListener, ItemStateListener
{
public TextField counterNumber = new TextField("请输入运算数据:", null, 50, TextField.ANY); //用于用户输入操作数
public TextField counterResult = new TextField("运算结果", null, 100, TextField.ANY); //用于显示结果
public Command okCommand = new Command("确定", Command.OK, 2); //定义确定软键
public Command backCommand = new Command("返回", Command.EXIT, 2); //定义离开软键
public ChoiceGroup mathChoice = new ChoiceGroup("请选择", ChoiceGroup.EXCLUSIVE); //定义选择框
public Alert alarmAlert = new Alert("警告", "不能对数据进行转换!",null,AlertType.ALARM); //初始化警告信息
private Display display = null;
private MainList mainList = null;
private String first = null; //用于获得第一个操作数
private String last = null; //用于获得第二个操作数
private boolean isArithmetic = true; //用于判断是否四则运算;
private boolean isSin =false;
private boolean isCos =false;
private boolean isTan =false;
private boolean isSqrt =false;
private boolean isAlarm = false; //检测出错时弹出警告窗口
/**
*初始化类的一些元素
*/
public CounterForm(String s) //把屏幕,跟主界面传进去
{
super(s);
this.display = display;
this.mainList = mainList;
alarmAlert.setTimeout(1000);
this.display = display;
this.mainList = mainList;
this.append(counterNumber);
this.append(counterResult);
mathChoice.append("四则运算", null);
mathChoice.append("求正弦",null);
mathChoice.append("求余弦",null);
mathChoice.append("求正切",null);
mathChoice.append("开平方",null);
this.append(mathChoice);
this.addCommand(okCommand);
this.addCommand(backCommand);
this.setCommandListener(this);
this.setItemStateListener(this);
}
public void showForm(Display display, MainList mainList)
{
this.display = display;
this.mainList = mainList;
this.display.setCurrent(this);
}
public String getOperationData() //返回操作数据
{
return this.counterNumber.getString();
}
/**
*此方法是用来判断操作符并根据它进行简单运算
*/
public void arithmetic()
{
double x = 0; //第一个操作数
double y = 0; //第而个操作数
String s = this.getOperationData(); //获得操作数
int i = this.getOperationData().indexOf("+"); //查找操作符
if (i > 0)
{
first = s.substring(0, i).trim(); //去除空格;
last = s.substring(i + 1).trim();
try
{
x = Double.parseDouble(first);
y = Double.parseDouble(last);
}
catch( NumberFormatException e) //进行异常处理
{
isAlarm = true; //赋值确定警告信息
}
counterResult.setString(String.valueOf(Operation.add(x, y))); //把基本类型转换成字符串
}
else
{
i = s.indexOf("-");
if (i > 0)
{
first = s.substring(0, i).trim();
last = s.substring(i + 1).trim();
try
{
x = Double.parseDouble(first);
y = Double.parseDouble(last);
}
catch( NumberFormatException e)
{
isAlarm = true;
}
counterResult.setString(String.valueOf(Operation.minus(x, y)));
}
else
{
i = s.indexOf("*");
if (i > 0)
{
first = s.substring(0, i).trim();
last = s.substring(i + 1).trim();
try
{
x = Double.parseDouble(first);
y = Double.parseDouble(last);
}
catch( NumberFormatException e)
{
isAlarm = true;
}
counterResult.setString(String.valueOf(Operation.multiply(x, y)));
}
else
{
i = s.indexOf("/");
if (i > 0)
{
first = s.substring(0, i).trim();
last = s.substring(i + 1).trim();
try
{
x = Double.parseDouble(first);
y = Double.parseDouble(last);
}
catch( NumberFormatException e)
{
isAlarm = true;
}
try
{
counterResult.setString(String.valueOf(Operation.division(x,y)));
}
catch(ArithmeticException e)
{
counterResult.setString("除数不能为0");
}
}
else
{
i = s.indexOf(">");
if (i > 0)
{
first = s.substring(0, i).trim();
last = s.substring(i + 1).trim();
try
{
x = Double.parseDouble(first);
y = Double.parseDouble(last);
}
catch(NumberFormatException e)
{
isAlarm = true;
}
counterResult.setString("x, y较大者为:" + Operation.max(x,y));
}
else
{
i = s.indexOf("<");
if (i > 0)
{
first = s.substring(0, i).trim();
last = s.substring(i + 1).trim();
try
{
x = Double.parseDouble(first);
y = Double.parseDouble(last);
}
catch( NumberFormatException e)
{
isAlarm = true;
}
counterResult.setString("x, y较小者为:" + Operation.min(x,y));
}
else
{
counterResult.setString("请输全操作数");
}
}
}
}
}
}
if (isAlarm == true)
{
display.setCurrent(alarmAlert);
isAlarm = false; //弹出后。重新设置为否。
}
}
public void sin()
{
double x = 0;
try
{
x = Double.parseDouble(this.getOperationData());
}
catch(NumberFormatException e)
{
isAlarm = true;
}
counterResult.setString(String.valueOf(Math.sin(x)));
if (isAlarm == true)
{
display.setCurrent(alarmAlert);
isAlarm = false;
}
}
public void cos()
{
double x = 0;
try
{
x = Double.parseDouble(this.getOperationData());
}
catch(NumberFormatException e)
{
isAlarm = true;
}
counterResult.setString(String.valueOf(Math.cos(x)));
if (isAlarm == true)
{
display.setCurrent(alarmAlert);
isAlarm = false;
}
}
public void tan()
{
double x = 0;
try
{
x = Double.parseDouble(this.getOperationData());
}
catch(NumberFormatException e)
{
isAlarm = true;
}
counterResult.setString(String.valueOf(Math.tan(x)));
if (isAlarm == true)
{
display.setCurrent(alarmAlert);
isAlarm = false;
}
}
public void sqrt()
{
double x = 0;
try
{
x = Double.parseDouble(this.getOperationData());
}
catch(NumberFormatException e)
{
isAlarm = true;
}
counterResult.setString(String.valueOf(Math.sqrt(x)));
if (isAlarm == true)
{
display.setCurrent(alarmAlert);
isAlarm = false;
}
}
/**
*实现监视器
*/
public void commandAction( Command c, Displayable displayable)
{
if (c == backCommand)
{
this.display.setCurrent(this.mainList);
}
if (c == okCommand)
{
if (isArithmetic == true)
{
this.arithmetic();
}
if (isSin== true)
{
this.sin();
}
if(isCos == true)
{
this.cos();
}
if(isTan == true)
{
this.tan();
}
if(isSqrt == true)
{
this.sqrt();
}
}
}
/**
*实现Item改变的监视器
*/
public void itemStateChanged(Item item)
{
if(item == mathChoice)
{
if(mathChoice.isSelected(0) == true) //判断是否对对话框做出选择
{
isArithmetic = true;
isSin = false;
isCos = false;
isTan = false;
isSqrt = false;
}
if(mathChoice.isSelected(1) == true) //判断是否对对话框做出选择
{
isSin = true;
isArithmetic = false;
isCos = false;
isTan = false;
isSqrt = false;
}
if(mathChoice.isSelected(2) == true) //判断是否对对话框做出选择
{
isCos = true;
isArithmetic = false;
isSin = false;
isTan = false;
isSqrt = false;
}
if(mathChoice.isSelected(3) == true) //判断是否对对话框做出选择
{
isTan = true;
isArithmetic = false;
isSin = false;
isCos = false;
isSqrt = false;
}
if(mathChoice.isSelected(4) == true) //判断是否对对话框做出选择
{
isSqrt = true;
isArithmetic = false;
isSin = false;
isCos = false;
isTan = false;
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?