Advertisement
Guest User

Untitled

a guest
Jul 1st, 2013
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.text.*;
  5. import java.awt.Color;
  6.  
  7. public class GradeCalculator extends JFrame
  8. {
  9.  
  10. JLabel gradeJLabel;
  11. JTextField gradeJTextField;
  12.  
  13. JLabel clickstartJLabel;
  14.  
  15. JButton enterJButton;
  16. JButton clearJButton;
  17. JButton closeJButton;
  18.  
  19. String testscores;
  20. int numberOfScores;
  21.  
  22. double priceValues;
  23.  
  24. double grade;
  25. int counter;
  26. int costAccumulator;
  27.  
  28.  
  29. public GradeCalculator()
  30. {
  31. createUserInterface();
  32. }
  33.  
  34. public void createUserInterface()
  35. {
  36. Container contentPane = getContentPane();
  37. contentPane.setBackground(Color.WHITE);
  38. contentPane.setLayout(null);
  39.  
  40. gradeJLabel = new JLabel();
  41. gradeJLabel.setBounds(50, 50, 150, 20);
  42. gradeJLabel.setFont(new Font("Default", Font.PLAIN, 12));
  43. gradeJLabel.setText("Grade:");
  44. gradeJLabel.setForeground(Color.BLACK);
  45. gradeJLabel.setHorizontalAlignment(JLabel.CENTER);
  46. contentPane.add(gradeJLabel);
  47.  
  48. gradeJTextField = new JTextField();
  49. gradeJTextField.setBounds(200, 50, 50, 20);
  50. gradeJTextField.setFont(new Font("Default", Font.PLAIN, 12));
  51. gradeJTextField.setHorizontalAlignment(JTextField.CENTER);
  52. gradeJTextField.setForeground(Color.BLACK);
  53. gradeJTextField.setBackground(Color.WHITE);
  54. gradeJTextField.setEditable(false);
  55. contentPane.add(gradeJTextField);
  56.  
  57. clickstartJLabel = new JLabel();
  58. clickstartJLabel.setBounds(100, 200, 150, 20);
  59. clickstartJLabel.setFont(new Font("Default", Font.PLAIN, 12));
  60. clickstartJLabel.setText("Click Start to Begin!");
  61. clickstartJLabel.setForeground(Color.BLACK);
  62. clickstartJLabel.setHorizontalAlignment(JLabel.CENTER);
  63. contentPane.add(clickstartJLabel);
  64.  
  65. enterJButton = new JButton();
  66. enterJButton.setBounds(20, 300, 100, 20);
  67. enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
  68. enterJButton.setText("Enter");
  69. enterJButton.setForeground(Color.BLACK);
  70. enterJButton.setBackground(Color.WHITE);
  71. contentPane.add(enterJButton);
  72. enterJButton.addActionListener(
  73.  
  74. new ActionListener()
  75. {
  76. public void actionPerformed(ActionEvent event)
  77. {
  78. enterJButtonActionPerformed(event);
  79. }
  80. }
  81. );
  82.  
  83. clearJButton = new JButton();
  84. clearJButton.setBounds(140, 300, 100, 20);
  85. clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
  86. clearJButton.setText("Clear");
  87. clearJButton.setForeground(Color.BLACK);
  88. clearJButton.setBackground(Color.WHITE);
  89. contentPane.add(clearJButton);
  90. clearJButton.addActionListener(
  91.  
  92. new ActionListener()
  93. {
  94. public void actionPerformed(ActionEvent event)
  95. {
  96. clearJButtonActionPerformed(event);
  97. }
  98. }
  99. );
  100.  
  101. closeJButton = new JButton();
  102. closeJButton.setBounds(260, 300, 100, 20);
  103. closeJButton.setFont(new Font("Default", Font.PLAIN, 12));
  104. closeJButton.setText("Close");
  105. closeJButton.setForeground(Color.BLACK);
  106. closeJButton.setBackground(Color.WHITE);
  107. contentPane.add(closeJButton);
  108. closeJButton.addActionListener(
  109.  
  110. new ActionListener()
  111. {
  112. public void actionPerformed(ActionEvent event)
  113. {
  114. closeJButtonActionPerformed(event);
  115. }
  116. }
  117. );
  118.  
  119.  
  120. setTitle("Grade Calculator");
  121. setSize(400, 400);
  122. setVisible(true);
  123. }
  124.  
  125. /* main method */
  126. public static void main(String[] args)
  127. {
  128. GradeCalculator application = new GradeCalculator();
  129. application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  130. }
  131.  
  132. public void enterJButtonActionPerformed(ActionEvent event)
  133. {
  134. getnumberoftests();
  135. }
  136.  
  137. public void getnumberoftests()
  138. {
  139. testscores = JOptionPane.showInputDialog("Enter Number of Grades");
  140. numberOfScores = Integer.parseInt(testscores);
  141. gettestscores();
  142. }
  143.  
  144. public void gettestscores()
  145. {
  146. for(counter = 0; counter < numberOfScores; counter++)
  147. {
  148. testscores = JOptionPane.showInputDialog("Enter Scores");
  149. priceValues = Integer.parseInt(testscores);
  150. costAccumulator += priceValues;
  151. }
  152.  
  153. calculateAverage();
  154. }
  155.  
  156. public void calculateAverage()
  157. {
  158. grade = costAccumulator / numberOfScores;
  159. }
  160.  
  161.  
  162. public void checkgrade()
  163. {
  164.  
  165. if(grade < 64)
  166. {
  167. gradeJTextField.setText("F");
  168.  
  169. }
  170. else if(grade < 69)
  171. {
  172. gradeJTextField.setText("D");
  173. }
  174. else if(grade < 79)
  175.  
  176. {
  177. gradeJTextField.setText("C");
  178. }
  179. else if(grade < 89)
  180.  
  181. {
  182. gradeJTextField.setText("B");
  183. }
  184.  
  185. else {
  186.  
  187. gradeJTextField.setText("A");
  188. }
  189.  
  190. }
  191.  
  192. public void clearJButtonActionPerformed(ActionEvent event)
  193. {
  194. gradeJTextField.setText("");
  195. }
  196.  
  197. public void closeJButtonActionPerformed(ActionEvent event)
  198. {
  199. GradeCalculator.this.dispose();
  200. }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement