当前位置:   article > 正文

java 简易计算器_java计算器

java计算器

要求:

1.使用Java图形界面组件设计软件,界面如图所示。

2.软件能够满足基本的“加、减、乘、除”等运算要求。

3.程序代码清晰,语法规范,结构合理,逻辑正确。

效果图:

2b9091ad8bc94af0b2f3af902ac72003.png

先分析,计算器大概是由三个大部分组成的:菜单栏,显示框,按钮。

所以定义一个类cal继承JFrame。

  1. class cal extends JFrame {
  2. private JPanel p1, p2;
  3. private JTextArea show;
  4. private String box ;
  5. JMenuBar menubar;//菜单
  6. JMenu menu1, menu2, menu3;//菜单
  7. StringBuffer strA;//用来存放用户输入的第一个数字
  8. StringBuffer strB;//用来存放用户输入的第二个数字
  9. char oper ='~';//初始化操作符,可以随便初始化一个特殊符号,这里只是用来区分的
  10. double A;
  11. double B;
  12. private String[] text2 = {"C", "CE","%", "/",
  13. "7", "8", "9", "*",
  14. "4", "5", "6", "-",
  15. "1", "2", "3", "+",
  16. "DEL","0", ".", "="};//计算器按钮面板
  17. private JButton[] munButton = new JButton[text2.length];
  18. }

我们定义完后开始初始化。

  1. class cal extends JFrame implements ActionListener {
  2. private JPanel p1, p2;
  3. private JTextArea show;
  4. private String box ;
  5. JMenuBar menubar;
  6. JMenu menu1, menu2, menu3;
  7. StringBuffer strA;
  8. StringBuffer strB;
  9. char oper ='~';
  10. double A;
  11. double B;
  12. private String[] text2 = {"C", "CE","%", "/",
  13. "7", "8", "9", "*",
  14. "4", "5", "6", "-",
  15. "1", "2", "3", "+",
  16. "DEL","0", ".", "="};//计算器按钮面板
  17. private JButton[] munButton = new JButton[text2.length];
  18. public cal() {
  19. p1 = new JPanel();
  20. p2 = new JPanel();
  21. show = new JTextArea();
  22. p1.setSize(600, 100);
  23. menubar = new JMenuBar();
  24. menu1 = new JMenu("查看(V)");
  25. menu2 = new JMenu("编辑(E)");
  26. menu3 = new JMenu("帮助(H)");
  27. strB=new StringBuffer();
  28. strA=new StringBuffer();
  29. }
  30. public void init() {//初始化
  31. this.setTitle("计算器");//设置名称
  32. this.setBounds(200, 200, 320, 300);
  33. this.setLayout(new BorderLayout());//设置布局
  34. this.add(p1, BorderLayout.CENTER);
  35. this.add(p2, BorderLayout.SOUTH);
  36. menubar.add(menu1);
  37. menubar.add(menu2);
  38. menubar.add(menu3);
  39. this.setJMenuBar(menubar);
  40. this.setLocationRelativeTo(null);//放置在屏幕中央
  41. this.setResizable(false);//固定大小,用户不能调整大小
  42. show.setPreferredSize(new Dimension(300, 100));
  43. p1.add(show);
  44. p2.setLayout(new GridLayout(5, 4, 2, 3));
  45. //添加数字按键
  46. for (int i = 0; i < text2.length; i++) {
  47. munButton[i] = new JButton(text2[i] + " ");
  48. p2.add(munButton[i]);
  49. }
  50. for (int i = 0; i < munButton.length; i++)
  51. munButton[i].addActionListener(this);
  52. this.setVisible(true);//窗体可视化
  53. this.addWindowListener(new WindowAdapter() {//监听事件,当按下关闭按钮时,结束程序
  54. @Override
  55. public void windowClosing(WindowEvent e) {
  56. System.exit(0);
  57. }
  58. });
  59. }

接着我们就开始进入到这个项目的最重要的部分了。

设置按钮监听事件,通过获取按钮的信息来进行判断运算。

在我们进行加减乘除计算的时候会出现一个特殊情况,除数为0,所以我们要预防出现异常影响程序的运行,我们就要进行异常的捕获处理,这里我是自定义了一个异常类munber_Exception,然后我们利用try{}catch{}语句来进行异常捕获和处理。

  1. public double division(double x,double y)throws munber_Exception{
  2. if(y==0){
  3. throw new munber_Exception("除数不能为0!");
  4. }
  5. else{
  6. return x/y;
  7. }
  8. }
  9. @Override
  10. public void actionPerformed(ActionEvent e) {
  11. try { String act=e.getActionCommand();//这个方法返回的是事件源组件的“命令” , 这个“命令” 实际上就是事件源组件上的“Label(标签)字符串”,即如果我按了“9”这个按钮他就会返回一个“9的值”
  12. char a=act.charAt(0);//取act这个字符串的首字符
  13. if (a=='0' ||a=='1' || a=='2' ||a=='3'||a=='4'||a=='5'||a=='6'||a=='7'||a=='8'||a=='9'||a=='.') {
  14. if(oper=='~'){//当oper=='~'时,则操作符为空
  15. strA.append(a);
  16. show.setText(String.valueOf(strA));
  17. }else {
  18. strB.append(a);
  19. show.setText(String.valueOf(strA)+oper+String.valueOf(strB));
  20. }
  21. }
  22. else if(a=='+'||a=='-'||a=='/'||a=='*'||a=='%'){
  23. oper=a;
  24. show.setText(String.valueOf(strA)+oper);
  25. }
  26. else if(a=='='){
  27. A = Double.parseDouble(String.valueOf(strA));
  28. B = Double.parseDouble(String.valueOf(strB));
  29. double j;
  30. int len1=strA.length();
  31. int len2=strB.length();
  32. if (oper == '+') {
  33. j = A + B;
  34. show.setText(Double.toString(j));
  35. strA.delete(0,len1);
  36. strB.delete(0,len2);
  37. strA.append(j);
  38. } else if (oper == '-') {
  39. j = A - B;
  40. show.setText(Double.toString(j));
  41. strA.delete(0,len1);
  42. strB.delete(0,len2);
  43. strA.append(j);
  44. } else if (oper == '*') {
  45. j = A * B;
  46. show.setText(Double.toString(j));
  47. strA.delete(0,len1);
  48. strB.delete(0,len2);
  49. strA.append(j);
  50. } else if (oper == '/') {
  51. try{j= division(A, B);}catch(munber_Exception u){
  52. show.setText(u.shows());
  53. }
  54. }
  55. else if (oper == '%') {
  56. j = A % B;
  57. show.setText(Double.toString(j));
  58. strA.delete(0,len1);
  59. strB.delete(0,len2);
  60. strA.append(j);
  61. }
  62. } else if (a=='C') {//清除
  63. show.setText(" ");
  64. oper='~';
  65. int len1=strA.length();
  66. int len2=strB.length();
  67. strA.delete(0,len1);
  68. strB.delete(0,len2);
  69. } else if (a=='D'){//删除
  70. if(oper!='~'){
  71. if(strB.length()>0){
  72. strB.delete(strB.length()-1,strB.length());
  73. show.setText(String.valueOf(strA)+oper+String.valueOf(strB));
  74. }
  75. else
  76. show.setText("0");
  77. }if(oper=='~'){
  78. if(strA.length()>0){
  79. strA.delete(strA.length()-1,strA.length());
  80. show.setText(String.valueOf(strA));
  81. }
  82. }
  83. } }catch(ArithmeticException m){
  84. System.out.println("除数不能为0");
  85. }
  86. }

完整代码如下:

  1. class munber_Exception extends Exception{ //异常处理
  2. String e;
  3. public munber_Exception(){
  4. }
  5. public munber_Exception(String message){
  6. this.e=message;
  7. }
  8. public String shows(){
  9. return e;
  10. }
  11. }
  12. class cal extends JFrame implements ActionListener {
  13. private JPanel p1, p2;
  14. private JTextArea show;
  15. private String box ;
  16. JMenuBar menubar;
  17. JMenu menu1, menu2, menu3;
  18. StringBuffer strA;
  19. StringBuffer strB;
  20. char oper ='~';
  21. double A;
  22. double B;
  23. private String[] text2 = {"C", "CE","%", "/",
  24. "7", "8", "9", "*",
  25. "4", "5", "6", "-",
  26. "1", "2", "3", "+",
  27. "DEL","0", ".", "="};//计算器按钮面板
  28. private JButton[] munButton = new JButton[text2.length];
  29. public cal() {
  30. p1 = new JPanel();
  31. p2 = new JPanel();
  32. show = new JTextArea();
  33. p1.setSize(600, 100);
  34. menubar = new JMenuBar();
  35. menu1 = new JMenu("查看(V)");
  36. menu2 = new JMenu("编辑(E)");
  37. menu3 = new JMenu("帮助(H)");
  38. strB=new StringBuffer();
  39. strA=new StringBuffer();
  40. }
  41. public void init() {//初始化
  42. this.setTitle("计算器");
  43. this.setBounds(200, 200, 320, 300);
  44. this.setLayout(new BorderLayout());
  45. this.add(p1, BorderLayout.CENTER);
  46. this.add(p2, BorderLayout.SOUTH);
  47. menubar.add(menu1);
  48. menubar.add(menu2);
  49. menubar.add(menu3);
  50. this.setJMenuBar(menubar);
  51. this.setLocationRelativeTo(null);
  52. this.setResizable(false);
  53. show.setPreferredSize(new Dimension(300, 100));
  54. p1.add(show);
  55. p2.setLayout(new GridLayout(5, 4, 2, 3));
  56. //添加数字按键
  57. for (int i = 0; i < text2.length; i++) {
  58. munButton[i] = new JButton(text2[i] + " ");
  59. p2.add(munButton[i]);
  60. }
  61. for (int i = 0; i < munButton.length; i++)
  62. munButton[i].addActionListener(this);
  63. this.setVisible(true);
  64. this.addWindowListener(new WindowAdapter() {
  65. @Override
  66. public void windowClosing(WindowEvent e) {
  67. System.exit(0);
  68. }
  69. });
  70. }
  71. public double division(double x,double y)throws munber_Exception{
  72. if(y==0){
  73. throw new munber_Exception("除数不能为0!");
  74. }
  75. else{
  76. return x/y;
  77. }
  78. }
  79. @Override
  80. public void actionPerformed(ActionEvent e) {
  81. try { String act=e.getActionCommand();
  82. char a=act.charAt(0);
  83. if (a=='0' ||a=='1' || a=='2' ||a=='3'||a=='4'||a=='5'||a=='6'||a=='7'||a=='8'||a=='9'||a=='.') {
  84. if(oper=='~'){
  85. strA.append(a);
  86. show.setText(String.valueOf(strA));
  87. }else {
  88. strB.append(a);
  89. show.setText(String.valueOf(strA)+oper+String.valueOf(strB));
  90. }
  91. }
  92. else if(a=='+'||a=='-'||a=='/'||a=='*'||a=='%'){
  93. oper=a;
  94. show.setText(String.valueOf(strA)+oper);
  95. }
  96. else if(a=='='){
  97. A = Double.parseDouble(String.valueOf(strA));
  98. B = Double.parseDouble(String.valueOf(strB));
  99. double j;
  100. int len1=strA.length();
  101. int len2=strB.length();
  102. if (oper == '+') {
  103. j = A + B;
  104. show.setText(Double.toString(j));
  105. strA.delete(0,len1);
  106. strB.delete(0,len2);
  107. strA.append(j);
  108. } else if (oper == '-') {
  109. j = A - B;
  110. show.setText(Double.toString(j));
  111. strA.delete(0,len1);
  112. strB.delete(0,len2);
  113. strA.append(j);
  114. } else if (oper == '*') {
  115. j = A * B;
  116. show.setText(Double.toString(j));
  117. strA.delete(0,len1);
  118. strB.delete(0,len2);
  119. strA.append(j);
  120. } else if (oper == '/') {
  121. try{j= division(A, B);}catch(munber_Exception u){
  122. show.setText(u.shows());
  123. }
  124. }
  125. else if (oper == '%') {
  126. j = A % B;
  127. show.setText(Double.toString(j));
  128. strA.delete(0,len1);
  129. strB.delete(0,len2);
  130. strA.append(j);
  131. }
  132. } else if (a=='C') {
  133. show.setText(" ");
  134. oper='~';
  135. int len1=strA.length();
  136. int len2=strB.length();
  137. strA.delete(0,len1);
  138. strB.delete(0,len2);
  139. } else if (a=='D'){
  140. if(oper!='~'){
  141. if(strB.length()>0){
  142. strB.delete(strB.length()-1,strB.length());
  143. show.setText(String.valueOf(strA)+oper+String.valueOf(strB));
  144. }
  145. else
  146. show.setText("0");
  147. }if(oper=='~'){
  148. if(strA.length()>0){
  149. strA.delete(strA.length()-1,strA.length());
  150. show.setText(String.valueOf(strA));
  151. }
  152. }
  153. } }catch(ArithmeticException m){
  154. System.out.println("除数不能为0");
  155. }
  156. }
  157. }
  158. public class Calculator {
  159. //调用
  160. public static void main(String[] args) {
  161. cal Calculator1 = new cal();
  162. Calculator1.init();
  163. }
  164. }
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/275812
推荐阅读
相关标签
  

闽ICP备14008679号