Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2014
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. public class Application extends JFrame {
  2. private static final long serialVersionUID = 1L;
  3.  
  4. private static final String TITLE = "Average Machine";
  5.  
  6. private static final int WIDTH = 320;
  7. private static final int HEIGHT = 125;
  8.  
  9. private JPanel panel;
  10. private JTextField field, infoField;
  11. private JButton btnAdd, btnClear, btnAvg;
  12. private JLabel label;
  13.  
  14. private int counter;
  15. private int index = 0;
  16. private double total = 0;
  17. private double ans = 0;
  18.  
  19. public double temps[] = new double[10000];
  20.  
  21. public Application() {
  22. initComponents();
  23. initLayout();
  24. }
  25.  
  26. private void initComponents() {
  27. //Fonts
  28. Font font0 = new Font("Arial", Font.PLAIN, 11);
  29. Font font1 = new Font("Arial", Font.PLAIN, 9);
  30.  
  31. panel = new JPanel();
  32.  
  33. // JButtons
  34. btnAdd = new JButton("Add");
  35. btnAdd.addActionListener(new ActionListener() {
  36. public void actionPerformed(ActionEvent e) {
  37. onClick();
  38. }
  39. });
  40.  
  41. btnAvg = new JButton("Average");
  42. btnAvg.addActionListener(new ActionListener() {
  43. public void actionPerformed(ActionEvent e) {
  44. average();
  45. }
  46. });
  47.  
  48. btnClear = new JButton("Clear");
  49. btnClear.addActionListener(new ActionListener() {
  50. public void actionPerformed(ActionEvent e) {
  51. clear();
  52. }
  53. });
  54.  
  55. btnAdd.setFont(font0);
  56. btnAvg.setFont(font0);
  57. btnClear.setFont(font0);
  58.  
  59. //Text Field
  60. field = new JTextField();
  61. field.setHorizontalAlignment(SwingConstants.CENTER);
  62. field.setColumns(38);
  63. field.addActionListener(new ActionListener() {
  64. public void actionPerformed(ActionEvent e) {
  65. onClick();
  66. }
  67. });
  68.  
  69. //Info field
  70. infoField = new JTextField();
  71. infoField.setEditable(false);
  72. infoField.setText("Start completed");
  73. infoField.setHorizontalAlignment(SwingConstants.CENTER);
  74. infoField.setColumns(38);
  75.  
  76. // Label
  77. label = new JLabel("All Rights Reserverd - v0.00");
  78. label.setFont(font1);
  79.  
  80. // Adding panel
  81. add(panel, BorderLayout.CENTER);
  82. }
  83.  
  84. private void onClick() {
  85. try {
  86. temps[index] = Double.parseDouble(field.getText());
  87. counter++;
  88. total += temps[index];
  89. infoField.setText(temps[index] + " added. " + "Amount: " + counter + ". Total: " + total);
  90. System.out.println("Total: " + total);
  91. System.out.println("Added: " + temps[index]);
  92. System.out.println("Counter: " + counter);
  93. System.out.println("Temp[" + index + "]: " + temps[index]);
  94. System.out.println("Index: " + index + "\n");
  95. field.setText("");
  96. field.requestFocusInWindow();
  97. } catch (NumberFormatException e) {
  98. infoField.setText("Error: Wrong character format");
  99. return;
  100. }
  101. index++;
  102. }
  103.  
  104. private void clear() {
  105. field.setText("");
  106. infoField.setText("Clear completed");
  107. counter = 0;
  108. total = 0;
  109. index = 0;
  110. System.out.println("Counter: " + counter);
  111. System.out.println("Index: " + index);
  112. System.out.println("Total: " + total + "\n");
  113. }
  114.  
  115. private void average() {
  116. double ans = total / index;
  117. infoField.setText("Average: " + ans + " | " + total + " / " + index + "");
  118. counter = 0;
  119. total = 0;
  120. index = 0;
  121. System.out.println("Counter: " + counter);
  122. System.out.println("Index: " + index);
  123. System.out.println("Total: " + total + "\n");
  124. }
  125.  
  126. private void initLayout() {
  127. //Adding all the components to the panel
  128. panel.add(infoField);
  129. panel.add(field);
  130. panel.add(btnAdd);
  131. panel.add(btnAvg);
  132. panel.add(btnClear);
  133. panel.add(label);
  134.  
  135. //setting the rules of the JFrame
  136. setTitle(Application.TITLE);
  137. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  138. setResizable(false);
  139. setSize(WIDTH, HEIGHT);
  140. setLocationRelativeTo(null);
  141. setVisible(true);
  142. field.requestFocusInWindow();
  143. }
  144.  
  145. public static void main(String[] args) {
  146. try {
  147. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  148. new Application();
  149. } catch (Exception e) {
  150. e.printStackTrace();
  151. }
  152.  
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement