Advertisement
lashrone1

calculator

Oct 17th, 2019
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.58 KB | None | 0 0
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3.  
  4. import javax.swing.JButton;
  5. import javax.swing.JFrame;
  6. import javax.swing.JLabel;
  7. import javax.swing.JTextArea;
  8. import javax.swing.SwingUtilities;
  9. class Calculator {
  10.  
  11. private JFrame frame = new JFrame("Calculator");
  12. private JTextArea txtScreen = new JTextArea();
  13.  
  14.  
  15. //Calculation Variables:
  16. String strNum1 = "";
  17. String strNum2 = "";
  18. String[] statement;
  19. double num1 = 0;
  20. double num2 = 0;
  21. String strTotal = "";
  22. double totalVal = 0;
  23.  
  24. private JButton btnEqual = new JButton("=");
  25.  
  26. //Number Buttons:
  27. private JButton btn0 = new JButton("0");
  28. private JButton btn1 = new JButton("1");
  29. private JButton btn2 = new JButton("2");
  30. private JButton btn3 = new JButton("3");
  31. private JButton btn4 = new JButton("4");
  32. private JButton btn5 = new JButton("5");
  33. private JButton btn6 = new JButton("6");
  34. private JButton btn7 = new JButton("7");
  35. private JButton btn8 = new JButton("8");
  36. private JButton btn9 = new JButton("9");
  37.  
  38. //Operator Buttons:
  39. private JButton btnDiv = new JButton("/");
  40. private JButton btnMul = new JButton("*");
  41. private JButton btnSub = new JButton("-");
  42. private JButton btnAdd = new JButton("+");
  43. private JButton btnDec = new JButton(".");
  44. private JButton btnPower = new JButton("^");
  45.  
  46. public Calculator() {
  47. //Frame Attributes:
  48. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  49. frame.setVisible(true);
  50. frame.setSize(400, 560);
  51. frame.setResizable(false);
  52. frame.setLayout(null);
  53.  
  54. //txtScreen Attributes:
  55. txtScreen.setSize(380, 150);
  56. txtScreen.setLocation(7, 15);
  57. txtScreen.setEditable(false);
  58.  
  59. //btn0:
  60. btn0.setSize(80, 50);
  61. btn0.setLocation(105, 470);
  62. btn0.addActionListener(new ActionListener(){
  63. public void actionPerformed(ActionEvent arg0) {
  64. txtScreen.append("0");
  65. }
  66. });
  67.  
  68. //btn1:
  69. btn1.setSize(80, 50);
  70. btn1.setLocation(10, 395);
  71. btn1.addActionListener(new ActionListener(){
  72. public void actionPerformed(ActionEvent arg0) {
  73. txtScreen.append("1");
  74. }
  75. });
  76.  
  77. //btn2:
  78. btn2.setSize(80, 50);
  79. btn2.setLocation(105, 395);
  80. btn2.addActionListener(new ActionListener(){
  81. public void actionPerformed(ActionEvent arg0) {
  82. txtScreen.append("2");
  83. }
  84. });
  85.  
  86. //btn3:
  87. btn3.setSize(80, 50);
  88. btn3.setLocation(200, 395);
  89. btn3.addActionListener(new ActionListener(){
  90. public void actionPerformed(ActionEvent arg0) {
  91. txtScreen.append("3");
  92. }
  93. });
  94.  
  95. //btn4:
  96. btn4.setSize(80, 50);
  97. btn4.setLocation(10, 310);
  98. btn4.addActionListener(new ActionListener(){
  99. public void actionPerformed(ActionEvent arg0) {
  100. txtScreen.append("4");
  101. }
  102. });
  103.  
  104. //btn5:
  105. btn5.setSize(80, 50);
  106. btn5.setLocation(105, 310);
  107. btn5.addActionListener(new ActionListener(){
  108. public void actionPerformed(ActionEvent arg0) {
  109. txtScreen.append("5");
  110. }
  111. });
  112.  
  113. //btn6:
  114. btn6.setSize(80, 50);
  115. btn6.setLocation(200, 310);
  116. btn6.addActionListener(new ActionListener(){
  117. public void actionPerformed(ActionEvent arg0) {
  118. txtScreen.append("6");
  119. }
  120. });
  121.  
  122. //btn7:
  123. btn7.setSize(80, 50);
  124. btn7.setLocation(10, 230);
  125. btn7.addActionListener(new ActionListener(){
  126. public void actionPerformed(ActionEvent arg0) {
  127. txtScreen.append("7");
  128. }
  129. });
  130.  
  131. //btn8:
  132. btn8.setSize(80, 50);
  133. btn8.setLocation(105, 230);
  134. btn8.addActionListener(new ActionListener(){
  135. public void actionPerformed(ActionEvent arg0) {
  136. txtScreen.append("8");
  137. }
  138. });
  139.  
  140. //btn9:
  141. btn9.setSize(80, 50);
  142. btn9.setLocation(200, 230);
  143. btn9.addActionListener(new ActionListener(){
  144. public void actionPerformed(ActionEvent arg0) {
  145. txtScreen.append("9");
  146. }
  147. });
  148.  
  149. //btnDiv:
  150. btnDiv.setSize(70, 60);
  151. btnDiv.setLocation(310, 180);
  152. btnDiv.addActionListener(new ActionListener(){
  153. public void actionPerformed(ActionEvent arg0) {
  154. txtScreen.append("/");
  155. }
  156. });
  157.  
  158. //btnMul:
  159. btnMul.setSize(70, 60);
  160. btnMul.setLocation(310, 250);
  161. btnMul.addActionListener(new ActionListener(){
  162. public void actionPerformed(ActionEvent arg0) {
  163. txtScreen.append("*");
  164. }
  165. });
  166.  
  167. //btnSub
  168. btnSub.setSize(70, 60);
  169. btnSub.setLocation(310, 320);
  170. btnSub.addActionListener(new ActionListener(){
  171. public void actionPerformed(ActionEvent arg0) {
  172. txtScreen.append("-");
  173. }
  174. });
  175.  
  176. //btnAdd:
  177. btnAdd.setSize(70, 60);
  178. btnAdd.setLocation(310, 390);
  179. btnAdd.addActionListener(new ActionListener(){
  180. public void actionPerformed(ActionEvent arg0) {
  181. txtScreen.append("+");
  182. }
  183. });
  184.  
  185. //btnDec:
  186. btnDec.setSize(80, 50);
  187. btnDec.setLocation(200, 470);
  188. btnDec.addActionListener(new ActionListener(){
  189. public void actionPerformed(ActionEvent arg0) {
  190. txtScreen.append(".");
  191. }
  192. });
  193.  
  194. //btnPower:
  195. btnPower.setSize(80, 50);
  196. btnPower.setLocation(10, 470);
  197. btnPower.addActionListener(new ActionListener(){
  198. public void actionPerformed(ActionEvent arg0) {
  199. txtScreen.append("^");
  200. }
  201. });
  202.  
  203. //btnEqual:
  204. btnEqual.setSize(70, 60);
  205. btnEqual.setLocation(310, 460);
  206. btnEqual.addActionListener(new ActionListener(){
  207. public void actionPerformed(ActionEvent arg0) {
  208. if(txtScreen.getText().contains("+")){
  209. statement = txtScreen.getText().split("\\+");
  210.  
  211. strNum1 = statement[0];
  212. strNum2 = statement[1];
  213.  
  214. num1 = Double.parseDouble(strNum1);
  215. num2 = Double.parseDouble(strNum2);
  216.  
  217. totalVal = num1 + num2;
  218.  
  219. strTotal = Double.toString(totalVal);
  220.  
  221. txtScreen.setText(strTotal);
  222. }
  223. else if(txtScreen.getText().contains("-")){
  224. statement = txtScreen.getText().split("\\-");
  225.  
  226. strNum1 = statement[0];
  227. strNum2 = statement[1];
  228.  
  229. num1 = Integer.parseInt(strNum1);
  230. num2 = Integer.parseInt(strNum2);
  231.  
  232. totalVal = num1 - num2;
  233.  
  234. strTotal = Double.toString(totalVal);
  235.  
  236. txtScreen.setText(strTotal);
  237. }
  238. else if(txtScreen.getText().contains("/")){
  239. statement = txtScreen.getText().split("\\/");
  240.  
  241. strNum1 = statement[0];
  242. strNum2 = statement[1];
  243.  
  244. num1 = Double.parseDouble(strNum1);
  245. num2 = Double.parseDouble(strNum2);
  246.  
  247. totalVal = num1 / num2;
  248.  
  249. strTotal = Double.toString(totalVal);
  250.  
  251. txtScreen.setText(strTotal);
  252. }
  253. else if(txtScreen.getText().contains("*")){
  254. statement = txtScreen.getText().split("\\*");
  255.  
  256. strNum1 = statement[0];
  257. strNum2 = statement[1];
  258.  
  259. num1 = Double.parseDouble(strNum1);
  260. num2 = Double.parseDouble(strNum2);
  261.  
  262. totalVal = num1 * num2;
  263.  
  264. strTotal = Double.toString(totalVal);
  265.  
  266. txtScreen.setText(strTotal);
  267. }
  268. else if(txtScreen.getText().contains("^")){
  269. statement = txtScreen.getText().split("\\^");
  270.  
  271. strNum1 = statement[0];
  272. strNum2 = statement[1];
  273.  
  274. num1 = Double.parseDouble(strNum1);
  275. num2 = Double.parseDouble(strNum2);
  276.  
  277. totalVal = 1;
  278. for(int i = 0; i < num2; i++){
  279. totalVal *= num1;
  280. }
  281.  
  282. strTotal = Double.toString(totalVal);
  283.  
  284. txtScreen.setText(strTotal);
  285. }
  286. }
  287. });
  288.  
  289. //Add Objects To Screen:
  290. frame.add(txtScreen);
  291. frame.add(btn0);
  292. frame.add(btn1);
  293. frame.add(btn2);
  294. frame.add(btn3);
  295. frame.add(btn4);
  296. frame.add(btn5);
  297. frame.add(btn6);
  298. frame.add(btn7);
  299. frame.add(btn8);
  300. frame.add(btn9);
  301.  
  302. frame.add(btnDiv);
  303. frame.add(btnMul);
  304. frame.add(btnSub);
  305. frame.add(btnAdd);
  306. frame.add(btnDec);
  307. frame.add(btnPower);
  308.  
  309. frame.add(btnEqual);
  310.  
  311. SwingUtilities.updateComponentTreeUI(frame);
  312. }
  313.  
  314. public static void main(String[] args) {
  315. new Calculator();
  316. }
  317.  
  318.  
  319.  
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement