Guest User

Untitled

a guest
Jan 23rd, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.64 KB | None | 0 0
  1. // Exercise 9.13: ArithmeticCalculator.java
  2. // This application accepts two operands and allows
  3. // the user to perform multiplication or addition.
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import javax.swing.*;
  7.  
  8. public class ArithmeticCalculator extends JFrame
  9. {
  10. // JLabel and JTextArea for operands entered by user
  11. private JLabel operandsJLabel;
  12. private JTextArea operandsJTextArea;
  13.  
  14. // JLabel and JTextField displays result of calculation
  15. private JLabel resultJLabel;
  16. private JTextField resultJTextField;
  17.  
  18. // JButton initiates asking user for operands
  19. private JButton enterOperandsJButton;
  20.  
  21. // JButtons initiate adding and/or multiplying operands
  22. private JButton addJButton;
  23. private JButton multiplyJButton;
  24.  
  25. private double value1 = 0; // holds the first value entered
  26. private double value2 = 0; // holds the second value entered
  27.  
  28. // no-argument constructor
  29. public ArithmeticCalculator()
  30. {
  31. createUserInterface();
  32. }
  33.  
  34. // create and position GUI components; register event handlers
  35. private void createUserInterface()
  36. {
  37. // get content pane for attaching GUI components
  38. Container contentPane = getContentPane();
  39.  
  40. // enable explicit positioning of GUI components
  41. contentPane.setLayout( null );
  42.  
  43. // set up operandsJLabel
  44. operandsJLabel = new JLabel();
  45. operandsJLabel.setBounds( 16, 8, 100, 16 );
  46. operandsJLabel.setText( "Operands:" );
  47. contentPane.add( operandsJLabel );
  48.  
  49. // set up operandsJTextArea
  50. operandsJTextArea = new JTextArea();
  51. operandsJTextArea.setBounds( 16, 27, 104, 32 );
  52. operandsJTextArea.setEditable( false );
  53. contentPane.add( operandsJTextArea );
  54.  
  55. // set up resultJLabel
  56. resultJLabel = new JLabel();
  57. resultJLabel.setBounds( 16, 64, 53, 23 );
  58. resultJLabel.setText( "Result:" );
  59. contentPane.add( resultJLabel );
  60.  
  61. // set up resultJTextField
  62. resultJTextField = new JTextField();
  63. resultJTextField.setBounds( 16, 91, 104, 26 );
  64. resultJTextField.setEditable( false );
  65. resultJTextField.setHorizontalAlignment( JTextField.CENTER );
  66. contentPane.add( resultJTextField );
  67.  
  68. // set up enterOperandsJButton
  69. enterOperandsJButton = new JButton();
  70. enterOperandsJButton.setBounds( 140, 27, 125, 26 );
  71. enterOperandsJButton.setText( "Enter Operands" );
  72. contentPane.add( enterOperandsJButton );
  73. enterOperandsJButton.addActionListener(
  74.  
  75. new ActionListener() // anonymous inner class
  76. {
  77. // event handler called when enterOperandsJButton pressed
  78. public void actionPerformed( ActionEvent event )
  79. {
  80. enterOperandsJButtonActionPerformed( event );
  81. }
  82.  
  83. } // end anonymous inner class
  84.  
  85. ); // end call to addActionListener
  86.  
  87. // set up addJButton
  88. addJButton = new JButton();
  89. addJButton.setBounds( 140, 59, 125, 26 );
  90. addJButton.setText( "Add" );
  91. addJButton.setEnabled( false );
  92. contentPane.add( addJButton );
  93. addJButton.addActionListener(
  94.  
  95. new ActionListener() // anonymous inner class
  96. {
  97. // event handler called when addJButton is pressed
  98. public void actionPerformed( ActionEvent event )
  99. {
  100. addJButtonActionPerformed( event );
  101. }
  102.  
  103. } // end anonymous inner class
  104.  
  105. ); // end call to addActionListener
  106.  
  107. // set up multiplyJButton
  108. multiplyJButton = new JButton();
  109. multiplyJButton.setBounds( 140, 91, 125, 26 );
  110. multiplyJButton.setText( "Multiply" );
  111. multiplyJButton.setEnabled( false );
  112. contentPane.add( multiplyJButton );
  113. multiplyJButton.addActionListener(
  114.  
  115. new ActionListener() // anonymous inner class
  116. {
  117. // event handler called when multiplyJButton is pressed
  118. public void actionPerformed( ActionEvent event )
  119. {
  120. multiplyJButtonActionPerformed( event );
  121. }
  122.  
  123. } // end anonymous inner class
  124.  
  125. ); // end call to addActionListener
  126.  
  127. // set properties of application's window
  128. setTitle( "Arithmetic Calculator" ); // set title bar text
  129. setSize( 290, 159 ); // set window size
  130. setVisible( true ); // display window
  131.  
  132. } // end method createUserInterface
  133.  
  134. // store and display input
  135. private void enterOperandsJButtonActionPerformed(
  136. ActionEvent event )
  137. {
  138. int counter = 1;
  139. String input;
  140.  
  141. // remove previous data
  142. operandsJTextArea.setText( "" );
  143. resultJTextField.setText( "" );
  144.  
  145. do // loop twice, once for each operand
  146. {
  147. input = JOptionPane.showInputDialog( null, "Enter Operand");
  148.  
  149. // convert input
  150. if ( counter == 1 )
  151. {
  152. value1 = Double.parseDouble(input);
  153. }
  154. else
  155. {
  156. value2 = Double.parseDouble(input);
  157. }
  158. counter++; //increment counter
  159. }
  160. while ( counter<=2);
  161.  
  162. // display operands
  163. operandsJTextArea.setText( value1 + "\n" + value2 );
  164.  
  165. // allow user to click calculate JButtons
  166. addJButton.setEnabled( true );
  167. multiplyJButton.setEnabled( true );
  168.  
  169. //transfer focus to addJButton
  170. addJButton.requestFocusInWindow();
  171.  
  172. } // end method enterOperandsJButtonActionPerformed
  173.  
  174. // add and display result
  175. private void addJButtonActionPerformed( ActionEvent event )
  176. {
  177. double result = value1 + value2;
  178. resultJTextField.setText( String.valueOf( result ) );
  179.  
  180. //prevent user from adding until new values entered
  181. addJButton.setEnabled(false);
  182. } // end method addJButtonActionPerformed
  183.  
  184. // multiply and display result
  185. private void multiplyJButtonActionPerformed( ActionEvent event )
  186. {
  187. double result = value1 * value2;
  188. resultJTextField.setText( String.valueOf( result ) );
  189.  
  190. //prevent user from multiplying until new values entered
  191. multiplyJButton.setEnabled(false);
  192. } // end method multiplyJButtonActionPerformed
  193.  
  194. // main method
  195. public static void main( String[] args )
  196. {
  197. ArithmeticCalculator application = new ArithmeticCalculator();
  198. application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  199.  
  200. } // end method main
  201.  
  202. } // end class ArithmeticCalculator
  203.  
  204. /**************************************************************************
  205. * (C) Copyright 1992-2004 by Deitel & Associates, Inc. and *
  206. * Pearson Education, Inc. All Rights Reserved. *
  207. * *
  208. * DISCLAIMER: The authors and publisher of this book have used their *
  209. * best efforts in preparing the book. These efforts include the *
  210. * development, research, and testing of the theories and programs *
  211. * to determine their effectiveness. The authors and publisher make *
  212. * no warranty of any kind, expressed or implied, with regard to these *
  213. * programs or to the documentation contained in these books. The authors *
  214. * and publisher shall not be liable in any event for incidental or *
  215. * consequential damages in connection with, or arising out of, the *
  216. * furnishing, performance, or use of these programs. *
  217. **************************************************************************/
Add Comment
Please, Sign In to add comment