`
piperzero
  • 浏览: 3481795 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

JAVA计算器【源码】

 
阅读更多
    1. import javax.microedition.midlet.*;
    2. import javax.microedition.lcdui.*;
    1. /**
    2. * 该类是应用程序的主类,控制应用程序的生命周期。
    3. */
    4. public class CalcMIDlet extends MIDlet implements CommandListener {
    5. //
    6. private CalcForm calcForm;
    7. private Command cmdExit = new Command("退出", Command.EXIT, 1);
    8. public void 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. public void pauseApp() {
    16. //
    17. }
    18. public void destroyApp(boolean unconditional) {
    19. //
    20. }
    21. public void commandAction(Command cmd, Displayable d) {
    22. if(cmd == cmdExit) {
    23. notifyDestroyed();
    24. }
    25. }
    26. }
    Java代码
    1. import javax.microedition.lcdui.*;
    2. /**
    3. * 该类描述了计算器。
    4. * 实现了计算器的界面,及加、减、乘、除等计算功能。
    5. */
    6. public class CalcForm extends Form implements CalcKeyboardListener { //
    7. private CalcScreen showArea; //计算器的显示区
    8. private CalcKeyboard ckeyboard; //计算器键盘
    9. private boolean hasNewOperand = false; //有新的操作数
    10. private boolean numInputing = false;
    11. private double acc = 0.0; //累加器
    12. private String operator = ""; //运算符
    13. private double 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. public void 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. else if(symbol == CalcKeyboard.SYMBOL_DOT
    39. && showArea.getText().indexOf(CalcKeyboard.SYMBOL_DOT) == -1) {
    40. //
    41. inputNum(symbol);
    42. }
    43. else if(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. else if(symbol == CalcKeyboard.CE) {
    51. showArea.setText("0.");
    52. }
    53. else if(symbol == CalcKeyboard.C) {
    54. //计算器归零
    55. reset();
    56. }
    57. else if(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. else if(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. private void 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. else if(operator.equals(CalcKeyboard.MINUS)) {
    89. acc -= operand;
    90. }
    91. else if(operator.equals(CalcKeyboard.MULT)) {
    92. acc *= operand;
    93. }
    94. else if(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. else if(operator.equals(CalcKeyboard.MINUS)) {
    104. acc -= operand;
    105. }
    106. else if(operator.equals(CalcKeyboard.MULT)) {
    107. acc *= operand;
    108. }
    109. else if(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. private void reset() {
    124. hasNewOperand = false;
    125. numInputing = false;
    126. acc = 0.0;
    127. operator = "";
    128. showArea.setText("0.");
    129. }
    130. private void 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. }
    Java代码
    1. import javax.microedition.lcdui.*;
    2. /**
    3. * 该类描述了计算器键盘。提供了直观的图形用户界面,该类支持触摸屏功能。
    4. */
    5. public class CalcKeyboard extends CustomItem {
    6. public static final String BACKSPACE = "<-";
    7. public static final String CE = "CE";
    8. public static final String C = "C";
    9. public static final String SYMBOL_MINUS = "+/-";
    10. public static final String NUM_ZERO = "0";
    11. public static final String NUM_ONE = "1";
    12. public static final String NUM_TWO = "2";
    13. public static final String NUM_THREE = "3";
    14. public static final String NUM_FOUR = "4";
    15. public static final String NUM_FIVE = "5";
    16. public static final String NUM_SIX = "6";
    17. public static final String NUM_SEVEN = "7";
    18. public static final String NUM_EIGHT = "8";
    19. public static final String NUM_NINE = "9";
    20. public static final String SYMBOL_DOT = ".";
    21. public static final String ADD = "+";
    22. public static final String MINUS = "-";
    23. public static final String MULT = "*";
    24. public static final String DIVIDE = "/";
    25. public static final String EQUALS = "=";
    26. private static final int PRESSED = 0;
    27. private static final int RELEASED = 1;
    28. private CalcKeyboardListener ckListener; //指针动作监视器
    29. private Font textFont;
    30. private int col; //列
    31. private int row; //行
    32. private int btnWidth; //按键宽
    33. private int btnHeight; //按键高
    34. private int hSpace = 4; //按键水平间距
    35. private int vSpace = 4; //按键垂直间距
    36. private int 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. protected int getMinContentHeight() {
    53. return row * (btnHeight + vSpace) - vSpace;
    54. }
    55. protected int getMinContentWidth() {
    56. return col * (btnWidth + hSpace) - hSpace;
    57. }
    58. protected int getPrefContentHeight(int width) {
    59. return getMinContentHeight();
    60. }
    61. protected int getPrefContentWidth(int height) {
    62. return getMinContentWidth();
    63. }
    64. protected void 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. private void 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. else if(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. private int 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. protected void 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. protected void 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. public void setCalcKeyboardListener(CalcKeyboardListener ckListener) {
    112. this.ckListener = ckListener;
    113. }
    114. }
    115. /**
    116. * 该接口描述了计算器键盘的监视器,定义了计算器键盘按钮单击动作
    117. * 的处理方法。
    118. */
    119. public interface CalcKeyboardListener {
    120. //指针单击动作的处理方法。
    121. //如果设备支持触摸屏功能,当用户使用笔单击屏幕上计算盘键盘上的按钮
    122. //时,监视该键盘的监视器将回调该方法,处理单击动作。
    123. //参数ck表示被监视的计算器键盘,symbol表示键盘上的按键。
    124. public void actionPerformmed(CalcKeyboard ck, String symbol);
    125. }
    Java代码
    1. import javax.microedition.lcdui.*;
    2. /**
    3. * 该类描述了计算器的显示区,用于显示输入的操作数及计算结果。
    4. */
    5. public class 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. protected int getMinContentHeight() {
    14. return showFont.getHeight() + 4;
    15. }
    16. protected int getMinContentWidth() {
    17. return showFont.stringWidth("012345678901234.-") + 4;
    18. }
    19. protected int getPrefContentHeight(int width) {
    20. return getMinContentHeight();
    21. }
    22. protected int getPrefContentWidth(int height) {
    23. return 150;
    24. }
    25. protected void 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. public void setText(String text) {
    35. this.text = text;
    36. repaint();
    37. }
    38. public String getText() {
    39. return text;
    40. }
    41. }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics