修复了一些bug,实现了基本的加减运算数据:使用double类型存储数字,定义一个x表示字符串现在应有的长度,当t改变时使用x调整t的长度,x最高为15
    方法:新建一个字符串,用户按按钮时对其进行改变
    如:t=“0 ”
    用户按1,+,2键
    t=“1+2 ”
    遍历一遍t将数字与符号分别存入两个数组中,之后再进行运算
    监听代码:

    1. public void actionPerformed(ActionEvent e) {
    2. if(e.getActionCommand().equals("7")) {
    3. if(x==15) {
    4. return;
    5. }else if(t.charAt(0)=='0') {
    6. x=x-1;}
    7. if(o==1) {
    8. x=x-1;
    9. o=0;
    10. }
    11. StringBuilder strBuilder = new StringBuilder(t);
    12. strBuilder.setCharAt(x,'7');
    13. strBuilder.toString();
    14. t=strBuilder.toString();
    15. x=x+1;
    16. s=0;y=0;
    17. }else if(e.getActionCommand().equals("8")) {
    18. if(x==15) {
    19. return;
    20. }else if(t.charAt(0)=='0'){
    21. x=x-1;}
    22. if(o==1) {
    23. x=x-1;
    24. o=0;
    25. }
    26. StringBuilder strBuilder = new StringBuilder(t);
    27. strBuilder.setCharAt(x,'8');
    28. strBuilder.toString();
    29. t=strBuilder.toString();
    30. x=x+1;
    31. s=0;y=0;
    32. }else if(e.getActionCommand().equals("9")) {
    33. if(x==15) {
    34. return;
    35. }else if(t.charAt(0)=='0'){
    36. x=x-1;}
    37. if(o==1) {
    38. x=x-1;
    39. o=0;
    40. }
    41. StringBuilder strBuilder = new StringBuilder(t);
    42. strBuilder.setCharAt(x,'9');
    43. strBuilder.toString();
    44. t=strBuilder.toString();
    45. x=x+1;
    46. s=0;y=0;
    47. }else if(e.getActionCommand().equals("4")) {
    48. if(x==15) {
    49. return;
    50. }else if(t.charAt(0)=='0'){
    51. x=x-1;}
    52. if(o==1) {
    53. x=x-1;
    54. o=0;
    55. }
    56. StringBuilder strBuilder = new StringBuilder(t);
    57. strBuilder.setCharAt(x,'4');
    58. strBuilder.toString();
    59. t=strBuilder.toString();
    60. x=x+1;
    61. s=0;y=0;
    62. }else if(e.getActionCommand().equals("5")) {
    63. if(x==15) {
    64. return;
    65. }else if(t.charAt(0)=='0'){
    66. x=x-1;}
    67. if(o==1) {
    68. x=x-1;
    69. o=0;
    70. }
    71. StringBuilder strBuilder = new StringBuilder(t);
    72. strBuilder.setCharAt(x,'5');
    73. strBuilder.toString();
    74. t=strBuilder.toString();
    75. x=x+1;
    76. s=0;y=0;
    77. }else if(e.getActionCommand().equals("6")) {
    78. if(x==15) {
    79. return;
    80. }else if(t.charAt(0)=='0'){
    81. x=x-1;}
    82. if(o==1) {
    83. x=x-1;
    84. o=0;
    85. }
    86. StringBuilder strBuilder = new StringBuilder(t);
    87. strBuilder.setCharAt(x,'6');
    88. strBuilder.toString();
    89. t=strBuilder.toString();
    90. x=x+1;
    91. s=0;y=0;
    92. }else if(e.getActionCommand().equals("1")) {
    93. if(x==15) {
    94. return;
    95. }else if(t.charAt(0)=='0'){
    96. x=x-1;}
    97. if(o==1) {
    98. x=x-1;
    99. o=0;
    100. }
    101. StringBuilder strBuilder = new StringBuilder(t);
    102. strBuilder.setCharAt(x,'1');
    103. strBuilder.toString();
    104. t=strBuilder.toString();
    105. x=x+1;
    106. s=0;y=0;
    107. }else if(e.getActionCommand().equals("2")) {
    108. if(x==15) {
    109. return;
    110. }else if(t.charAt(0)=='0'){
    111. x=x-1;
    112. }
    113. if(o==1) {
    114. x=x-1;
    115. o=0;
    116. }
    117. StringBuilder strBuilder = new StringBuilder(t);
    118. strBuilder.setCharAt(x,'2');
    119. strBuilder.toString();
    120. t=strBuilder.toString();
    121. x=x+1;
    122. s=0;y=0;
    123. }else if(e.getActionCommand().equals("3")) {
    124. if(x==15) {
    125. return;
    126. }else if(t.charAt(0)=='0'){
    127. x=x-1;}
    128. if(o==1) {
    129. x=x-1;
    130. o=0;
    131. }
    132. StringBuilder strBuilder = new StringBuilder(t);
    133. strBuilder.setCharAt(x,'3');
    134. strBuilder.toString();
    135. t=strBuilder.toString();
    136. x=x+1;
    137. s=0;y=0;
    138. }else if(e.getActionCommand().equals("0")) {
    139. if(x==15) {
    140. return;
    141. }else if(t.charAt(0)=='0'){
    142. return;
    143. }else {
    144. if(t.charAt(x-1)=='+') {
    145. o=1;
    146. }
    147. StringBuilder strBuilder = new StringBuilder(t);
    148. strBuilder.setCharAt(x,'0');
    149. strBuilder.toString();
    150. t=strBuilder.toString();
    151. x=x+1;
    152. s=0;y=0;
    153. }
    154. }else if(e.getActionCommand().equals("+")) {
    155. if(x==15||x==14) {
    156. return;
    157. }else if(t.charAt(0)=='0'||s==1) {
    158. return;
    159. }else {
    160. if(y==1){
    161. x=x-1;
    162. }
    163. StringBuilder strBuilder = new StringBuilder(t);
    164. strBuilder.setCharAt(x,'+');
    165. strBuilder.toString();
    166. t=strBuilder.toString();
    167. s=1;
    168. y=0;
    169. x=x+1;
    170. }
    171. }else if(e.getActionCommand().equals("-")) {
    172. if(x==15||x==14) {
    173. return;
    174. }else if(t.charAt(0)=='0'||y==1) {
    175. return;
    176. }else
    177. {
    178. if(s==1){
    179. x=x-1;
    180. }
    181. StringBuilder strBuilder = new StringBuilder(t);
    182. strBuilder.setCharAt(x,'-');
    183. strBuilder.toString();
    184. t=strBuilder.toString();
    185. y=1;
    186. s=0;
    187. x=x+1;
    188. }
    189. }else if(e.getActionCommand().equals("=")) {
    190. int j=0;
    191. double []a=new double[10];
    192. int b=0;
    193. String[] t2=new String[10];
    194. while(t.charAt(j)!=' ') {
    195. String t1="0 ";
    196. for(int i=j;i<16;i++) {
    197. if(t.charAt(i)>='0'&&t.charAt(i)<='9') {
    198. StringBuilder strBuilder1 = new StringBuilder(t1);
    199. strBuilder1.setCharAt(i-j,t.charAt(i));
    200. strBuilder1.toString();
    201. t1=strBuilder1.toString();
    202. }else {
    203. c[b]=t.charAt(i);
    204. j=i+1;
    205. break;
    206. }
    207. }
    208. double all=0;
    209. for(int g=0;g<t1.replace(" ","").length();g++) {
    210. double u=(int)Math.pow(10,t1.replace(" ","").length()-g-1);
    211. all=all+u*Integer.parseInt(t1.charAt(g)+"");
    212. }
    213. t2[b]=t1;
    214. a[b]=all;
    215. b=b+1;
    216. }
    217. if(sum==0) {
    218. sum=a[0];
    219. }
    220. for(int l=0;l<b;l++) {
    221. if(c[l]=='+') {
    222. sum=sum+a[l+1];
    223. }else if(c[l]=='-') {
    224. sum=sum-a[l+1];
    225. }
    226. }
    227. int f=0;
    228. for(int m=0;m<b;m++) {
    229. if(t2[m].replace(" ","").length()>10) {
    230. t="error";
    231. sum=0;
    232. f=1;
    233. }
    234. }
    235. if(f==0) {
    236. t=String.valueOf(df.format(sum));
    237. }
    238. x=t.replace(" ", "").length();
    239. for(int h=0;h<16-(x-1);h++) {
    240. t=t+" ";
    241. }
    242. s=0;y=0;
    243. }else if(e.getActionCommand().equals("Replay")) {
    244. t="0 ";
    245. x=1;
    246. sum=0;
    247. }
    248. }