Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3.  
  4.  
  5. class Calculator implements ActionListener
  6. {
  7. JFrame f;
  8. JTextField t;
  9. JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdiv,bmul,bsub,badd,bdec,beq,bdel,bclr;
  10.  
  11. static double a=0,b=0,result=0;
  12. static int operator=0;
  13.  
  14. Calculator()
  15. {
  16. f=new JFrame("Calculator");
  17. t=new JTextField();
  18. b1=new JButton("1");
  19. b2=new JButton("2");
  20. b3=new JButton("3");
  21. b4=new JButton("4");
  22. b5=new JButton("5");
  23. b6=new JButton("6");
  24. b7=new JButton("7");
  25. b8=new JButton("8");
  26. b9=new JButton("9");
  27. b0=new JButton("0");
  28. bdiv=new JButton("/");
  29. bmul=new JButton("*");
  30. bsub=new JButton("-");
  31. badd=new JButton("+");
  32. bdec=new JButton(".");
  33. beq=new JButton("=");
  34. bdel=new JButton("Delete");
  35. bclr=new JButton("Clear");
  36.  
  37. t.setBounds(30,40,280,30);
  38. b7.setBounds(40,100,50,40);
  39. b8.setBounds(110,100,50,40);
  40. b9.setBounds(180,100,50,40);
  41. bdiv.setBounds(250,100,50,40);
  42.  
  43. b4.setBounds(40,170,50,40);
  44. b5.setBounds(110,170,50,40);
  45. b6.setBounds(180,170,50,40);
  46. bmul.setBounds(250,170,50,40);
  47.  
  48. b1.setBounds(40,240,50,40);
  49. b2.setBounds(110,240,50,40);
  50. b3.setBounds(180,240,50,40);
  51. bsub.setBounds(250,240,50,40);
  52.  
  53. bdec.setBounds(40,310,50,40);
  54. b0.setBounds(110,310,50,40);
  55. beq.setBounds(180,310,50,40);
  56. badd.setBounds(250,310,50,40);
  57.  
  58. bdel.setBounds(60,380,100,40);
  59. bclr.setBounds(180,380,100,40);
  60.  
  61. f.add(t);
  62. f.add(b7);
  63. f.add(b8);
  64. f.add(b9);
  65. f.add(bdiv);
  66. f.add(b4);
  67. f.add(b5);
  68. f.add(b6);
  69. f.add(bmul);
  70. f.add(b1);
  71. f.add(b2);
  72. f.add(b3);
  73. f.add(bsub);
  74. f.add(bdec);
  75. f.add(b0);
  76. f.add(beq);
  77. f.add(badd);
  78. f.add(bdel);
  79. f.add(bclr);
  80.  
  81. f.setLayout(null);
  82. f.setVisible(true);
  83. f.setSize(350,500);
  84. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  85. f.setResizable(false);
  86.  
  87. b1.addActionListener(this);
  88. b2.addActionListener(this);
  89. b3.addActionListener(this);
  90. b4.addActionListener(this);
  91. b5.addActionListener(this);
  92. b6.addActionListener(this);
  93. b7.addActionListener(this);
  94. b8.addActionListener(this);
  95. b9.addActionListener(this);
  96. b0.addActionListener(this);
  97. badd.addActionListener(this);
  98. bdiv.addActionListener(this);
  99. bmul.addActionListener(this);
  100. bsub.addActionListener(this);
  101. bdec.addActionListener(this);
  102. beq.addActionListener(this);
  103. bdel.addActionListener(this);
  104. bclr.addActionListener(this);
  105. }
  106.  
  107. public void actionPerformed(ActionEvent e)
  108. {
  109. if(e.getSource()==b1)
  110. t.setText(t.getText().concat("1"));
  111.  
  112. if(e.getSource()==b2)
  113. t.setText(t.getText().concat("2"));
  114.  
  115. if(e.getSource()==b3)
  116. t.setText(t.getText().concat("3"));
  117.  
  118. if(e.getSource()==b4)
  119. t.setText(t.getText().concat("4"));
  120.  
  121. if(e.getSource()==b5)
  122. t.setText(t.getText().concat("5"));
  123.  
  124. if(e.getSource()==b6)
  125. t.setText(t.getText().concat("6"));
  126.  
  127. if(e.getSource()==b7)
  128. t.setText(t.getText().concat("7"));
  129.  
  130. if(e.getSource()==b8)
  131. t.setText(t.getText().concat("8"));
  132.  
  133. if(e.getSource()==b9)
  134. t.setText(t.getText().concat("9"));
  135.  
  136. if(e.getSource()==b0)
  137. t.setText(t.getText().concat("0"));
  138.  
  139. if(e.getSource()==bdec)
  140. t.setText(t.getText().concat("."));
  141.  
  142. if(e.getSource()==badd)
  143. {
  144. a=Double.parseDouble(t.getText());
  145. operator=1;
  146. t.setText("");
  147. }
  148.  
  149. if(e.getSource()==bsub)
  150. {
  151. a=Double.parseDouble(t.getText());
  152. operator=2;
  153. t.setText("");
  154. }
  155.  
  156. if(e.getSource()==bmul)
  157. {
  158. a=Double.parseDouble(t.getText());
  159. operator=3;
  160. t.setText("");
  161. }
  162.  
  163. if(e.getSource()==bdiv)
  164. {
  165. a=Double.parseDouble(t.getText());
  166. operator=4;
  167. t.setText("");
  168. }
  169.  
  170. if(e.getSource()==beq)
  171. {
  172. b=Double.parseDouble(t.getText());
  173.  
  174. switch(operator)
  175. {
  176. case 1: result=a+b;
  177. break;
  178.  
  179. case 2: result=a-b;
  180. break;
  181.  
  182. case 3: result=a*b;
  183. break;
  184.  
  185. case 4: result=a/b;
  186. break;
  187.  
  188. default: result=0;
  189. }
  190.  
  191. t.setText(""+result);
  192. }
  193.  
  194. if(e.getSource()==bclr)
  195. t.setText("");
  196.  
  197. if(e.getSource()==bdel)
  198. {
  199. String s=t.getText();
  200. t.setText("");
  201. for(int i=0;i<s.length()-1;i++)
  202. t.setText(t.getText()+s.charAt(i));
  203. }
  204. }
  205.  
  206. public static void main(String...s)
  207. {
  208. new Calculator();
  209. }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement