1. import javax.microedition.midlet.*;  

    2. import javax.microedition.lcdui.*;  




    1. /**
    2. * 该类是应用程序的主类,控制应用程序的生命周期。
    3. */ 
    4. publicclass CalcMIDlet extends MIDlet implements CommandListener {  
    5.     //  
    6.     private CalcForm calcForm;  
    7.     private Command cmdExit = new Command("退出", Command.EXIT, 1);  

    8.     publicvoid startApp() {  
    9.         Display display = Display.getDisplay(this);  
    10.         calcForm = new CalcForm();  
    11.         calcForm.addCommand(cmdExit);  
    12.         calcForm.setCommandListener(this);  
    13.         display.setCurrent(calcForm);  
    14.     }  

    15.     publicvoid pauseApp() {  
    16.         //  
    17.     }  

    18.     publicvoid destroyApp(boolean unconditional) {  
    19.         //  
    20.     }  

    21.     publicvoid commandAction(Command cmd, Displayable d) {  
    22.         if(cmd == cmdExit) {  
    23.             notifyDestroyed();  
    24.         }  
    25.     }  
    26. }  


  1. Java代码


    1. import javax.microedition.lcdui.*;  

    2. /**
    3. * 该类描述了计算器。
    4. * 实现了计算器的界面,及加、减、乘、除等计算功能。
    5. */ 
    6. publicclass CalcForm extends Form implements CalcKeyboardListener { //  
    7.     private CalcScreen showArea;    //计算器的显示区  
    8.     private CalcKeyboard ckeyboard; //计算器键盘  

    9.     privateboolean hasNewOperand = false;  //有新的操作数  
    10.     privateboolean numInputing = false;  
    11.     privatedouble acc = 0.0;  //累加器  
    12.     private String operator = "";    //运算符  
    13.     privatedouble operand = 0.0;   //操作数  

    14.     public CalcForm() {  
    15.         super("计算器");  
    16.         showArea = new CalcScreen();            //创建计算器的显示区对象  
    17.         ckeyboard = new CalcKeyboard(4, 5);     //创建计算器的键盘  
    18.         ckeyboard.setCalcKeyboardListener(this);    //  

    19.         //布局  
    20.         showArea.setLayout(Item.LAYOUT_2|Item.LAYOUT_CENTER|Item.LAYOUT_NEWLINE_AFTER);  
    21.         append(showArea);  
    22.         append(new Spacer(this.getWidth(), 5));  
    23.         ckeyboard.setLayout(Item.LAYOUT_2|Item.LAYOUT_CENTER);  
    24.         append(ckeyboard);  

    25.         reset();  
    26.     }  

    27.     //按钮单击事件处理方法  
    28.     //如果设备支持触摸屏功能,当用户使用笔在按钮上单击后,  
    29.     //注册在键盘上的监视器将调用下面的方法,对单击事件进行处理。  
    30.     publicvoid actionPerformmed(CalcKeyboard btn, String symbol) {  
    31.         if(symbol == CalcKeyboard.NUM_ZERO || symbol == CalcKeyboard.NUM_ONE || symbol == CalcKeyboard.NUM_TWO ||  
    32.             symbol == CalcKeyboard.NUM_THREE ||symbol == CalcKeyboard.NUM_FOUR ||symbol == CalcKeyboard.NUM_FIVE ||  
    33.             symbol == CalcKeyboard.NUM_SIX ||symbol == CalcKeyboard.NUM_SEVEN ||symbol == CalcKeyboard.NUM_EIGHT ||  
    34.             symbol == CalcKeyboard.NUM_NINE ) {  
    35.             //  
    36.             inputNum(symbol);  
    37.         }  
    38.         elseif(symbol == CalcKeyboard.SYMBOL_DOT   
    39.                 && showArea.getText().indexOf(CalcKeyboard.SYMBOL_DOT) == -1) {  
    40.             //  
    41.             inputNum(symbol);  
    42.         }  
    43.         elseif(symbol == CalcKeyboard.BACKSPACE) {  
    44.             String text = showArea.getText();  
    45.             if(text.length() > 0) {  
    46.                 text = text.substring(0, text.length()-1);  
    47.                 showArea.setText(text);  
    48.             }  
    49.         }  
    50.         elseif(symbol == CalcKeyboard.CE) {  
    51.             showArea.setText("0.");  
    52.         }  
    53.         elseif(symbol == CalcKeyboard.C) {  
    54.             //计算器归零  
    55.             reset();  
    56.         }  
    57.         elseif(symbol == CalcKeyboard.ADD || symbol == CalcKeyboard.MINUS ||   
    58.                     symbol == CalcKeyboard.MULT || symbol == CalcKeyboard.DIVIDE ||  
    59.                     symbol == CalcKeyboard.EQUALS) {  
    60.             //  
    61.             numInputing = false;  
    62.             String s = showArea.getText();  
    63.             double d = Double.parseDouble(s);  
    64.             jisuan(d, symbol);  
    65.             showArea.setText(String.valueOf(acc));  
    66.         }  
    67.         elseif(symbol == CalcKeyboard.SYMBOL_MINUS) {  
    68.             String str = showArea.getText();  
    69.             if(str.charAt(0) == '-') {  
    70.                 showArea.setText(str.substring(1, str.length()));  
    71.             }  
    72.             else {  
    73.                 showArea.setText("-" + str);  
    74.             }  
    75.         }  
    76.     }  

    77.     privatevoid jisuan(double exp, String oper) {  
    78.         if(operator.equals("")) {  
    79.             acc = exp;  
    80.             operand = exp;  
    81.         }  
    82.         else {  
    83.             if(hasNewOperand) { //新的操作数  
    84.                 operand = exp;  
    85.                 if(operator.equals(CalcKeyboard.ADD)) {  
    86.                     acc += operand;  
    87.                 }  
    88.                 elseif(operator.equals(CalcKeyboard.MINUS)) {  
    89.                     acc -= operand;  
    90.                 }  
    91.                 elseif(operator.equals(CalcKeyboard.MULT)) {  
    92.                     acc *= operand;  
    93.                 }  
    94.                 elseif(operator.equals(CalcKeyboard.DIVIDE)) {  
    95.                     acc /= operand;  
    96.                 }  

    97.             }  
    98.             else {  
    99.                 if(oper.equals(CalcKeyboard.EQUALS)) {  
    100.                     if(operator.equals(CalcKeyboard.ADD)) {  
    101.                         acc += operand;  
    102.                     }  
    103.                     elseif(operator.equals(CalcKeyboard.MINUS)) {  
    104.                         acc -= operand;  
    105.                     }  
    106.                     elseif(operator.equals(CalcKeyboard.MULT)) {  
    107.                         acc *= operand;  
    108.                     }  
    109.                     elseif(operator.equals(CalcKeyboard.DIVIDE)) {  
    110.                         acc /= operand;  
    111.                     }  
    112.                     if(!oper.equals(CalcKeyboard.EQUALS)) {  
    113.                         operator = oper;  
    114.                     }  
    115.                 }  
    116.             }  
    117.         }  
    118.         if(!oper.equals(CalcKeyboard.EQUALS)) {  
    119.             operator = oper;  
    120.         }  
    121.         hasNewOperand = false;  
    122.     }  

    123.     privatevoid reset() {  
    124.         hasNewOperand = false;  
    125.         numInputing = false;  
    126.         acc = 0.0;  
    127.         operator = "";  
    128.         showArea.setText("0.");  
    129.     }  

    130.     privatevoid inputNum(String str) {  
    131.         if(numInputing) {  
    132.             showArea.setText(showArea.getText() + str);  
    133.         }  
    134.         else {  
    135.             showArea.setText(str);  
    136.             numInputing = true;  
    137.         }  
    138.         hasNewOperand = true;  
    139.     }  
    140. }  


  2. Java代码


    1. import javax.microedition.lcdui.*;  

    2. /**
    3. * 该类描述了计算器键盘。提供了直观的图形用户界面,该类支持触摸屏功能。
    4. */ 
    5. publicclass CalcKeyboard extends CustomItem {  
    6.     publicstaticfinal String BACKSPACE = "<-";  
    7.     publicstaticfinal String CE = "CE";  
    8.     publicstaticfinal String C = "C";  
    9.     publicstaticfinal String SYMBOL_MINUS = "+/-";  
    10.     publicstaticfinal String NUM_ZERO = "0";  
    11.     publicstaticfinal String NUM_ONE = "1";  
    12.     publicstaticfinal String NUM_TWO = "2";  
    13.     publicstaticfinal String NUM_THREE = "3";  
    14.     publicstaticfinal String NUM_FOUR = "4";  
    15.     publicstaticfinal String NUM_FIVE = "5";  
    16.     publicstaticfinal String NUM_SIX = "6";  
    17.     publicstaticfinal String NUM_SEVEN = "7";  
    18.     publicstaticfinal String NUM_EIGHT = "8";  
    19.     publicstaticfinal String NUM_NINE = "9";  
    20.     publicstaticfinal String SYMBOL_DOT = ".";  
    21.     publicstaticfinal String ADD = "+";  
    22.     publicstaticfinal String MINUS = "-";  
    23.     publicstaticfinal String MULT = "*";  
    24.     publicstaticfinal String DIVIDE = "/";  
    25.     publicstaticfinal String EQUALS = "=";  

    26.     privatestaticfinalint PRESSED = 0;  
    27.     privatestaticfinalint RELEASED = 1;  

    28.     private CalcKeyboardListener ckListener;    //指针动作监视器  
    29.     private Font textFont;  
    30.     privateint col;    //列  
    31.     privateint row;    //行  
    32.     privateint btnWidth;   //按键宽  
    33.     privateint btnHeight;  //按键高  
    34.     privateint hSpace = 4; //按键水平间距  
    35.     privateint vSpace = 4; //按键垂直间距  

    36.     privateint keyState = RELEASED;  

    37.     private String[] keyLabel = {  
    38.         BACKSPACE, CE, C, SYMBOL_MINUS,   
    39.         NUM_SEVEN, NUM_EIGHT, NUM_NINE, DIVIDE,  
    40.         NUM_FOUR, NUM_FIVE, NUM_SIX, MULT,  
    41.         NUM_ONE, NUM_TWO, NUM_THREE, MINUS,  
    42.         NUM_ZERO, SYMBOL_DOT, EQUALS, ADD  
    43.     };  

    44.     public CalcKeyboard(int col, int row) {  
    45.         super(null);  
    46.         textFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);  
    47.         this.col = col;  
    48.         this.row = row;  
    49.         btnHeight = textFont.getHeight() + 4;  
    50.         btnWidth = btnHeight + 10;  
    51.     }  

    52.     protectedint getMinContentHeight() {  
    53.         return row * (btnHeight + vSpace) - vSpace;  
    54.     }  

    55.     protectedint getMinContentWidth() {  
    56.         return col * (btnWidth + hSpace) - hSpace;  
    57.     }  

    58.     protectedint getPrefContentHeight(int width) {  
    59.         return getMinContentHeight();  
    60.     }  

    61.     protectedint getPrefContentWidth(int height) {  
    62.         return getMinContentWidth();  
    63.     }  

    64.     protectedvoid paint(Graphics g, int w, int h) {  
    65.         for(int i=0; i<keyLabel.length; i++) {  
    66.             drawButton(g, keyLabel[i], i%col * (btnWidth+hSpace), i/col*(btnHeight+vSpace), btnWidth, btnHeight);  
    67.         }  
    68.     }  

    69.     privatevoid drawButton(Graphics g, String str, int x, int y, int w, int h) {  
    70.         g.setColor(160, 160, 255);  
    71.         g.drawRect(x, y, w-1, h-1);  
    72.         if(keyState == RELEASED) {  
    73.             g.setColor(240, 240, 255);  
    74.         }  
    75.         elseif(keyState == PRESSED) {  
    76.             g.setColor(210, 210, 255);  
    77.         }  
    78.         g.fillRect(x+2, y+2, w-4, h-4);  

    79.         g.setColor(0, 0, 0);  
    80.         g.setFont(textFont);  
    81.         g.drawString(str, x+w/2, y+h, Graphics.BOTTOM|Graphics.HCENTER);  
    82.     }  

    83.     privateint getIndex(int x, int y) {  
    84.         int j = x / (btnWidth+hSpace);  
    85.         int i = y / (btnHeight+vSpace);  

    86.         return (col*i)+j;  
    87.     }  

    88.     //指针事件处理方法  
    89.     //如果当前设备支持触摸屏功能,当用户使用笔在屏幕上按下时,  
    90.     //系统将调用该方法。  
    91.     protectedvoid pointerPressed(int x, int y) {  
    92.         keyState = PRESSED;  
    93.         int ax = x - x % (btnWidth+hSpace);  
    94.         int ay = y - y % (btnHeight+vSpace);  
    95.         repaint(ax, ay, btnWidth, btnHeight);  
    96.     }   

    97.     //指针事件处理方法  
    98.     //如果当前设备支持触摸屏功能,当用户使用笔在屏幕上按下,然后释放时,  
    99.     //系统将调用该方法。  
    100.     protectedvoid pointerReleased(int x, int y) {  
    101.         keyState = RELEASED;  
    102.         int ax = x - x % (btnWidth+hSpace);  
    103.         int ay = y - y % (btnHeight+vSpace);  
    104.         repaint(ax, ay, btnWidth, btnHeight);  
    105.         if(ckListener != null) {  
    106.             int index = getIndex(x, y);  
    107.             ckListener.actionPerformmed(this, keyLabel[index]);  
    108.         }  
    109.     }  

    110.     //为当前计算器键盘设置监视器  
    111.     publicvoid setCalcKeyboardListener(CalcKeyboardListener ckListener) {  
    112.         this.ckListener = ckListener;  
    113.     }  
    114. }  


    115. /**
    116. * 该接口描述了计算器键盘的监视器,定义了计算器键盘按钮单击动作
    117. * 的处理方法。
    118. */ 
    119. publicinterface CalcKeyboardListener {  
    120.     //指针单击动作的处理方法。  
    121.     //如果设备支持触摸屏功能,当用户使用笔单击屏幕上计算盘键盘上的按钮  
    122.     //时,监视该键盘的监视器将回调该方法,处理单击动作。  
    123.     //参数ck表示被监视的计算器键盘,symbol表示键盘上的按键。  
    124.     publicvoid actionPerformmed(CalcKeyboard ck, String symbol);  
    125. }  


  3. Java代码


    1. import javax.microedition.lcdui.*;  

    2. /**
    3. * 该类描述了计算器的显示区,用于显示输入的操作数及计算结果。
    4. */ 
    5. publicclass CalcScreen extends CustomItem {  
    6.     private String text;  
    7.     private Font showFont;  

    8.     public CalcScreen() {  
    9.         super(null);  
    10.         showFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);  
    11.         text = "";  
    12.     }  

    13.     protectedint getMinContentHeight() {  
    14.         return showFont.getHeight() + 4;  
    15.     }  

    16.     protectedint getMinContentWidth() {  
    17.         return showFont.stringWidth("012345678901234.-") + 4;  
    18.     }  

    19.     protectedint getPrefContentHeight(int width) {  
    20.         return getMinContentHeight();  
    21.     }  

    22.     protectedint getPrefContentWidth(int height) {  
    23.         return 150;  
    24.     }  

    25.     protectedvoid paint(Graphics g, int w, int h) {  
    26.         g.setColor(160, 160, 255);  
    27.         g.drawRect(0, 0, w-1, h-1);  
    28.         g.setColor(210, 210, 255);  
    29.         g.drawRect(2, 2, w-5, h-5);  

    30.         g.setColor(0, 0, 0);  
    31.         g.setFont(showFont);  
    32.         g.drawString(text, w-10, h-3, Graphics.BOTTOM|Graphics.RIGHT);  
    33.     }  

    34.     publicvoid setText(String text) {  
    35.         this.text = text;  
    36.         repaint();  
    37.     }  

    38.     public String getText() {  
    39.         return text;  
    40.     }  
    41. }