Advertisement
vvccs

E9_Calculator_With_Swing

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