adeen-s

Calculator.java

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