Advertisement
NB52053

Untitled

Dec 7th, 2016
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3.  
  4.  
  5. class Calc implements ActionListener
  6. {
  7. JFrame Frame;
  8. JTextField TF;
  9. JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,badd,bclr;
  10.  
  11. static int a=0,b=0,result=0;
  12. static int operator=0;
  13.  
  14. Calc()
  15. {
  16. Frame = new JFrame("Calculator");
  17. TF=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.  
  29.  
  30. badd=new JButton("+"); // For addition button
  31. bclr=new JButton("Clear"); // For clear button
  32.  
  33.  
  34. TF.setBounds(30,40,280,30); // TextFIelds Axis
  35.  
  36.  
  37. b1.setBounds(40,240,50,40);
  38. b2.setBounds(110,240,50,40);
  39. b3.setBounds(180,240,50,40);
  40. b4.setBounds(40,170,50,40);
  41. b5.setBounds(110,170,50,40);
  42. b6.setBounds(180,170,50,40);
  43. b7.setBounds(40,100,50,40);
  44. b8.setBounds(110,100,50,40);
  45. b9.setBounds(180,100,50,40);
  46. b0.setBounds(110,310,50,40);
  47.  
  48.  
  49. badd.setBounds(50,300,50,50);
  50. bclr.setBounds(180,380,100,40);
  51.  
  52.  
  53. Frame.add(TF); // TextField
  54.  
  55. Frame.add(b1); // Digits
  56. Frame.add(b2);
  57. Frame.add(b3);
  58. Frame.add(b4);
  59. Frame.add(b5);
  60. Frame.add(b6);
  61. Frame.add(b7);
  62. Frame.add(b8);
  63. Frame.add(b9);
  64. Frame.add(b0); // Digits
  65.  
  66.  
  67. Frame.add(badd); // AdditionButton
  68. Frame.add(bclr);
  69.  
  70. Frame.setLayout(null);
  71. Frame.setVisible(true);
  72. Frame.setSize(350,500);
  73. Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  74. Frame.setResizable(false);
  75.  
  76. b1.addActionListener(this);
  77. b2.addActionListener(this);
  78. b3.addActionListener(this);
  79. b4.addActionListener(this);
  80. b5.addActionListener(this);
  81. b6.addActionListener(this);
  82. b7.addActionListener(this);
  83. b8.addActionListener(this);
  84. b9.addActionListener(this);
  85. b0.addActionListener(this);
  86. badd.addActionListener(this);
  87. bclr.addActionListener(this);
  88.  
  89. }
  90.  
  91. public void actionPerformed(ActionEvent e)
  92. {
  93. if(e.getSource()==b1)
  94. TF.setText(TF.getText().concat("1"));
  95.  
  96. if(e.getSource()==b2)
  97. TF.setText(TF.getText().concat("2"));
  98.  
  99. if(e.getSource()==b3)
  100. TF.setText(TF.getText().concat("3"));
  101.  
  102. if(e.getSource()==b4)
  103. TF.setText(TF.getText().concat("4"));
  104.  
  105. if(e.getSource()==b5)
  106. TF.setText(TF.getText().concat("5"));
  107.  
  108. if(e.getSource()==b6)
  109. TF.setText(TF.getText().concat("6"));
  110.  
  111. if(e.getSource()==b7)
  112. TF.setText(TF.getText().concat("7"));
  113.  
  114. if(e.getSource()==b8)
  115. TF.setText(TF.getText().concat("8"));
  116.  
  117. if(e.getSource()==b9)
  118. TF.setText(TF.getText().concat("9"));
  119.  
  120. if(e.getSource()==b0)
  121. TF.setText(TF.getText().concat("0"));
  122.  
  123.  
  124. /*if(e.getSource()==badd)
  125. {
  126. a=Double.parseDouble(t.getText());
  127. operator=1;
  128. t.setText("");
  129. }*/
  130.  
  131.  
  132.  
  133.  
  134. if(e.getSource()==badd)
  135. {
  136. a=Integer.parseInt(TF.getText());
  137. b=Integer.parseInt(TF.getText());
  138. TF.setText("");
  139.  
  140. switch(operator)
  141. {
  142. case 1: result=a+b;
  143. break;
  144.  
  145.  
  146. default: result=0;
  147. }
  148.  
  149. TF.setText(""+result);
  150. }
  151.  
  152. if(e.getSource()==bclr)
  153. TF.setText("");
  154.  
  155. }
  156.  
  157. public static void main(String []args)
  158. {
  159. new Calc();
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement