Advertisement
Guest User

Java Calc Problem

a guest
Nov 21st, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. package Calculator;
  2.  
  3. import java.awt.Color;
  4.  
  5. import javax.swing.*;
  6.  
  7. import java.awt.event.*;
  8.  
  9.  
  10. public class CalcPanel extends JPanel implements ActionListener {
  11. String num1="";
  12. String num2="";
  13. boolean usingFirst=true;
  14.  
  15. JTextField display;
  16. JButton b1;
  17. JButton b2;
  18. JButton b3;
  19. JButton b4;
  20. JButton b5;
  21. JButton b6;
  22. JButton b7;
  23. JButton b8;
  24. JButton b9;
  25. JButton bequals;
  26. JButton bsum;
  27. JButton bdifference;
  28. JButton bproduct;
  29. JButton bquotient;
  30. JButton bdecimal;
  31. JButton b0;
  32. JButton bclear;
  33.  
  34. public CalcPanel()
  35. {
  36. this.setBackground(Color.gray);
  37. setLayout(null);
  38. display=new JTextField();
  39. b0=new JButton("0");
  40. b1=new JButton("1");
  41. b2=new JButton("2");
  42. b3=new JButton("3");
  43. b4=new JButton("4");
  44. b5=new JButton("5");
  45. b6=new JButton("6");
  46. b7=new JButton("7");
  47. b8=new JButton("8");
  48. b9=new JButton("9");
  49. bclear=new JButton("c");
  50. bequals=new JButton("=");
  51. bsum=new JButton("+");
  52. bdifference=new JButton("-");
  53. bproduct=new JButton("*");
  54. bquotient=new JButton("/");
  55. bdecimal=new JButton(".");
  56. display.setBounds(0,0,200,350);
  57. b0.setBounds(0,275,100,50);
  58. bdecimal.setBounds(100,275,50,50);
  59. bequals.setBounds(150,225,50,100);
  60. b1.setBounds(0,225,50,50);
  61. b2.setBounds(50,225,50,50);
  62. b3.setBounds(100,225,50,50);
  63. b4.setBounds(0,175,50,50);
  64. b5.setBounds(50,175,50,50);
  65. b6.setBounds(100,175,50,50);
  66. bsum.setBounds(150,125,50,100);
  67. b7.setBounds(0,125,50,50);
  68. b8.setBounds(50,125,50,50);
  69. b9.setBounds(100,125,50,50);
  70. bproduct.setBounds(50,75,50,50);
  71. bdifference.setBounds(150,75,50,50);
  72. bquotient.setBounds(100,75,50,50);
  73. bclear.setBounds(0,75,50,50);
  74. display.setBounds(0,0,200,75);
  75.  
  76.  
  77. add(b0);
  78. add(bdecimal);
  79. add(bequals);
  80. add(b1);
  81. add(b2);
  82. add(b3);
  83. add(b4);
  84. add(b5);
  85. add(b6);
  86. add(b7);
  87. add(b8);
  88. add(b9);
  89. add(bsum);
  90. add(bdifference);
  91. add(bproduct);
  92. add(bquotient);
  93. add(bclear);
  94. add(display);
  95.  
  96. b1.addActionListener(this);
  97. b2.addActionListener(this);
  98. b3.addActionListener(this);
  99. b4.addActionListener(this);
  100. b5.addActionListener(this);
  101. b6.addActionListener(this);
  102. b7.addActionListener(this);
  103. b8.addActionListener(this);
  104. b9.addActionListener(this);
  105. b0.addActionListener(this);
  106. bequals.addActionListener(this);
  107. bsum.addActionListener(this);
  108. bclear.addActionListener(this);
  109. bdecimal.addActionListener(this);
  110. bdifference.addActionListener(this);
  111. bproduct.addActionListener(this);
  112. bquotient.addActionListener(this);
  113.  
  114.  
  115. }
  116.  
  117. public void actionPerformed(ActionEvent e){
  118. String s=e.getActionCommand();
  119. if(s.equals("1")||s.equals("2")||s.equals("3")||s.equals("4")||s.equals("5")||s.equals("6")||s.equals("7")||s.equals("8")||s.equals("9")||s.equals("0")||s.equals("."))
  120. {
  121. if(usingFirst)
  122. {num1=num1+s;
  123. display.setText(num1);
  124. }
  125. else
  126. { num2=num2+s;
  127. display.setText(num2);
  128. }
  129.  
  130. }
  131. if(s.equals("+"))
  132. {
  133. usingFirst=false;
  134. }
  135. if(s.equals("-"))
  136. {
  137. usingFirst=false;
  138. }
  139. if(s.equals("*"))
  140. {
  141. usingFirst=false;
  142. }
  143. if(s.equals("/"))
  144. {
  145. usingFirst=false;
  146. }
  147. if(s.equals("="))
  148. {
  149. Double total=Double.parseDouble(num1)+Double.parseDouble(num2);
  150. display.setText(""+total);
  151. usingFirst=false;
  152. num1=Double.toString(total);
  153. num2="";
  154.  
  155. }
  156. if(s.equals("c"))
  157. {
  158. display.setText("");
  159. usingFirst=true;
  160. num1="";
  161. num2="";
  162. }
  163. }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement