943,935 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1347
  • Java RSS
Nov 9th, 2006
0

Java Noob

Expand Post »
Hello, I'm a bit new to Java and I'm wondering how to do arrays with JTextField, so how would i go about starting this program here..

Build a GUI to accept integers (when the ENTER key is depressed) into an array of 15 int; have a button called AVERAGE; when AVERAGE is selected, there is a display of the count and the current average; have a button called CLEAR; when CLEAR is selected, count is set to 0 and so are all the array elements and then there is another display: count is 0 and average is 0.0.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Santa_Laws is offline Offline
2 posts
since Nov 2006
Nov 10th, 2006
0

Re: Java Noob

The first step is to create a GUI which has what you need.
Then you need to add some listeners to those buttons and keypresses you want the program to respond to.
Then you need to add the programming to those listeners so that the program will do what you intend it to do. In this case add the integers into an array, calculate and display the average, and clear the array.

If you get any errors, feel free to post your code (remember to wrap it in [code]code goes here[/code] tags) and the text of the errors.
Reputation Points: 22
Solved Threads: 11
Posting Whiz in Training
DavidRyan is offline Offline
229 posts
since Jul 2006
Nov 10th, 2006
0

Re: Java Noob

Thanks David,

I'm too just learning the ropes with Java. I appreciate your introduction to the subject.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fireworks-style is offline Offline
15 posts
since Jun 2006
Nov 10th, 2006
0

Re: Java Noob

Java Syntax (Toggle Plain Text)
  1. package argui;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. /**
  6.  * <p>Title: </p>
  7.  *
  8.  * <p>Description: </p>
  9.  *
  10.  * <p>Copyright: Copyright (c) 2006</p>
  11.  *
  12.  * <p>Company: </p>
  13.  *
  14.  * @author not attributable
  15.  * @version 1.0
  16.  */
  17. public class ArrayGUIPanel extends JPanel
  18. {
  19. private JLabel inputLabel, outputLabel, resultLabel, countLabel, averageLabel;
  20.  
  21. private JTextField nums;
  22. private JButton average, clear;
  23. private int count;
  24. private double avg;
  25. //-----------------------------------------------------------------
  26. // Constructor: Sets up the main GUI components.
  27. //-----------------------------------------------------------------
  28. public ArrayGUIPanel()
  29. {
  30.  
  31. count = 0;
  32. avg=0.0;
  33.  
  34. inputLabel = new JLabel ("Enter integers: " );
  35. resultLabel = new JLabel ("---");
  36. countLabel = new JLabel ("Count: " + count);
  37. averageLabel = new JLabel ("Average: " + avg);
  38.  
  39.  
  40.  
  41. average = new JButton ("AVERAGE");
  42. clear = new JButton ("CLEAR");
  43. AvgListener listener = new AvgListener();
  44. average.addActionListener (listener);
  45. clear.addActionListener(listener);
  46. add (inputLabel);
  47. add (average);
  48. add (clear);
  49. add (resultLabel);
  50. add (countLabel);
  51. add (averageLabel);
  52. setPreferredSize (new Dimension(300, 75));
  53. setBackground (Color.white);
  54.  
  55. }
  56.  
  57. //*****************************************************************
  58. // Represents an action listener for the average input field.
  59. //*****************************************************************
  60. private class AvgListener implements ActionListener
  61. {
  62. //--------------------------------------------------------------
  63. // Performs the conversion when the enter key is pressed in
  64. // the text field.
  65. //--------------------------------------------------------------
  66. public void actionPerformed (ActionEvent event)
  67. {
  68. int numsTemp, averageTemp;
  69. String text = nums.getText();


okay am i going in the right direction here? how does the array come in?
Last edited by Santa_Laws; Nov 10th, 2006 at 1:04 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Santa_Laws is offline Offline
2 posts
since Nov 2006
Nov 10th, 2006
0

Re: Java Noob

The first thing to do is NOT to build that GUI but to figure out what you're actually trying to do.

Without knowing that you can never expect to create a user interface which actually makes sense, let alone the code behind it to work with the data entered and produce the data you want to produce.

Your array is meant to store numbers entered into the user interface for later calculation of their average.
A rather limited implementation, but I guess you haven't come to the point of learning about more appropriate data structures yet so it'll have to do for now.
Just make sure you have some checks on the number of inputs in there so you don't try to insert more than 15 elements into the array
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java with Blue J Programming Help
Next Thread in Java Forum Timeline: object stream and byte array conversion





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC