代码改变世界

JAVA计算器【源码】

2012-08-10 23:49  java线程例子  阅读(793)  评论(0编辑  收藏  举报
    1. import javax.microedition.midlet.*;  
    2.  
    3. import javax.microedition.lcdui.*;  
    1.    
    2. /**
    3. * 该类是应用程序的主类,控制应用程序的生命周期。
    4. */ 
    5. public class CalcMIDlet extends MIDlet implements CommandListener {  
    6.     //  
    7.     private CalcForm calcForm;  
    8.     private Command cmdExit = new Command("退出", Command.EXIT, 1);  
    9.       
    10.     public void 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.     public void pauseApp() {  
    19.         //  
    20.     }  
    21.       
    22.     public void destroyApp(boolean unconditional) {  
    23.         //  
    24.     }  
    25.       
    26.     public void 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. public class CalcForm extends Form implements CalcKeyboardListener { //  
    8.     private CalcScreen showArea;    //计算器的显示区  
    9.     private CalcKeyboard ckeyboard; //计算器键盘  
    10.       
    11.     private boolean hasNewOperand = false//有新的操作数  
    12.     private boolean numInputing = false;  
    13.     private double acc = 0.0//累加器  
    14.     private String operator = "";    //运算符  
    15.     private double 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.     public void 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.         else if(symbol == CalcKeyboard.SYMBOL_DOT   
    45.                 && showArea.getText().indexOf(CalcKeyboard.SYMBOL_DOT) == -1) {  
    46.             //  
    47.             inputNum(symbol);  
    48.         }  
    49.         else if(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.         else if(symbol == CalcKeyboard.CE) {  
    57.             showArea.setText("0.");  
    58.         }  
    59.         else if(symbol == CalcKeyboard.C) {  
    60.             //计算器归零  
    61.             reset();  
    62.         }  
    63.         else if(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.         else if(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.     private void 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.                 else if(operator.equals(CalcKeyboard.MINUS)) {  
    96.                     acc -= operand;  
    97.                 }  
    98.                 else if(operator.equals(CalcKeyboard.MULT)) {  
    99.                     acc *= operand;  
    100.                 }  
    101.                 else if(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.                     else if(operator.equals(CalcKeyboard.MINUS)) {  
    112.                         acc -= operand;  
    113.                     }  
    114.                     else if(operator.equals(CalcKeyboard.MULT)) {  
    115.                         acc *= operand;  
    116.                     }  
    117.                     else if(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.     private void reset() {  
    133.         hasNewOperand = false;  
    134.         numInputing = false;  
    135.         acc = 0.0;  
    136.         operator = "";  
    137.         showArea.setText("0.");  
    138.     }  
    139.       
    140.     private void 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. public class CalcKeyboard extends CustomItem {  
    7.     public static final String BACKSPACE = "<-";  
    8.     public static final String CE = "CE";  
    9.     public static final String C = "C";  
    10.     public static final String SYMBOL_MINUS = "+/-";  
    11.     public static final String NUM_ZERO = "0";  
    12.     public static final String NUM_ONE = "1";  
    13.     public static final String NUM_TWO = "2";  
    14.     public static final String NUM_THREE = "3";  
    15.     public static final String NUM_FOUR = "4";  
    16.     public static final String NUM_FIVE = "5";  
    17.     public static final String NUM_SIX = "6";  
    18.     public static final String NUM_SEVEN = "7";  
    19.     public static final String NUM_EIGHT = "8";  
    20.     public static final String NUM_NINE = "9";  
    21.     public static final String SYMBOL_DOT = ".";  
    22.     public static final String ADD = "+";  
    23.     public static final String MINUS = "-";  
    24.     public static final String MULT = "*";  
    25.     public static final String DIVIDE = "/";  
    26.     public static final String EQUALS = "=";  
    27.       
    28.     private static final int PRESSED = 0;  
    29.     private static final int RELEASED = 1;  
    30.       
    31.     private CalcKeyboardListener ckListener;    //指针动作监视器  
    32.     private Font textFont;  
    33.     private int col;    //列  
    34.     private int row;    //行  
    35.     private int btnWidth;   //按键宽  
    36.     private int btnHeight;  //按键高  
    37.     private int hSpace = 4; //按键水平间距  
    38.     private int vSpace = 4; //按键垂直间距  
    39.       
    40.     private int 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.     protected int getMinContentHeight() {  
    60.         return row * (btnHeight + vSpace) - vSpace;  
    61.     }  
    62.       
    63.     protected int getMinContentWidth() {  
    64.         return col * (btnWidth + hSpace) - hSpace;  
    65.     }  
    66.       
    67.     protected int getPrefContentHeight(int width) {  
    68.         return getMinContentHeight();  
    69.     }  
    70.       
    71.     protected int getPrefContentWidth(int height) {  
    72.         return getMinContentWidth();  
    73.     }  
    74.       
    75.     protected void 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.     private void 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.         else if(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.     private int 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.     protected void 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.     protected void 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.     public void setCalcKeyboardListener(CalcKeyboardListener ckListener) {  
    130.         this.ckListener = ckListener;  
    131.     }  
    132. }  
    133.    
    134.    
    135. /**
    136. * 该接口描述了计算器键盘的监视器,定义了计算器键盘按钮单击动作
    137. * 的处理方法。
    138. */ 
    139. public interface CalcKeyboardListener {  
    140.     //指针单击动作的处理方法。  
    141.     //如果设备支持触摸屏功能,当用户使用笔单击屏幕上计算盘键盘上的按钮  
    142.     //时,监视该键盘的监视器将回调该方法,处理单击动作。  
    143.     //参数ck表示被监视的计算器键盘,symbol表示键盘上的按键。  
    144.     public void actionPerformmed(CalcKeyboard ck, String symbol);  
    145. }  
    146.    
    Java代码
    1. import javax.microedition.lcdui.*;  
    2.    
    3. /**
    4. * 该类描述了计算器的显示区,用于显示输入的操作数及计算结果。
    5. */ 
    6. public class CalcScreen extends 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.     protected int getMinContentHeight() {  
    17.         return showFont.getHeight() + 4;  
    18.     }  
    19.       
    20.     protected int getMinContentWidth() {  
    21.         return showFont.stringWidth("012345678901234.-") + 4;  
    22.     }  
    23.       
    24.     protected int getPrefContentHeight(int width) {  
    25.         return getMinContentHeight();  
    26.     }  
    27.       
    28.     protected int getPrefContentWidth(int height) {  
    29.         return 150;  
    30.     }  
    31.       
    32.     protected void 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.     public void setText(String text) {  
    44.         this.text = text;  
    45.         repaint();  
    46.     }  
    47.       
    48.     public String getText() {  
    49.         return text;  
    50.     }  
    51. }