JAVA计算器【源码】

来源:互联网 发布:js数组slice 编辑:程序博客网 时间:2024/05/01 00:32
    1. import javax.microedition.midlet.*;  
    2.  
    3. import javax.microedition.lcdui.*;  
    1.    
    2. /**
    3. * 该类是应用程序的主类,控制应用程序的生命周期。
    4. */ 
    5. publicclass CalcMIDletextends MIDletimplements CommandListener {  
    6.     // 
    7.     private CalcForm calcForm;  
    8.     private Command cmdExit =new Command("退出", Command.EXIT,1);  
    9.       
    10.     publicvoid startApp() {  
    11.         Display display = Display.getDisplay(this);  
    12.         calcForm = new CalcForm();  
    13.         calcForm.addCommand(cmdExit);  
    14.         calcForm.setCommandListener(this);  
    15.         display.setCurrent(calcForm);  
    16.     }  
    17.       
    18.     publicvoid pauseApp() {  
    19.         //  
    20.     }  
    21.       
    22.     publicvoid destroyApp(boolean unconditional) {  
    23.         //  
    24.     }  
    25.       
    26.     publicvoid commandAction(Command cmd, Displayable d) {  
    27.         if(cmd == cmdExit) {  
    28.             notifyDestroyed();  
    29.         }  
    30.     }  
    31. }  
    32.    
    Java代码
    1. import javax.microedition.lcdui.*;  
    2.    
    3. /**
    4. * 该类描述了计算器。
    5. * 实现了计算器的界面,及加、减、乘、除等计算功能。
    6. */ 
    7. publicclass CalcFormextends Formimplements CalcKeyboardListener {//  
    8.     private CalcScreen showArea;   //计算器的显示区  
    9.     private CalcKeyboard ckeyboard;//计算器键盘  
    10.       
    11.     privateboolean hasNewOperand =false//有新的操作数  
    12.     privateboolean numInputing =false;  
    13.     privatedouble acc =0.0//累加器 
    14.     private String operator ="";   //运算符  
    15.     privatedouble operand =0.0;   //操作数 
    16.       
    17.     public CalcForm() {  
    18.         super("计算器");  
    19.         showArea = new CalcScreen();           //创建计算器的显示区对象  
    20.         ckeyboard = new CalcKeyboard(4,5);     //创建计算器的键盘 
    21.         ckeyboard.setCalcKeyboardListener(this);   //  
    22.           
    23.         //布局 
    24.         showArea.setLayout(Item.LAYOUT_2|Item.LAYOUT_CENTER|Item.LAYOUT_NEWLINE_AFTER);  
    25.         append(showArea);  
    26.         append(new Spacer(this.getWidth(),5));  
    27.         ckeyboard.setLayout(Item.LAYOUT_2|Item.LAYOUT_CENTER);  
    28.         append(ckeyboard);  
    29.           
    30.         reset();  
    31.     }  
    32.       
    33.     //按钮单击事件处理方法 
    34.     //如果设备支持触摸屏功能,当用户使用笔在按钮上单击后, 
    35.     //注册在键盘上的监视器将调用下面的方法,对单击事件进行处理。 
    36.     publicvoid actionPerformmed(CalcKeyboard btn, String symbol) {  
    37.         if(symbol == CalcKeyboard.NUM_ZERO || symbol == CalcKeyboard.NUM_ONE || symbol == CalcKeyboard.NUM_TWO ||  
    38.             symbol == CalcKeyboard.NUM_THREE ||symbol == CalcKeyboard.NUM_FOUR ||symbol == CalcKeyboard.NUM_FIVE ||  
    39.             symbol == CalcKeyboard.NUM_SIX ||symbol == CalcKeyboard.NUM_SEVEN ||symbol == CalcKeyboard.NUM_EIGHT ||  
    40.             symbol == CalcKeyboard.NUM_NINE ) {  
    41.             // 
    42.             inputNum(symbol);  
    43.         }  
    44.         elseif(symbol == CalcKeyboard.SYMBOL_DOT   
    45.                 && showArea.getText().indexOf(CalcKeyboard.SYMBOL_DOT) == -1) {  
    46.             // 
    47.             inputNum(symbol);  
    48.         }  
    49.         elseif(symbol == CalcKeyboard.BACKSPACE) {  
    50.             String text = showArea.getText();  
    51.             if(text.length() >0) {  
    52.                 text = text.substring(0, text.length()-1);  
    53.                 showArea.setText(text);  
    54.             }  
    55.         }  
    56.         elseif(symbol == CalcKeyboard.CE) {  
    57.             showArea.setText("0.");  
    58.         }  
    59.         elseif(symbol == CalcKeyboard.C) {  
    60.             //计算器归零 
    61.             reset();  
    62.         }  
    63.         elseif(symbol == CalcKeyboard.ADD || symbol == CalcKeyboard.MINUS ||   
    64.                     symbol == CalcKeyboard.MULT || symbol == CalcKeyboard.DIVIDE ||  
    65.                     symbol == CalcKeyboard.EQUALS) {  
    66.             // 
    67.             numInputing = false;  
    68.             String s = showArea.getText();  
    69.             double d = Double.parseDouble(s);  
    70.             jisuan(d, symbol);  
    71.             showArea.setText(String.valueOf(acc));  
    72.         }  
    73.         elseif(symbol == CalcKeyboard.SYMBOL_MINUS) {  
    74.             String str = showArea.getText();  
    75.             if(str.charAt(0) =='-') {  
    76.                 showArea.setText(str.substring(1, str.length()));  
    77.             }  
    78.             else {  
    79.                 showArea.setText("-" + str);  
    80.             }  
    81.         }  
    82.     }  
    83.       
    84.     privatevoid jisuan(double exp, String oper) {  
    85.         if(operator.equals("")) {  
    86.             acc = exp;  
    87.             operand = exp;  
    88.         }  
    89.         else {  
    90.             if(hasNewOperand) {//新的操作数  
    91.                 operand = exp;  
    92.                 if(operator.equals(CalcKeyboard.ADD)) {  
    93.                     acc += operand;  
    94.                 }  
    95.                 elseif(operator.equals(CalcKeyboard.MINUS)) {  
    96.                     acc -= operand;  
    97.                 }  
    98.                 elseif(operator.equals(CalcKeyboard.MULT)) {  
    99.                     acc *= operand;  
    100.                 }  
    101.                 elseif(operator.equals(CalcKeyboard.DIVIDE)) {  
    102.                     acc /= operand;  
    103.                 }  
    104.                   
    105.             }  
    106.             else {  
    107.                 if(oper.equals(CalcKeyboard.EQUALS)) {  
    108.                     if(operator.equals(CalcKeyboard.ADD)) {  
    109.                         acc += operand;  
    110.                     }  
    111.                     elseif(operator.equals(CalcKeyboard.MINUS)) {  
    112.                         acc -= operand;  
    113.                     }  
    114.                     elseif(operator.equals(CalcKeyboard.MULT)) {  
    115.                         acc *= operand;  
    116.                     }  
    117.                     elseif(operator.equals(CalcKeyboard.DIVIDE)) {  
    118.                         acc /= operand;  
    119.                     }  
    120.                     if(!oper.equals(CalcKeyboard.EQUALS)) {  
    121.                         operator = oper;  
    122.                     }  
    123.                 }  
    124.             }  
    125.         }  
    126.         if(!oper.equals(CalcKeyboard.EQUALS)) {  
    127.             operator = oper;  
    128.         }  
    129.         hasNewOperand = false;  
    130.     }  
    131.       
    132.     privatevoid reset() {  
    133.         hasNewOperand = false;  
    134.         numInputing = false;  
    135.         acc = 0.0;  
    136.         operator = "";  
    137.         showArea.setText("0.");  
    138.     }  
    139.       
    140.     privatevoid inputNum(String str) {  
    141.         if(numInputing) {  
    142.             showArea.setText(showArea.getText() + str);  
    143.         }  
    144.         else {  
    145.             showArea.setText(str);  
    146.             numInputing = true;  
    147.         }  
    148.         hasNewOperand = true;  
    149.     }  
    150. }  
    151.    
    Java代码
    1. import javax.microedition.lcdui.*;  
    2.    
    3. /**
    4. * 该类描述了计算器键盘。提供了直观的图形用户界面,该类支持触摸屏功能。
    5. */ 
    6. publicclass CalcKeyboardextends CustomItem {  
    7.     publicstaticfinal String BACKSPACE ="<-";  
    8.     publicstaticfinal String CE ="CE";  
    9.     publicstaticfinal String C ="C";  
    10.     publicstaticfinal String SYMBOL_MINUS ="+/-";  
    11.     publicstaticfinal String NUM_ZERO ="0";  
    12.     publicstaticfinal String NUM_ONE ="1";  
    13.     publicstaticfinal String NUM_TWO ="2";  
    14.     publicstaticfinal String NUM_THREE ="3";  
    15.     publicstaticfinal String NUM_FOUR ="4";  
    16.     publicstaticfinal String NUM_FIVE ="5";  
    17.     publicstaticfinal String NUM_SIX ="6";  
    18.     publicstaticfinal String NUM_SEVEN ="7";  
    19.     publicstaticfinal String NUM_EIGHT ="8";  
    20.     publicstaticfinal String NUM_NINE ="9";  
    21.     publicstaticfinal String SYMBOL_DOT =".";  
    22.     publicstaticfinal String ADD ="+";  
    23.     publicstaticfinal String MINUS ="-";  
    24.     publicstaticfinal String MULT ="*";  
    25.     publicstaticfinal String DIVIDE ="/";  
    26.     publicstaticfinal String EQUALS ="=";  
    27.       
    28.     privatestaticfinalint PRESSED =0;  
    29.     privatestaticfinalint RELEASED =1;  
    30.       
    31.     private CalcKeyboardListener ckListener;   //指针动作监视器  
    32.     private Font textFont;  
    33.     privateint col;   //列  
    34.     privateint row;   //行  
    35.     privateint btnWidth;  //按键宽  
    36.     privateint btnHeight; //按键高  
    37.     privateint hSpace =4; //按键水平间距 
    38.     privateint vSpace =4; //按键垂直间距 
    39.       
    40.     privateint keyState = RELEASED;  
    41.       
    42.     private String[] keyLabel = {  
    43.         BACKSPACE, CE, C, SYMBOL_MINUS,   
    44.         NUM_SEVEN, NUM_EIGHT, NUM_NINE, DIVIDE,  
    45.         NUM_FOUR, NUM_FIVE, NUM_SIX, MULT,  
    46.         NUM_ONE, NUM_TWO, NUM_THREE, MINUS,  
    47.         NUM_ZERO, SYMBOL_DOT, EQUALS, ADD  
    48.     };  
    49.       
    50.     public CalcKeyboard(int col,int row) {  
    51.         super(null);  
    52.         textFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);  
    53.         this.col = col;  
    54.         this.row = row;  
    55.         btnHeight = textFont.getHeight() + 4;  
    56.         btnWidth = btnHeight + 10;  
    57.     }  
    58.       
    59.     protectedint getMinContentHeight() {  
    60.         return row * (btnHeight + vSpace) - vSpace;  
    61.     }  
    62.       
    63.     protectedint getMinContentWidth() {  
    64.         return col * (btnWidth + hSpace) - hSpace;  
    65.     }  
    66.       
    67.     protectedint getPrefContentHeight(int width) {  
    68.         return getMinContentHeight();  
    69.     }  
    70.       
    71.     protectedint getPrefContentWidth(int height) {  
    72.         return getMinContentWidth();  
    73.     }  
    74.       
    75.     protectedvoid paint(Graphics g,int w,int h) {  
    76.         for(int i=0; i<keyLabel.length; i++) {  
    77.             drawButton(g, keyLabel[i], i%col * (btnWidth+hSpace), i/col*(btnHeight+vSpace), btnWidth, btnHeight);  
    78.         }  
    79.     }  
    80.       
    81.     privatevoid drawButton(Graphics g, String str,int x,int y,int w,int h) {  
    82.         g.setColor(160, 160, 255);  
    83.         g.drawRect(x, y, w-1, h-1);  
    84.         if(keyState == RELEASED) {  
    85.             g.setColor(240,240, 255);  
    86.         }  
    87.         elseif(keyState == PRESSED) {  
    88.             g.setColor(210, 210, 255);  
    89.         }  
    90.         g.fillRect(x+2, y+2, w-4, h-4);  
    91.           
    92.         g.setColor(0, 0,0);  
    93.         g.setFont(textFont);  
    94.         g.drawString(str, x+w/2, y+h, Graphics.BOTTOM|Graphics.HCENTER);  
    95.     }  
    96.       
    97.     privateint getIndex(int x,int y) {  
    98.         int j = x / (btnWidth+hSpace);  
    99.         int i = y / (btnHeight+vSpace);  
    100.           
    101.         return (col*i)+j;  
    102.     }  
    103.       
    104.     //指针事件处理方法 
    105.     //如果当前设备支持触摸屏功能,当用户使用笔在屏幕上按下时, 
    106.     //系统将调用该方法。 
    107.     protectedvoid pointerPressed(int x,int y) {  
    108.         keyState = PRESSED;  
    109.         int ax = x - x % (btnWidth+hSpace);  
    110.         int ay = y - y % (btnHeight+vSpace);  
    111.         repaint(ax, ay, btnWidth, btnHeight);  
    112.     }   
    113.       
    114.     //指针事件处理方法 
    115.     //如果当前设备支持触摸屏功能,当用户使用笔在屏幕上按下,然后释放时, 
    116.     //系统将调用该方法。 
    117.     protectedvoid pointerReleased(int x,int y) {  
    118.         keyState = RELEASED;  
    119.         int ax = x - x % (btnWidth+hSpace);  
    120.         int ay = y - y % (btnHeight+vSpace);  
    121.         repaint(ax, ay, btnWidth, btnHeight);  
    122.         if(ckListener !=null) {  
    123.             int index = getIndex(x, y);  
    124.             ckListener.actionPerformmed(this, keyLabel[index]);  
    125.         }  
    126.     }  
    127.       
    128.     //为当前计算器键盘设置监视器 
    129.     publicvoid setCalcKeyboardListener(CalcKeyboardListener ckListener) {  
    130.         this.ckListener = ckListener;  
    131.     }  
    132. }  
    133.    
    134.    
    135. /**
    136. * 该接口描述了计算器键盘的监视器,定义了计算器键盘按钮单击动作
    137. * 的处理方法。
    138. */ 
    139. publicinterface CalcKeyboardListener {  
    140.     //指针单击动作的处理方法。 
    141.     //如果设备支持触摸屏功能,当用户使用笔单击屏幕上计算盘键盘上的按钮 
    142.     //时,监视该键盘的监视器将回调该方法,处理单击动作。 
    143.     //参数ck表示被监视的计算器键盘,symbol表示键盘上的按键。 
    144.     publicvoid actionPerformmed(CalcKeyboard ck, String symbol);  
    145. }  
    146.    
    Java代码
    1. import javax.microedition.lcdui.*;  
    2.    
    3. /**
    4. * 该类描述了计算器的显示区,用于显示输入的操作数及计算结果。
    5. */ 
    6. publicclass CalcScreenextends CustomItem {  
    7.     private String text;  
    8.     private Font showFont;  
    9.       
    10.     public CalcScreen() {  
    11.         super(null);  
    12.         showFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);  
    13.         text = "";  
    14.     }  
    15.       
    16.     protectedint getMinContentHeight() {  
    17.         return showFont.getHeight() +4;  
    18.     }  
    19.       
    20.     protectedint getMinContentWidth() {  
    21.         return showFont.stringWidth("012345678901234.-") +4;  
    22.     }  
    23.       
    24.     protectedint getPrefContentHeight(int width) {  
    25.         return getMinContentHeight();  
    26.     }  
    27.       
    28.     protectedint getPrefContentWidth(int height) {  
    29.         return150;  
    30.     }  
    31.       
    32.     protectedvoid paint(Graphics g,int w,int h) {  
    33.         g.setColor(160,160, 255);  
    34.         g.drawRect(0, 0, w-1, h-1);  
    35.         g.setColor(210,210, 255);  
    36.         g.drawRect(2, 2, w-5, h-5);  
    37.           
    38.         g.setColor(0, 0,0);  
    39.         g.setFont(showFont);  
    40.         g.drawString(text, w-10, h-3, Graphics.BOTTOM|Graphics.RIGHT);  
    41.     }  
    42.       
    43.     publicvoid setText(String text) {  
    44.         this.text = text;  
    45.         repaint();  
    46.     }  
    47.       
    48.     public String getText() {  
    49.         return text;  
    50.     }  
    51. }
原创粉丝点击