- import javax.microedition.midlet.*;
- import javax.microedition.lcdui.*;
- /**
- * 该类是应用程序的主类,控制应用程序的生命周期。
- */
- public class CalcMIDlet extends MIDlet implements CommandListener {
- //
- private CalcForm calcForm;
- private Command cmdExit = new Command("退出", Command.EXIT, 1);
- public void startApp() {
- Display display = Display.getDisplay(this);
- calcForm = new CalcForm();
- calcForm.addCommand(cmdExit);
- calcForm.setCommandListener(this);
- display.setCurrent(calcForm);
- }
- public void pauseApp() {
- //
- }
- public void destroyApp(boolean unconditional) {
- //
- }
- public void commandAction(Command cmd, Displayable d) {
- if(cmd == cmdExit) {
- notifyDestroyed();
- }
- }
- }
Java代码
- import javax.microedition.lcdui.*;
- /**
- * 该类描述了计算器。
- * 实现了计算器的界面,及加、减、乘、除等计算功能。
- */
- public class CalcForm extends Form implements CalcKeyboardListener { //
- private CalcScreen showArea; //计算器的显示区
- private CalcKeyboard ckeyboard; //计算器键盘
- private boolean hasNewOperand = false; //有新的操作数
- private boolean numInputing = false;
- private double acc = 0.0; //累加器
- private String operator = ""; //运算符
- private double operand = 0.0; //操作数
- public CalcForm() {
- super("计算器");
- showArea = new CalcScreen(); //创建计算器的显示区对象
- ckeyboard = new CalcKeyboard(4, 5); //创建计算器的键盘
- ckeyboard.setCalcKeyboardListener(this); //
- //布局
- showArea.setLayout(Item.LAYOUT_2|Item.LAYOUT_CENTER|Item.LAYOUT_NEWLINE_AFTER);
- append(showArea);
- append(new Spacer(this.getWidth(), 5));
- ckeyboard.setLayout(Item.LAYOUT_2|Item.LAYOUT_CENTER);
- append(ckeyboard);
- reset();
- }
- //按钮单击事件处理方法
- //如果设备支持触摸屏功能,当用户使用笔在按钮上单击后,
- //注册在键盘上的监视器将调用下面的方法,对单击事件进行处理。
- public void actionPerformmed(CalcKeyboard btn, String symbol) {
- if(symbol == CalcKeyboard.NUM_ZERO || symbol == CalcKeyboard.NUM_ONE || symbol == CalcKeyboard.NUM_TWO ||
- symbol == CalcKeyboard.NUM_THREE ||symbol == CalcKeyboard.NUM_FOUR ||symbol == CalcKeyboard.NUM_FIVE ||
- symbol == CalcKeyboard.NUM_SIX ||symbol == CalcKeyboard.NUM_SEVEN ||symbol == CalcKeyboard.NUM_EIGHT ||
- symbol == CalcKeyboard.NUM_NINE ) {
- //
- inputNum(symbol);
- }
- else if(symbol == CalcKeyboard.SYMBOL_DOT
- && showArea.getText().indexOf(CalcKeyboard.SYMBOL_DOT) == -1) {
- //
- inputNum(symbol);
- }
- else if(symbol == CalcKeyboard.BACKSPACE) {
- String text = showArea.getText();
- if(text.length() > 0) {
- text = text.substring(0, text.length()-1);
- showArea.setText(text);
- }
- }
- else if(symbol == CalcKeyboard.CE) {
- showArea.setText("0.");
- }
- else if(symbol == CalcKeyboard.C) {
- //计算器归零
- reset();
- }
- else if(symbol == CalcKeyboard.ADD || symbol == CalcKeyboard.MINUS ||
- symbol == CalcKeyboard.MULT || symbol == CalcKeyboard.DIVIDE ||
- symbol == CalcKeyboard.EQUALS) {
- //
- numInputing = false;
- String s = showArea.getText();
- double d = Double.parseDouble(s);
- jisuan(d, symbol);
- showArea.setText(String.valueOf(acc));
- }
- else if(symbol == CalcKeyboard.SYMBOL_MINUS) {
- String str = showArea.getText();
- if(str.charAt(0) == '-') {
- showArea.setText(str.substring(1, str.length()));
- }
- else {
- showArea.setText("-" + str);
- }
- }
- }
- private void jisuan(double exp, String oper) {
- if(operator.equals("")) {
- acc = exp;
- operand = exp;
- }
- else {
- if(hasNewOperand) { //新的操作数
- operand = exp;
- if(operator.equals(CalcKeyboard.ADD)) {
- acc += operand;
- }
- else if(operator.equals(CalcKeyboard.MINUS)) {
- acc -= operand;
- }
- else if(operator.equals(CalcKeyboard.MULT)) {
- acc *= operand;
- }
- else if(operator.equals(CalcKeyboard.DIVIDE)) {
- acc /= operand;
- }
- }
- else {
- if(oper.equals(CalcKeyboard.EQUALS)) {
- if(operator.equals(CalcKeyboard.ADD)) {
- acc += operand;
- }
- else if(operator.equals(CalcKeyboard.MINUS)) {
- acc -= operand;
- }
- else if(operator.equals(CalcKeyboard.MULT)) {
- acc *= operand;
- }
- else if(operator.equals(CalcKeyboard.DIVIDE)) {
- acc /= operand;
- }
- if(!oper.equals(CalcKeyboard.EQUALS)) {
- operator = oper;
- }
- }
- }
- }
- if(!oper.equals(CalcKeyboard.EQUALS)) {
- operator = oper;
- }
- hasNewOperand = false;
- }
- private void reset() {
- hasNewOperand = false;
- numInputing = false;
- acc = 0.0;
- operator = "";
- showArea.setText("0.");
- }
- private void inputNum(String str) {
- if(numInputing) {
- showArea.setText(showArea.getText() + str);
- }
- else {
- showArea.setText(str);
- numInputing = true;
- }
- hasNewOperand = true;
- }
- }
Java代码
- import javax.microedition.lcdui.*;
- /**
- * 该类描述了计算器键盘。提供了直观的图形用户界面,该类支持触摸屏功能。
- */
- public class CalcKeyboard extends CustomItem {
- public static final String BACKSPACE = "<-";
- public static final String CE = "CE";
- public static final String C = "C";
- public static final String SYMBOL_MINUS = "+/-";
- public static final String NUM_ZERO = "0";
- public static final String NUM_ONE = "1";
- public static final String NUM_TWO = "2";
- public static final String NUM_THREE = "3";
- public static final String NUM_FOUR = "4";
- public static final String NUM_FIVE = "5";
- public static final String NUM_SIX = "6";
- public static final String NUM_SEVEN = "7";
- public static final String NUM_EIGHT = "8";
- public static final String NUM_NINE = "9";
- public static final String SYMBOL_DOT = ".";
- public static final String ADD = "+";
- public static final String MINUS = "-";
- public static final String MULT = "*";
- public static final String DIVIDE = "/";
- public static final String EQUALS = "=";
- private static final int PRESSED = 0;
- private static final int RELEASED = 1;
- private CalcKeyboardListener ckListener; //指针动作监视器
- private Font textFont;
- private int col; //列
- private int row; //行
- private int btnWidth; //按键宽
- private int btnHeight; //按键高
- private int hSpace = 4; //按键水平间距
- private int vSpace = 4; //按键垂直间距
- private int keyState = RELEASED;
- private String[] keyLabel = {
- BACKSPACE, CE, C, SYMBOL_MINUS,
- NUM_SEVEN, NUM_EIGHT, NUM_NINE, DIVIDE,
- NUM_FOUR, NUM_FIVE, NUM_SIX, MULT,
- NUM_ONE, NUM_TWO, NUM_THREE, MINUS,
- NUM_ZERO, SYMBOL_DOT, EQUALS, ADD
- };
- public CalcKeyboard(int col, int row) {
- super(null);
- textFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
- this.col = col;
- this.row = row;
- btnHeight = textFont.getHeight() + 4;
- btnWidth = btnHeight + 10;
- }
- protected int getMinContentHeight() {
- return row * (btnHeight + vSpace) - vSpace;
- }
- protected int getMinContentWidth() {
- return col * (btnWidth + hSpace) - hSpace;
- }
- protected int getPrefContentHeight(int width) {
- return getMinContentHeight();
- }
- protected int getPrefContentWidth(int height) {
- return getMinContentWidth();
- }
- protected void paint(Graphics g, int w, int h) {
- for(int i=0; i<keyLabel.length; i++) {
- drawButton(g, keyLabel[i], i%col * (btnWidth+hSpace), i/col*(btnHeight+vSpace), btnWidth, btnHeight);
- }
- }
- private void drawButton(Graphics g, String str, int x, int y, int w, int h) {
- g.setColor(160, 160, 255);
- g.drawRect(x, y, w-1, h-1);
- if(keyState == RELEASED) {
- g.setColor(240, 240, 255);
- }
- else if(keyState == PRESSED) {
- g.setColor(210, 210, 255);
- }
- g.fillRect(x+2, y+2, w-4, h-4);
- g.setColor(0, 0, 0);
- g.setFont(textFont);
- g.drawString(str, x+w/2, y+h, Graphics.BOTTOM|Graphics.HCENTER);
- }
- private int getIndex(int x, int y) {
- int j = x / (btnWidth+hSpace);
- int i = y / (btnHeight+vSpace);
- return (col*i)+j;
- }
- //指针事件处理方法
- //如果当前设备支持触摸屏功能,当用户使用笔在屏幕上按下时,
- //系统将调用该方法。
- protected void pointerPressed(int x, int y) {
- keyState = PRESSED;
- int ax = x - x % (btnWidth+hSpace);
- int ay = y - y % (btnHeight+vSpace);
- repaint(ax, ay, btnWidth, btnHeight);
- }
- //指针事件处理方法
- //如果当前设备支持触摸屏功能,当用户使用笔在屏幕上按下,然后释放时,
- //系统将调用该方法。
- protected void pointerReleased(int x, int y) {
- keyState = RELEASED;
- int ax = x - x % (btnWidth+hSpace);
- int ay = y - y % (btnHeight+vSpace);
- repaint(ax, ay, btnWidth, btnHeight);
- if(ckListener != null) {
- int index = getIndex(x, y);
- ckListener.actionPerformmed(this, keyLabel[index]);
- }
- }
- //为当前计算器键盘设置监视器
- public void setCalcKeyboardListener(CalcKeyboardListener ckListener) {
- this.ckListener = ckListener;
- }
- }
- /**
- * 该接口描述了计算器键盘的监视器,定义了计算器键盘按钮单击动作
- * 的处理方法。
- */
- public interface CalcKeyboardListener {
- //指针单击动作的处理方法。
- //如果设备支持触摸屏功能,当用户使用笔单击屏幕上计算盘键盘上的按钮
- //时,监视该键盘的监视器将回调该方法,处理单击动作。
- //参数ck表示被监视的计算器键盘,symbol表示键盘上的按键。
- public void actionPerformmed(CalcKeyboard ck, String symbol);
- }
Java代码
- import javax.microedition.lcdui.*;
- /**
- * 该类描述了计算器的显示区,用于显示输入的操作数及计算结果。
- */
- public class CalcScreen extends CustomItem {
- private String text;
- private Font showFont;
- public CalcScreen() {
- super(null);
- showFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
- text = "";
- }
- protected int getMinContentHeight() {
- return showFont.getHeight() + 4;
- }
- protected int getMinContentWidth() {
- return showFont.stringWidth("012345678901234.-") + 4;
- }
- protected int getPrefContentHeight(int width) {
- return getMinContentHeight();
- }
- protected int getPrefContentWidth(int height) {
- return 150;
- }
- protected void paint(Graphics g, int w, int h) {
- g.setColor(160, 160, 255);
- g.drawRect(0, 0, w-1, h-1);
- g.setColor(210, 210, 255);
- g.drawRect(2, 2, w-5, h-5);
- g.setColor(0, 0, 0);
- g.setFont(showFont);
- g.drawString(text, w-10, h-3, Graphics.BOTTOM|Graphics.RIGHT);
- }
- public void setText(String text) {
- this.text = text;
- repaint();
- }
- public String getText() {
- return text;
- }
- }