Advertisement
Crenox

Winston Tutorial 13-16 Calculator

Jul 27th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. package com.samkough.main;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. @SuppressWarnings("serial")
  8. public class Win extends JFrame implements ActionListener
  9. {
  10. private static final int WIDTH = 250;
  11. private static final int HEIGHT = 175;
  12.  
  13. JButton add, subtract, multiply, divide, sqrt;
  14. JTextField num1, num2;
  15. JLabel result, enter1, enter2;
  16.  
  17. public Win()
  18. {
  19. // this layout allows you to create a layout anyway you want basically
  20. setLayout(new GridBagLayout());
  21. // this will be for padding and whatnot
  22. GridBagConstraints c = new GridBagConstraints();
  23.  
  24. enter1 = new JLabel("1st: ");
  25. c.fill = GridBagConstraints.HORIZONTAL;
  26. c.gridx = 0;
  27. c.gridy = 0;
  28. add(enter1, c);
  29.  
  30. num1 = new JTextField(10);
  31. c.fill = GridBagConstraints.HORIZONTAL;
  32. c.gridx = 1;
  33. c.gridy = 0;
  34. c.gridwidth = 3;
  35. add(num1, c);
  36.  
  37. enter2 = new JLabel("2nd: ");
  38. c.fill = GridBagConstraints.HORIZONTAL;
  39. c.gridx = 0;
  40. c.gridy = 1;
  41. c.gridwidth = 1;
  42. add(enter2, c);
  43.  
  44. num2 = new JTextField(10);
  45. c.fill = GridBagConstraints.HORIZONTAL;
  46. c.gridx = 1;
  47. c.gridy = 1;
  48. c.gridwidth = 3;
  49. add(num2, c);
  50.  
  51. add = new JButton("+");
  52. c.fill = GridBagConstraints.HORIZONTAL;
  53. c.gridx = 0;
  54. c.gridy = 2;
  55. c.gridwidth = 1;
  56. add(add, c);
  57.  
  58. subtract = new JButton("-");
  59. c.fill = GridBagConstraints.HORIZONTAL;
  60. c.gridx = 1;
  61. c.gridy = 2;
  62. add(subtract, c);
  63.  
  64. multiply = new JButton("*");
  65. c.fill = GridBagConstraints.HORIZONTAL;
  66. c.gridx = 2;
  67. c.gridy = 2;
  68. add(multiply, c);
  69.  
  70. divide = new JButton("/");
  71. c.fill = GridBagConstraints.HORIZONTAL;
  72. c.gridx = 3;
  73. c.gridy = 2;
  74. add(divide, c);
  75.  
  76. result = new JLabel("");
  77. c.fill = GridBagConstraints.HORIZONTAL;
  78. c.gridx = 0;
  79. c.gridy = 4;
  80. c.gridwidth = 4;
  81. add(result, c);
  82.  
  83. add.addActionListener(this);
  84. subtract.addActionListener(this);
  85. multiply.addActionListener(this);
  86. divide.addActionListener(this);
  87. }
  88.  
  89. public void actionPerformed(ActionEvent e)
  90. {
  91. double n1, n2;
  92.  
  93. try
  94. {
  95. n1 = Double.parseDouble(num1.getText());
  96. }
  97. catch(NumberFormatException e1)
  98. {
  99. result.setText("Illegal character for 1st textbox!");
  100. result.setForeground(Color.RED);
  101. return;
  102. }
  103.  
  104. try
  105. {
  106. n2 = Double.parseDouble(num2.getText());
  107. }
  108. catch(NumberFormatException e2)
  109. {
  110. result.setText("Illegal character for 2nd textbox!");
  111. result.setForeground(Color.RED);
  112. return;
  113. }
  114.  
  115. // this is gonna help validate what button the user presses
  116. String op = e.getActionCommand();
  117.  
  118. // this is the math logic for the symbols
  119. if (op.equals("+"))
  120. {
  121. double sum = n1 + n2;
  122. result.setText(n1 + " + " + n2 + " = " + sum);
  123. result.setForeground(Color.BLUE);
  124. }
  125. else if (op.equals("-"))
  126. {
  127. double difference = n1 - n2;
  128. result.setText(n1 + " - " + n2 + " = " + difference);
  129. result.setForeground(Color.BLUE);
  130. }
  131. else if (op.equals("*"))
  132. {
  133. double product = n1 * n2;
  134. result.setText(n1 + " * " + n2 + " = " + product);
  135. result.setForeground(Color.BLUE);
  136. }
  137. else if (op.equals("/"))
  138. {
  139. // we make an if because you can't divide by zero
  140. if (n2 == 0)
  141. {
  142. result.setText("Cannot divide by zero!");
  143. result.setForeground(Color.RED);
  144. }
  145. else
  146. {
  147. double quotient = n1 / n2;
  148. result.setText(n1 + " / " + n2 + " = " + quotient);
  149. result.setForeground(Color.BLUE);
  150. }
  151. }
  152. }
  153.  
  154. public static void main(String args[])
  155. {
  156. Win frame = new Win();
  157.  
  158. frame.setVisible(true);
  159. frame.setSize(WIDTH, HEIGHT);
  160. // frame.pack();
  161. frame.setTitle("Calculator");
  162. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  163. frame.setLocationRelativeTo(null);
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement