Advertisement
niloy_47

Calculator Demo 3

Aug 22nd, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. public class Tanvir implements WindowListener, ActionListener {
  5. String str = "";
  6. char sign;
  7. double v1, v2, ans;
  8.  
  9. private Frame f;
  10. private MenuBar mb;
  11. private Menu edit, help;
  12. private MenuItem mi1, mi2, mi3, mi4, mi5;
  13. private Button num0, num1, num2, num3, num4, num5, num6, num7, num8, num9;
  14. private Button bAdd, bSub, bMul, bDiv, bPer, bSqrt, bFrac, bDot, bCE,
  15. bEqual, bBackspace, bClear;
  16. private TextField tf;
  17.  
  18. Panel p1, p2, p3, p4, p5;
  19.  
  20. public Tanvir() {
  21. f = new Frame("xE Calculator");
  22. tf = new TextField(30);
  23.  
  24. p1 = new Panel();
  25. p2 = new Panel();
  26. p3 = new Panel();
  27. p4 = new Panel();
  28. p5 = new Panel();
  29.  
  30. mb = new MenuBar();
  31. edit = new Menu("Edit");
  32. help = new Menu("Help");
  33.  
  34. mi1 = new MenuItem("Copy");
  35. mi1.setActionCommand("mi1");
  36. mi2 = new MenuItem("Paste");
  37. mi2.setActionCommand("mi2");
  38. mi3 = new MenuItem("Quit");
  39. mi3.setActionCommand("mi3");
  40. mi4 = new MenuItem("Help");
  41. mi4.setActionCommand("mi4");
  42. mi5 = new MenuItem("About");
  43. mi5.setActionCommand("mi5");
  44.  
  45. edit.add(mi1);
  46. edit.add(mi2);
  47. edit.addSeparator();
  48. edit.add(mi3);
  49. help.add(mi4);
  50. help.addSeparator();
  51. help.add(mi5);
  52.  
  53. bCE = new Button(" CE ");
  54. bCE.setActionCommand("bCE");
  55. bBackspace = new Button(" Backspace ");
  56. bBackspace.setActionCommand("bBackspace");
  57. bClear = new Button(" C ");
  58. bClear.setActionCommand("bClear");
  59. num7 = new Button(" 7 ");
  60. num7.setActionCommand("num7");
  61. num8 = new Button(" 8 ");
  62. num8.setActionCommand("num8");
  63. num9 = new Button(" 9 ");
  64. num9.setActionCommand("num9");
  65. bDiv = new Button(" / ");
  66. bDiv.setActionCommand("bDiv");
  67. bSqrt = new Button(" sqrt ");
  68. bSqrt.setActionCommand("bSqrt");
  69. num4 = new Button(" 4 ");
  70. num4.setActionCommand("num4");
  71. num5 = new Button(" 5 ");
  72. num5.setActionCommand("num5");
  73. num6 = new Button(" 6 ");
  74. num6.setActionCommand("num6");
  75. bMul = new Button(" x ");
  76. bMul.setActionCommand("bMul");
  77. bPer = new Button(" % ");
  78. bPer.setActionCommand("bPer");
  79. num1 = new Button(" 1 ");
  80. num1.setActionCommand("num1");
  81. num2 = new Button(" 2 ");
  82. num2.setActionCommand("num2");
  83. num3 = new Button(" 3 ");
  84. num3.setActionCommand("num3");
  85. bSub = new Button(" - ");
  86. bSub.setActionCommand("bSub");
  87. bFrac = new Button(" 1/x ");
  88. bFrac.setActionCommand("bFrac");
  89. num0 = new Button(" 0 ");
  90. num0.setActionCommand("num0");
  91. bAdd = new Button(" + ");
  92. bAdd.setActionCommand("bAdd");
  93. bDot = new Button(" . ");
  94. bDot.setActionCommand("bDot");
  95. bEqual = new Button(" = ");
  96. bEqual.setActionCommand("bEqual");
  97. }
  98.  
  99. public void launchFrame() {
  100. f.setMenuBar(mb);
  101. mb.add(edit);
  102. mb.add(help);
  103.  
  104. f.add(num0);
  105. f.add(num1);
  106. f.add(num2);
  107. f.add(num3);
  108. f.add(num4);
  109. f.add(num5);
  110. f.add(num6);
  111. f.add(num7);
  112. f.add(num8);
  113. f.add(num9);
  114. f.add(bAdd);
  115. f.add(bSub);
  116. f.add(bMul);
  117. f.add(bDiv);
  118. f.add(bPer);
  119. f.add(bCE);
  120. f.add(bSqrt);
  121. f.add(bDot);
  122. f.add(bFrac);
  123.  
  124. p1.add(bBackspace);
  125. p1.add(bCE);
  126. p1.add(bClear);
  127. p2.add(num7);
  128. p2.add(num8);
  129. p2.add(num9);
  130. p2.add(bDiv);
  131. p2.add(bSqrt);
  132. p3.add(num4);
  133. p3.add(num5);
  134. p3.add(num6);
  135. p3.add(bMul);
  136. p3.add(bPer);
  137. p4.add(num1);
  138. p4.add(num2);
  139. p4.add(num3);
  140. p4.add(bSub);
  141. p4.add(bFrac);
  142. p5.add(num0);
  143. p5.add(bDot);
  144. p5.add(bAdd);
  145. p5.add(bEqual);
  146.  
  147. f.add(tf);
  148. tf.setText("0");
  149. tf.setEnabled(false);
  150.  
  151. bClear.addActionListener(this);
  152. bCE.addActionListener(this);
  153. num0.addActionListener(this);
  154. num1.addActionListener(this);
  155. num2.addActionListener(this);
  156. num3.addActionListener(this);
  157. num4.addActionListener(this);
  158. num5.addActionListener(this);
  159. num6.addActionListener(this);
  160. num7.addActionListener(this);
  161. num8.addActionListener(this);
  162. num9.addActionListener(this);
  163. bAdd.addActionListener(this);
  164. bSub.addActionListener(this);
  165. bMul.addActionListener(this);
  166. bDiv.addActionListener(this);
  167. bPer.addActionListener(this);
  168. bSqrt.addActionListener(this);
  169. bFrac.addActionListener(this);
  170. bDot.addActionListener(this);
  171. bEqual.addActionListener(this);
  172. bBackspace.addActionListener(this);
  173. mi1.addActionListener(this);
  174. mi2.addActionListener(this);
  175. mi3.addActionListener(this);
  176. mi3.addActionListener(this);
  177. mi4.addActionListener(this);
  178.  
  179. f.setLayout((new GridLayout(6, 0)));
  180. f.setSize(300, 250);
  181. f.add(p1);
  182. f.add(p2);
  183. f.add(p3);
  184. f.add(p4);
  185. f.add(p5);
  186. f.setVisible(true);
  187. }
  188.  
  189. public void actionPerformed(ActionEvent e) {
  190. if (e.getActionCommand() == "num0") {
  191. str = str + 0;
  192. tf.setText(str);
  193. }
  194. if (e.getActionCommand() == "num1") {
  195. str = str + 1;
  196. tf.setText(str);
  197. }
  198. if (e.getActionCommand() == "num2") {
  199. str = str + 2;
  200. tf.setText(str);
  201. }
  202. if (e.getActionCommand() == "num3") {
  203. str = str + 3;
  204. tf.setText(str);
  205. }
  206. if (e.getActionCommand() == "num4") {
  207. str = str + 4;
  208. tf.setText(str);
  209. }
  210. if (e.getActionCommand() == "num5") {
  211. str = str + 5;
  212. tf.setText(str);
  213. }
  214. if (e.getActionCommand() == "num6") {
  215. str = str + 6;
  216. tf.setText(str);
  217. }
  218. if (e.getActionCommand() == "num7") {
  219. str = str + 7;
  220. tf.setText(str);
  221. }
  222. if (e.getActionCommand() == "num8") {
  223. str = str + 8;
  224. tf.setText(str);
  225. }
  226. if (e.getActionCommand() == "num9") {
  227. str = str + 9;
  228. tf.setText(str);
  229. }
  230. if (e.getActionCommand() == "bDot") {
  231. str = str + ".";
  232. tf.setText(str);
  233. }
  234.  
  235.  
  236. if (e.getActionCommand() == "bAdd") {
  237. str = "";
  238. v1 = Double.parseDouble(tf.getText());
  239. sign = '+';
  240. tf.setText("");
  241. }
  242. if (e.getActionCommand() == "bSub") {
  243. str = "";
  244. v1 = Double.parseDouble(tf.getText());
  245. sign = '-';
  246. tf.setText("");
  247. }
  248. if (e.getActionCommand() == "bMul") {
  249. str = "";
  250. v1 = Double.parseDouble(tf.getText());
  251. sign = '*';
  252. tf.setText("");
  253. }
  254. if (e.getActionCommand() == "bDiv") {
  255. str = "";
  256. v1 = Double.parseDouble(tf.getText());
  257. sign = '/';
  258. tf.setText("");
  259. }
  260. if (e.getActionCommand() == "bSqrt") {
  261. str = "";
  262. v1 = Double.parseDouble(tf.getText());
  263. v1 = Math.sqrt(v1);
  264. tf.setText(""+v1);
  265. }
  266. if (e.getActionCommand() == "bFrac") {
  267. str = "";
  268. v1 = Double.parseDouble(tf.getText());
  269. v1 = 1/v1;
  270. tf.setText(""+v1);
  271. }
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284. if (e.getActionCommand() == "bEqual") {
  285. v2=Double.parseDouble(tf.getText());
  286.  
  287. if(sign == '+'){
  288. ans=v1+v2;
  289. tf.setText(" "+ans);
  290. sign= '=';
  291. }
  292. if(sign == '-'){
  293. ans=v1-v2;
  294. tf.setText(" "+ans);
  295. sign= '=';
  296. }
  297. if(sign == '*'){
  298. ans=v1*v2;
  299. tf.setText(" "+ans);
  300. sign= '=';
  301. }
  302. if(sign == '/'){
  303. ans=v1/v2;
  304. tf.setText(" "+ans);
  305. sign= '=';
  306. }
  307. if(sign == '%'){
  308. ans=(v1 % v2);
  309. tf.setText(" "+ans);
  310. sign= '=';
  311. }
  312.  
  313. }
  314. if (e.getActionCommand() == "mi3") {
  315. System.exit(0);
  316. }
  317. }
  318.  
  319. public void windowClosing(WindowEvent e) {
  320. System.exit(0);
  321. }
  322.  
  323. public void windowOpened(WindowEvent e) {
  324. }
  325.  
  326. public void windowIconified(WindowEvent e) {
  327. }
  328.  
  329. public void windowDeiconified(WindowEvent e) {
  330. }
  331.  
  332. public void windowClosed(WindowEvent e) {
  333. }
  334.  
  335. public void windowActivated(WindowEvent e) {
  336. }
  337.  
  338. public void windowDeactivated(WindowEvent e) {
  339. }
  340.  
  341. public static void main(String[] args) {
  342. Tanvir t = new Tanvir();
  343. t.launchFrame();
  344. }
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement