Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.54 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Button;
  3. import java.awt.Color;
  4. import java.awt.Frame;
  5. import java.awt.GridLayout;
  6. import java.awt.Panel;
  7. import java.awt.TextField;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.WindowEvent;
  11. import java.awt.event.WindowListener;
  12.  
  13. public class CalcListener implements WindowListener, ActionListener {
  14. private Frame f;
  15. private Panel p;
  16. private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
  17. private Button division;
  18. private Button plus;
  19. private Button minus;
  20. private Button multiply;
  21. private Button decPoint;
  22. private Button equal;
  23. private TextField tf;
  24.  
  25.  
  26. double c,n;
  27. String s1,s2,s3,s4,s5;
  28.  
  29. public CalcListener () {
  30. f = new Frame("GuiCalculator");
  31. p = new Panel();
  32. tf = new TextField ();
  33. b0 = new Button("0"); b9 = new Button("9");
  34. b1 = new Button("1"); b2 = new Button("2");
  35. b3 = new Button("3"); b4 = new Button("4");
  36. b5 = new Button("5"); b6 = new Button("6");
  37. b7 = new Button("7"); b8 = new Button("8");
  38. division = new Button("/"); plus = new Button("+");
  39. minus = new Button("-"); multiply = new Button("*");
  40. decPoint = new Button("."); equal = new Button("=");
  41. }
  42.  
  43. public void launchFrame() {
  44. f.addWindowListener(this);
  45.  
  46. f.add( tf,BorderLayout.NORTH);
  47.  
  48. //f.setBackground(Color.blue);
  49.  
  50. //p.setBackground(Color.yellow);
  51. f.add(p,BorderLayout.CENTER);
  52. p.setLayout (new GridLayout(4,4));
  53. p.add(b7); p.add(b8); p.add(b9); p.add(division);
  54. p.add(b4); p.add(b5); p.add(b6); p.add(multiply);
  55. p.add(b1); p.add(b2); p.add(b3); p.add(minus);
  56. p.add(b0); p.add(decPoint ); p.add(equal); p.add(plus);
  57.  
  58. b0.addActionListener(this);
  59. b1.addActionListener(this);
  60. b2.addActionListener(this);
  61. b3.addActionListener(this);
  62. b4.addActionListener(this);
  63. b5.addActionListener(this);
  64. b6.addActionListener(this);
  65. b7.addActionListener(this);
  66. b8.addActionListener(this);
  67. b9.addActionListener(this);
  68. division.addActionListener(this);
  69. plus.addActionListener(this);
  70. minus.addActionListener(this);
  71. multiply.addActionListener(this);
  72. decPoint .addActionListener(this);
  73. equal.addActionListener(this);
  74.  
  75.  
  76. f.setSize(300, 300);
  77. f.setVisible(true);
  78. }
  79.  
  80. public static void main(String[] args) {
  81. CalcListener calcu = new CalcListener();
  82. calcu.launchFrame();
  83. }
  84.  
  85. @Override
  86. public void actionPerformed(ActionEvent e) {
  87. if(e.getSource()==b0)
  88. {
  89. s3 = tf.getText();
  90. s4 = "0";
  91. s5 = s3+s4;
  92. tf.setText(s5);
  93. }
  94. if(e.getSource()==b1)
  95. {
  96. s3 = tf.getText();
  97. s4 = "1";
  98. s5 = s3+s4;
  99. tf.setText(s5);
  100. }
  101. if(e.getSource()==b2)
  102. {
  103. s3 = tf.getText();
  104. s4 = "2";
  105. s5 = s3+s4;
  106. tf.setText(s5);
  107. }if(e.getSource()==b3)
  108. {
  109. s3 = tf.getText();
  110. s4 = "3";
  111. s5 = s3+s4;
  112. tf.setText(s5);
  113. }
  114. if(e.getSource()==b4)
  115. {
  116. s3 = tf.getText();
  117. s4 = "4";
  118. s5 = s3+s4;
  119. tf.setText(s5);
  120. }
  121. if(e.getSource()==b5)
  122. {
  123. s3 = tf.getText();
  124. s4 = "5";
  125. s5 = s3+s4;
  126. tf.setText(s5);
  127. }
  128. if(e.getSource()==b6)
  129. {
  130. s3 = tf.getText();
  131. s4 = "6";
  132. s5 = s3+s4;
  133. tf.setText(s5);
  134. }
  135. if(e.getSource()==b7)
  136. {
  137. s3 = tf.getText();
  138. s4 = "7";
  139. s5 = s3+s4;
  140. tf.setText(s5);
  141. }
  142. if(e.getSource()==b8)
  143. {
  144. s3 = tf.getText();
  145. s4 = "8";
  146. s5 = s3+s4;
  147. tf.setText(s5);
  148. }
  149. if(e.getSource()==b9)
  150. {
  151. s3 = tf.getText();
  152. s4 = "9";
  153. s5 = s3+s4;
  154. tf.setText(s5);
  155. }
  156. if(e.getSource()==plus)
  157. {
  158. s1 = tf.getText();
  159. tf.setText("");
  160. c=1;
  161.  
  162. }
  163. if(e.getSource()==minus)
  164. {
  165. s1 = tf.getText();
  166. tf.setText("");
  167. c=2;
  168.  
  169. }
  170. if(e.getSource()==multiply)
  171. {
  172. s1 = tf.getText();
  173. tf.setText("");
  174. c=3;
  175.  
  176. }
  177. if(e.getSource()==division)
  178. {
  179. s1 = tf.getText();
  180. tf.setText("");
  181. c=4;
  182.  
  183. }
  184.  
  185. if(e.getSource()==equal)
  186. {
  187. s2 = tf.getText();
  188. if(c==1)
  189. {
  190. n = Integer.parseInt(s1)+Integer.parseInt(s2);
  191. tf.setText(String.valueOf(n));
  192. }
  193. else
  194. if(c==2)
  195. {
  196. n = Integer.parseInt(s1)-Integer.parseInt(s2);
  197. tf.setText(String.valueOf(n));
  198. }
  199. else
  200. if(c==3)
  201. {
  202. n = Integer.parseInt(s1)*Integer.parseInt(s2);
  203. tf.setText(String.valueOf(n));
  204. }
  205. if(c==4)
  206. {
  207. try
  208. {
  209. int p=Integer.parseInt(s2);
  210. if(p!=0)
  211. {
  212. n = Integer.parseInt(s1)/Integer.parseInt(s2);
  213. tf.setText(String.valueOf(n));
  214. }
  215. else
  216. tf.setText("infinite");
  217.  
  218. }
  219. catch(Exception i){}
  220. }
  221.  
  222. if(e.getSource()==decPoint ){
  223. tf.setText(tf.getText()+decPoint );
  224.  
  225. }
  226.  
  227.  
  228.  
  229. /*
  230. if(e.getSource()==dot){
  231.  
  232. s1 = tf.getText();
  233. if (s1.indexOf('.')==-1){
  234. tf.setText(".");
  235. c=5;
  236. } else{
  237.  
  238. }
  239. tf.setText("");
  240. c=5;
  241.  
  242. }*/
  243. }
  244.  
  245. }
  246.  
  247.  
  248.  
  249.  
  250. ///////////////////////////////////
  251. @Override
  252. public void windowOpened(WindowEvent e) {
  253. // TODO Auto-generated method stub
  254.  
  255. }
  256. /////////////////////////////////////
  257. @Override
  258. public void windowClosing(WindowEvent e) {
  259. System.exit(0);
  260. /////////////////////////////////////
  261. }
  262.  
  263. @Override
  264. public void windowClosed(WindowEvent e) {
  265. // TODO Auto-generated method stub
  266.  
  267. }
  268.  
  269. @Override
  270. public void windowIconified(WindowEvent e) {
  271. // TODO Auto-generated method stub
  272.  
  273. }
  274.  
  275. @Override
  276. public void windowDeiconified(WindowEvent e) {
  277. // TODO Auto-generated method stub
  278.  
  279. }
  280.  
  281. @Override
  282. public void windowActivated(WindowEvent e) {
  283. // TODO Auto-generated method stub
  284.  
  285. }
  286.  
  287. @Override
  288. public void windowDeactivated(WindowEvent e) {
  289. // TODO Auto-generated method stub
  290.  
  291. }
  292.  
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement