当前位置:   article > 正文

Java:实现简单的计算器(加减乘除)_java编写简易计算器实现加减乘除

java编写简易计算器实现加减乘除

基本要求:

1、采用Java Application方式建立应用程序框架
2、仿照Windows计算器,利用多种布局方式实现界面设计
3、完成简单的整数加、减、乘、除运算

简单计算器的界面如下:

 代码:

  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Container;
  4. import java.awt.Dimension;
  5. import java.awt.Font;
  6. import java.awt.GridLayout;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.WindowAdapter;
  10. import java.awt.event.WindowEvent;
  11. import javax.swing.*;
  12. public class Gui_Calculator {
  13. JFrame jFrame;//
  14. JLabel text,text2,text3,text4;//操作数1,运算符,操作数2,结果
  15. JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;//数字
  16. JButton b_add,b_sub,b_mul,b_div;//+,-,*,/
  17. JButton b_del,b_equal;//删除,=
  18. JPanel jPanel,jPanel2;//jPanel放操作数1,运算符,操作数2,结果 jPanel2放按钮
  19. Container contentPane;//
  20. Font font=new Font("微软黑体",Font.PLAIN,25);//设置字体为25磅的普通微软黑体
  21. double outcome=0;//初始化结果为零
  22. public Gui_Calculator() {
  23. jFrame=new JFrame("计算器");
  24. jFrame.setBounds(300, 200, 300, 400);//位置及大小
  25. jFrame.addWindowListener(new WindowAdapter(){//关闭窗口的监听
  26. public void windowClosing(WindowEvent e){
  27. System.out.println("系统退出!");
  28. System.exit(1);
  29. }
  30. });
  31. contentPane=jFrame.getContentPane();
  32. //文本框右对齐
  33. text=new JLabel(" ",SwingConstants.RIGHT);
  34. text.setFont(font);
  35. text2=new JLabel(" ",SwingConstants.RIGHT);
  36. text2.setFont(font);
  37. text3=new JLabel(" ",SwingConstants.RIGHT);
  38. text3.setFont(font);
  39. text4=new JLabel(" ",SwingConstants.RIGHT);
  40. text4.setFont(font);
  41. //数字button的创建及监听 b0~b9:内容与b0类似
  42. b0=new JButton("0");
  43. b0.setFont(font);
  44. b0.addActionListener(new ActionListener() {
  45. public void actionPerformed(ActionEvent e) {
  46. if(text2.getText()==" ") {//没有运算符 填写操作数1
  47. if(text.getText()==" ") {
  48. text.setText("");
  49. }
  50. text.setText(text.getText()+"0");
  51. }
  52. else {//有运算符 填写操作数2
  53. if(text4.getText()==" ") {
  54. if(text3.getText()==" ") {
  55. text3.setText("");
  56. }
  57. text3.setText(text3.getText()+"0");
  58. }
  59. }
  60. if(text4.getText()!=" ") {//有运算结果 重新填写操作数1并清空运算符、操作数2、结果
  61. text.setText("0");
  62. text2.setText(" ");
  63. text3.setText(" ");
  64. text4.setText(" ");
  65. }
  66. }
  67. });
  68. b1=new JButton("1");
  69. b1.setFont(font);
  70. b1.addActionListener(new ActionListener() {
  71. public void actionPerformed(ActionEvent e) {
  72. if(text2.getText()==" ") {
  73. if(text.getText()==" ") {
  74. text.setText("");
  75. }
  76. text.setText(text.getText()+"1");
  77. }
  78. else {
  79. if(text4.getText()==" ") {
  80. if(text3.getText()==" ") {
  81. text3.setText("");
  82. }
  83. text3.setText(text3.getText()+"1");
  84. }
  85. }
  86. if(text4.getText()!=" ") {
  87. text.setText("1");
  88. text2.setText(" ");
  89. text3.setText(" ");
  90. text4.setText(" ");
  91. }
  92. }
  93. });
  94. b2=new JButton("2");
  95. b2.setFont(font);
  96. b2.addActionListener(new ActionListener() {
  97. public void actionPerformed(ActionEvent e) {
  98. if(text2.getText()==" ") {
  99. if(text.getText()==" ") {
  100. text.setText("");
  101. }
  102. text.setText(text.getText()+"2");
  103. }
  104. else {
  105. if(text4.getText()==" ") {
  106. if(text3.getText()==" ") {
  107. text3.setText("");
  108. }
  109. text3.setText(text3.getText()+"2");
  110. }
  111. }
  112. if(text4.getText()!=" ") {
  113. text.setText("2");
  114. text2.setText(" ");
  115. text3.setText(" ");
  116. text4.setText(" ");
  117. }
  118. }
  119. });
  120. b3=new JButton("3");
  121. b3.setFont(font);
  122. b3.addActionListener(new ActionListener() {
  123. public void actionPerformed(ActionEvent e) {
  124. if(text2.getText()==" ") {
  125. if(text.getText()==" ") {
  126. text.setText("");
  127. }
  128. text.setText(text.getText()+"3");
  129. }
  130. else {
  131. if(text4.getText()==" ") {
  132. if(text3.getText()==" ") {
  133. text3.setText("");
  134. }
  135. text3.setText(text3.getText()+"3");
  136. }
  137. }
  138. if(text4.getText()!=" ") {
  139. text.setText("3");
  140. text2.setText(" ");
  141. text3.setText(" ");
  142. text4.setText(" ");
  143. }
  144. }
  145. });
  146. b4=new JButton("4");
  147. b4.setFont(font);
  148. b4.addActionListener(new ActionListener() {
  149. public void actionPerformed(ActionEvent e) {
  150. if(text2.getText()==" ") {
  151. if(text.getText()==" ") {
  152. text.setText("");
  153. }
  154. text.setText(text.getText()+"4");
  155. }
  156. else {
  157. if(text4.getText()==" ") {
  158. if(text3.getText()==" ") {
  159. text3.setText("");
  160. }
  161. text3.setText(text3.getText()+"4");
  162. }
  163. }
  164. if(text4.getText()!=" ") {
  165. text.setText("4");
  166. text2.setText(" ");
  167. text3.setText(" ");
  168. text4.setText(" ");
  169. }
  170. }
  171. });
  172. b5=new JButton("5");
  173. b5.setFont(font);
  174. b5.addActionListener(new ActionListener() {
  175. public void actionPerformed(ActionEvent e) {
  176. if(text2.getText()==" ") {
  177. if(text.getText()==" ") {
  178. text.setText("");
  179. }
  180. text.setText(text.getText()+"5");
  181. }
  182. else {
  183. if(text4.getText()==" ") {
  184. if(text3.getText()==" ") {
  185. text3.setText("");
  186. }
  187. text3.setText(text3.getText()+"5");
  188. }
  189. }
  190. if(text4.getText()!=" ") {
  191. text.setText("5");
  192. text2.setText(" ");
  193. text3.setText(" ");
  194. text4.setText(" ");
  195. }
  196. }
  197. });
  198. b6=new JButton("6");
  199. b6.setFont(font);
  200. b6.addActionListener(new ActionListener() {
  201. public void actionPerformed(ActionEvent e) {
  202. if(text2.getText()==" ") {
  203. if(text.getText()==" ") {
  204. text.setText("");
  205. }
  206. text.setText(text.getText()+"6");
  207. }
  208. else {
  209. if(text4.getText()==" ") {
  210. if(text3.getText()==" ") {
  211. text3.setText("");
  212. }
  213. text3.setText(text3.getText()+"6");
  214. }
  215. }
  216. if(text4.getText()!=" ") {
  217. text.setText("6");
  218. text2.setText(" ");
  219. text3.setText(" ");
  220. text4.setText(" ");
  221. }
  222. }
  223. });
  224. b7=new JButton("7");
  225. b7.setFont(font);
  226. b7.addActionListener(new ActionListener() {
  227. public void actionPerformed(ActionEvent e) {
  228. if(text2.getText()==" ") {
  229. if(text.getText()==" ") {
  230. text.setText("");
  231. }
  232. text.setText(text.getText()+"7");
  233. }
  234. else {
  235. if(text4.getText()==" ") {
  236. if(text3.getText()==" ") {
  237. text3.setText("");
  238. }
  239. text3.setText(text3.getText()+"7");
  240. }
  241. }
  242. if(text4.getText()!=" ") {
  243. text.setText("7");
  244. text2.setText(" ");
  245. text3.setText(" ");
  246. text4.setText(" ");
  247. }
  248. }
  249. });
  250. b8=new JButton("8");
  251. b8.setFont(font);
  252. b8.addActionListener(new ActionListener() {
  253. public void actionPerformed(ActionEvent e) {
  254. if(text2.getText()==" ") {
  255. if(text.getText()==" ") {
  256. text.setText("");
  257. }
  258. text.setText(text.getText()+"8");
  259. }
  260. else {
  261. if(text4.getText()==" ") {
  262. if(text3.getText()==" ") {
  263. text3.setText("");
  264. }
  265. text3.setText(text3.getText()+"8");
  266. }
  267. }
  268. if(text4.getText()!=" ") {
  269. text.setText("8");
  270. text2.setText(" ");
  271. text3.setText(" ");
  272. text4.setText(" ");
  273. }
  274. }
  275. });
  276. b9=new JButton("9");
  277. b9.setFont(font);
  278. b9.addActionListener(new ActionListener() {
  279. public void actionPerformed(ActionEvent e) {
  280. if(text2.getText()==" ") {
  281. if(text.getText()==" ") {
  282. text.setText("");
  283. }
  284. text.setText(text.getText()+"9");
  285. }
  286. else {
  287. if(text4.getText()==" ") {
  288. if(text3.getText()==" ") {
  289. text3.setText("");
  290. }
  291. text3.setText(text3.getText()+"9");
  292. }
  293. }
  294. if(text4.getText()!=" ") {
  295. text.setText("9");
  296. text2.setText(" ");
  297. text3.setText(" ");
  298. text4.setText(" ");
  299. }
  300. }
  301. });
  302. //运算符button的创建及监听 b_add:+,b_sub:-,b_mul:*,b_div:/
  303. b_add=new JButton("+");
  304. b_add.setFont(font);
  305. b_add.addActionListener(new ActionListener() {
  306. public void actionPerformed(ActionEvent e) {
  307. if(text3.getText()==" ") {
  308. if(text.getText()!=" ") {
  309. text2.setText("+");
  310. }
  311. }
  312. if(text4.getText()!="不能除零!!!"&&text4.getText()!=" ") {
  313. text.setText(""+outcome);
  314. text2.setText("+");
  315. text3.setText(" ");
  316. text4.setText(" ");
  317. }
  318. }
  319. });
  320. b_sub=new JButton("-");
  321. b_sub.setFont(font);
  322. b_sub.addActionListener(new ActionListener() {
  323. public void actionPerformed(ActionEvent e) {
  324. if(text3.getText()==" ") {
  325. if(text.getText()!=" ") {
  326. text2.setText("-");
  327. }
  328. }
  329. if(text4.getText()!="不能除零!!!"&&text4.getText()!=" ") {
  330. text.setText(""+outcome);
  331. text2.setText("-");
  332. text3.setText(" ");
  333. text4.setText(" ");
  334. }
  335. }
  336. });
  337. b_mul=new JButton("*");
  338. b_mul.setFont(font);
  339. b_mul.addActionListener(new ActionListener() {
  340. public void actionPerformed(ActionEvent e) {
  341. if(text3.getText()==" ") {
  342. if(text.getText()!=" ") {
  343. text2.setText("*");
  344. }
  345. }
  346. if(text4.getText()!="不能除零!!!"&&text4.getText()!=" ") {
  347. text.setText(""+outcome);
  348. text2.setText("*");
  349. text3.setText(" ");
  350. text4.setText(" ");
  351. }
  352. }
  353. });
  354. b_div=new JButton("/");
  355. b_div.setFont(font);
  356. b_div.addActionListener(new ActionListener() {
  357. public void actionPerformed(ActionEvent e) {
  358. if(text3.getText()==" ") {
  359. if(text.getText()!=" ") {
  360. text2.setText("/");
  361. }
  362. }
  363. if(text4.getText()!="不能除零!!!"&&text4.getText()!=" ") {
  364. text.setText(""+outcome);
  365. text2.setText("/");
  366. text3.setText(" ");
  367. text4.setText(" ");
  368. }
  369. }
  370. });
  371. //删除button的创建及监听 b_del
  372. b_del=new JButton("CE");
  373. b_del.setFont(font);
  374. b_del.addActionListener(new ActionListener() {
  375. public void actionPerformed(ActionEvent e) {
  376. text.setText(" ");
  377. text2.setText(" ");
  378. text3.setText(" ");
  379. text4.setText(" ");
  380. }
  381. });
  382. //等于button的创建及监听 b_equal
  383. b_equal=new JButton("=");
  384. b_equal.setFont(font);
  385. b_equal.addActionListener(new ActionListener() {
  386. public void actionPerformed(ActionEvent e) {
  387. if(text3.getText()!=" ") {//有操作数2 进行运算
  388. switch (text2.getText()){
  389. case "+"://加
  390. outcome=Double.valueOf(text.getText().trim())+Double.valueOf(text3.getText().trim());
  391. text4.setText("="+outcome);
  392. break;
  393. case "-"://减
  394. outcome=Double.valueOf(text.getText().trim())-Double.valueOf(text3.getText().trim());
  395. text4.setText("="+outcome);
  396. break;
  397. case "*"://乘
  398. outcome=Double.valueOf(text.getText().trim())*Double.valueOf(text3.getText().trim());
  399. text4.setText("="+outcome);
  400. break;
  401. case "/"://除
  402. if(Integer.valueOf(text3.getText())==0) {//除数为零
  403. text4.setText("不能除零!!!");
  404. }
  405. else {//除数不为零
  406. outcome=Double.valueOf(text.getText().trim())/Double.valueOf(text3.getText().trim());
  407. text4.setText("="+outcome);
  408. }
  409. break;
  410. }
  411. }
  412. if(text2.getText()==" "&&text.getText()!=" ") {//没有运算符 有操作数1 直接等于操作数1
  413. outcome=Double.valueOf(text.getText());
  414. text4.setText("="+outcome);
  415. }
  416. }
  417. });
  418. jPanel=new JPanel();
  419. jPanel2=new JPanel();
  420. jPanel.setLayout(new GridLayout(4,1));
  421. jPanel2.setLayout(new GridLayout(4,4));
  422. //添加组件
  423. jPanel.add(text);
  424. jPanel.add(text2);
  425. jPanel.add(text3);
  426. jPanel.add(text4);
  427. jPanel2.add(b_add);
  428. jPanel2.add(b_sub);
  429. jPanel2.add(b_mul);
  430. jPanel2.add(b_div);
  431. jPanel2.add(b9);
  432. jPanel2.add(b0);
  433. jPanel2.add(b_del);
  434. jPanel2.add(b_equal);
  435. jPanel2.add(b5);
  436. jPanel2.add(b6);
  437. jPanel2.add(b7);
  438. jPanel2.add(b8);
  439. jPanel2.add(b1);
  440. jPanel2.add(b2);
  441. jPanel2.add(b3);
  442. jPanel2.add(b4);
  443. contentPane.add(jPanel,BorderLayout.NORTH);
  444. contentPane.add(jPanel2);
  445. jFrame.show();
  446. }
  447. public static void main(String[] args) {
  448. new Gui_Calculator();
  449. }
  450. }

该计算器的功能简单只能实现加、减、乘、除以及求出结果后的连续运算的简单功能

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

闽ICP备14008679号