Java实现的窗口计算器

计算器代码如下:

  1 package ljm.calculator;
  2 
  3 import javax.swing.*;
  4 import java.awt.*;
  5 import java.io.Serializable;
  6 import java.math.*;
  7 
  8 public class Main {
  9     public static void main(String[] args) {
 10         new Calculator("敏敏敏牌计算器 - ljm@lpqo.cn");
 11     }
 12 }
 13 
 14 class Calculator extends JFrame {
 15     private TextArea show;  //  显示
 16 
 17     public Calculator(String t){
 18         super(t);
 19         setLayout(null);
 20         setSize(430,488);
 21         setVisible(true);
 22         setLocationRelativeTo(null);
 23         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 24         setResizable(false);
 25         this.setBackground(new Color(237, 239, 238));
 26 
 27         setShow();
 28         addBut();
 29         setVisible(true);
 30     }
 31 
 32     private void setShow() {
 33         show = new TextArea("",8,52,1);
 34         show.setSize(415,102);
 35         show.setLocation(0,0);
 36         show.setFont(new Font("", Font.BOLD, 40));
 37         show.setBackground(Color.WHITE);
 38         show.setForeground(new Color(48, 48, 48));
 39         show.setEditable(false);
 40         show.setText(0 + "");
 41         this.add(show);
 42     }
 43 
 44     private double nub1 = 0;    //  数1
 45     private double nub2 = 0;    //  数2
 46     private String snub2 = "";  //  数2的字符串型
 47     private int f = '+';        //  运算符号
 48     private boolean isDoc = false;  //  是否有小数点
 49     private boolean isEqual = false;    //  是否点击等于
 50     private int nubLen = 0;
 51 
 52     private void setBut(String s, int i, int j, int x, int y, final int c) {
 53         Button b = new Button(s);
 54         b.setLocation(x, y);
 55         b.setSize(i, j);
 56         b.setFont(new Font("", Font.BOLD, 35));
 57         b.setBackground(new Color(254, 254, 254));
 58         b.setForeground(new Color(88, 92, 100));
 59 
 60         //  计算逻辑算法
 61         //  Ljm ljm@lpqo.cn
 62         //  2020.6.23 JXUT
 63         b.addActionListener(e -> {
 64             if (c < 10) {
 65                 //    数字0-9
 66                 if (isEqual) {
 67                     nub1 = 0;
 68                 }
 69                 if (nubLen > 20) {
 70                     return;
 71                 }
 72                 if (c == 0 && nub2 == 0 ) {
 73                     return;
 74                 }
 75                 snub2 += String.valueOf(c);
 76                 show.setText(snub2);
 77                 nubLen = show.getText().length();
 78                 nub2 = Double.parseDouble(snub2);
 79 
 80             } else if (c == '.') {
 81                 //    小数
 82                 if (nubLen > 20) {
 83                     return;
 84                 }
 85                 if (!isDoc) {
 86                     if (nub2 != 0) {
 87                         snub2 += ".";
 88                     } else {
 89                         snub2 = "0.";
 90                     }
 91                     nub2 = Double.parseDouble(snub2);
 92                     show.setText(snub2);
 93                     isDoc = true;
 94                 }
 95 
 96             } else if (c == 'S') {
 97                 // 结果相反数
 98                 if (Double.parseDouble(show.getText()) == nub1) {
 99                     nub1 = 0 - nub1;
100                     showf(nub1 +"");
101                 } else {
102                     nub2 = 0 -nub2;
103                     showf(nub2 +"");
104                 }
105             } else if (c == 'C') {
106                 // 归零
107                 nub1 = 0;
108                 nub2 = 0;
109                 snub2 = "";
110                 f = '+';
111                 isDoc = false;
112                 isEqual = false;
113                 nubLen = 0;
114                 showf("0");
115             } else {
116                 isEqual = false;
117                 nubLen = 0;
118 
119                 switch (f) {
120                 case '/':
121                     //    除法
122                     nub1 = DoubleUtil.divide(nub1, nub2);
123                     break;
124                 case '*':
125                     // 乘法
126                     nub1 = DoubleUtil.mul(nub1, nub2);
127                     break;
128                 case '-':
129                     // 减法
130                     nub1 = DoubleUtil.sub(nub1, nub2);
131                     break;
132                 case '+':
133                     //    加法
134                     nub1 = DoubleUtil.add(nub1, nub2);
135                     break;
136                 }
137 
138                 showf(nub1 + "");
139                 if (c != '=') {
140                     f = c;
141                 } else {
142                     // 等于
143                     f = '+';
144                     isEqual = true;
145                 }
146 
147                 nub2 = 0;
148                 snub2 = "";
149                 isDoc = false;
150             }
151         });
152         this.add(b);
153     }
154 
155 
156     //    去0小数
157     private void showf(String s ){
158         double td = Double.parseDouble(s);
159         int ti = (int)td;
160 
161         if (td == ti) {
162             show.setText(ti +"");
163         } else {
164             show.setText(td + "");
165         }
166     }
167 
168     private void addBut() {
169         setBut("AC", 215, 70, 0, 100, 'C');
170         setBut("±", 107, 70, 214, 100, 'S');
171         setBut("÷", 108, 70, 312, 100, '/');
172 
173         setBut("7", 107, 70, 0, 170, 7);
174         setBut("8", 108, 70, 107, 170, 8);
175         setBut("9", 107, 70, 214, 170, 9);
176         setBut("×", 108, 70, 312, 170, '*');
177 
178         setBut("4", 107, 70, 0, 240, 4);
179         setBut("5", 108, 70, 107, 240, 5);
180         setBut("6", 107, 70, 214, 240, 6);
181         setBut("-", 108, 70, 312, 240, '-');
182 
183         setBut("1", 107, 70, 0, 310, 1);
184         setBut("2", 108, 70, 107, 310, 2);
185         setBut("3", 107, 70, 214, 310, 3);
186         setBut("+", 108, 70, 312, 310, '+');
187 
188         setBut("0", 107, 70, 0, 380, 0);
189         setBut(".", 108, 70, 107, 380, '.');
190         setBut("=", 215, 70, 214, 380, '=');
191     }
192 
193 }
194 
195 //  BigDecimal的Double精确计算
196 class DoubleUtil implements Serializable {
197     private static final long serialVersionUID = -3345205828566485102L;
198     private static final Integer DEF_DIV_SCALE = 40;    //  除法精度
199 
200     //  精确加
201     public static Double add(Double value1, Double value2) {
202         BigDecimal b1 = new BigDecimal(Double.toString(value1));
203         BigDecimal b2 = new BigDecimal(Double.toString(value2));
204         return b1.add(b2).doubleValue();
205     }
206 
207     //  精确减
208     public static double sub(Double value1, Double value2) {
209         BigDecimal b1 = new BigDecimal(Double.toString(value1));
210         BigDecimal b2 = new BigDecimal(Double.toString(value2));
211         return b1.subtract(b2).doubleValue();
212     }
213 
214     //  精确乘
215     public static Double mul(Double value1, Double value2) {
216         BigDecimal b1 = new BigDecimal(Double.toString(value1));
217         BigDecimal b2 = new BigDecimal(Double.toString(value2));
218         return b1.multiply(b2).doubleValue();
219     }
220 
221     //  精确除 精度为 DEF_DIV_SCALE
222     public static Double divide(Double dividend, Double divisor) {
223         return divide(dividend, divisor, DEF_DIV_SCALE);
224     }
225 
226     //  精确除
227     public static double divide(Double dividend, Double divisor, Integer scale) {
228         if (scale < 0) {
229             return -1;
230         }
231         BigDecimal b1 = new BigDecimal(Double.toString(dividend));
232         BigDecimal b2 = new BigDecimal(Double.toString(divisor));
233         return b1.divide(b2, scale, RoundingMode.HALF_UP).doubleValue();
234     }
235 
236 }

运行截图如下:

敬请指正。

posted @ 2020-06-24 15:21  ljmjiudu  阅读(911)  评论(0编辑  收藏  举报