Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.70 KB | None | 0 0
  1.  
  2. import javax.swing.JOptionPane;
  3.  
  4. /*
  5. * To change this template, choose Tools | Templates
  6. * and open the template in the editor.
  7. */
  8.  
  9. /*
  10. * CalculatorForm.java
  11. *
  12. * Created on Jun 30, 2011, 3:21:18 PM
  13. */
  14.  
  15. /**
  16. *
  17. * @author aaronmauer
  18. */
  19. public class CalculatorForm extends javax.swing.JFrame {
  20.  
  21. /** Creates new form CalculatorForm */
  22. public CalculatorForm() {
  23. initComponents();
  24. }
  25.  
  26. /** This method is called from within the constructor to
  27. * initialize the form.
  28. * WARNING: Do NOT modify this code. The content of this method is
  29. * always regenerated by the Form Editor.
  30. */
  31. @SuppressWarnings("unchecked")
  32. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  33. private void initComponents() {
  34.  
  35. firstNumberText = new javax.swing.JTextField();
  36. secondNumberText = new javax.swing.JTextField();
  37. jLabel1 = new javax.swing.JLabel();
  38. jLabel2 = new javax.swing.JLabel();
  39. plusbutton = new javax.swing.JButton();
  40. minusbutton = new javax.swing.JButton();
  41. answerLabel = new javax.swing.JLabel();
  42.  
  43. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  44. setTitle("Simple Calculator");
  45.  
  46. jLabel1.setText("First Number");
  47.  
  48. jLabel2.setText("Second Number");
  49.  
  50. plusbutton.setText("+");
  51. plusbutton.setToolTipText("Add");
  52. plusbutton.addActionListener(new java.awt.event.ActionListener() {
  53. public void actionPerformed(java.awt.event.ActionEvent evt) {
  54. plusbuttonActionPerformed(evt);
  55. }
  56. });
  57.  
  58. minusbutton.setText("-");
  59. minusbutton.setToolTipText("Subtract");
  60. minusbutton.addActionListener(new java.awt.event.ActionListener() {
  61. public void actionPerformed(java.awt.event.ActionEvent evt) {
  62. minusbuttonActionPerformed(evt);
  63. }
  64. });
  65.  
  66. answerLabel.setFont(new java.awt.Font("Stone Sans Sem ITC TT", 1, 24)); // NOI18N
  67. answerLabel.setText("The Answer is:");
  68.  
  69. org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
  70. getContentPane().setLayout(layout);
  71. layout.setHorizontalGroup(
  72. layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  73. .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
  74. .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
  75. .add(layout.createSequentialGroup()
  76. .addContainerGap(76, Short.MAX_VALUE)
  77. .add(firstNumberText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 392, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
  78. .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
  79. .add(76, 76, 76)
  80. .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  81. .add(secondNumberText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
  82. .add(layout.createSequentialGroup()
  83. .add(plusbutton)
  84. .add(28, 28, 28)
  85. .add(minusbutton))
  86. .add(answerLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE))))
  87. .add(59, 59, 59))
  88. .add(layout.createSequentialGroup()
  89. .add(49, 49, 49)
  90. .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  91. .add(jLabel2)
  92. .add(jLabel1))
  93. .addContainerGap(379, Short.MAX_VALUE))
  94. );
  95. layout.setVerticalGroup(
  96. layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  97. .add(layout.createSequentialGroup()
  98. .add(42, 42, 42)
  99. .add(jLabel1)
  100. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
  101. .add(firstNumberText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  102. .add(11, 11, 11)
  103. .add(jLabel2)
  104. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
  105. .add(secondNumberText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  106. .add(18, 18, 18)
  107. .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
  108. .add(minusbutton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  109. .add(plusbutton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
  110. .add(18, 18, 18)
  111. .add(answerLabel)
  112. .addContainerGap(83, Short.MAX_VALUE))
  113. );
  114.  
  115. pack();
  116. }// </editor-fold>
  117.  
  118. private void plusbuttonActionPerformed(java.awt.event.ActionEvent evt) {
  119. int number1,;
  120. try (
  121. number1 = Integer.parseInt(
  122. this.firstNumberText.getText());
  123. )
  124. catch (Exception e) (
  125. JOptionPane.showMessageDialog(this, "Bad First Number", "Error", JOptionPane.ERROR_MESSAGE,) ;
  126. return;
  127. int number2;
  128. try (
  129. number2 = Integer.parseInt(
  130. this.secondNumberText.getText());
  131. )
  132. catch (Exception e) (
  133. JOptionPane.showMessageDialog(this, "Bad Second Number", "Error", JOptionPane.ERROR_MESSAGE,) ;
  134. return;
  135.  
  136. }
  137.  
  138. private void minusbuttonActionPerformed(java.awt.event.ActionEvent evt) {
  139. int number1, ;
  140. try
  141. (
  142. number1 = Integer.parseInt(
  143. this.firstNumberText.getText());
  144. )
  145. catch (Exception e)
  146. (
  147. JOptionPane.showMessageDialog(this, "Bad First Number", "Error", JOptionPane.ERROR_MESSAGE,) ;
  148. return;
  149. int number2;
  150. try
  151. (
  152. number2 = Integer.parseInt(
  153. this.secondNumberText.getText());
  154. )
  155. catch (Exception e)
  156. (
  157. JOptionPane.showMessageDialog(this, "Bad Second Number", "Error", JOptionPane.ERROR_MESSAGE,) ;
  158. return;
  159. }
  160. int answer = number1 + number2;
  161. this.answerLabel.setText(
  162. "The Answer is: " - answer);
  163. }
  164. /**
  165. * @param args the command line arguments
  166. */
  167. public static void main(String args[]) {
  168. java.awt.EventQueue.invokeLater(new Runnable() {
  169. public void run() {
  170. new CalculatorForm().setVisible(true);
  171. }
  172. });
  173. }
  174.  
  175. // Variables declaration - do not modify
  176. private javax.swing.JLabel answerLabel;
  177. private javax.swing.JTextField firstNumberText;
  178. private javax.swing.JLabel jLabel1;
  179. private javax.swing.JLabel jLabel2;
  180. private javax.swing.JButton minusbutton;
  181. private javax.swing.JButton plusbutton;
  182. private javax.swing.JTextField secondNumberText;
  183. // End of variables declaration
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement