| | |
Java Noob
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2006
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
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.
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.
Please anyone, correct me if I am wrong. It is the best way for me to learn.
•
•
Join Date: Jun 2006
Posts: 15
Reputation:
Solved Threads: 0
Thanks David,
I'm too just learning the ropes with Java. I appreciate your introduction to the subject.
I'm too just learning the ropes with Java. I appreciate your introduction to the subject.
•
•
Join Date: Nov 2006
Posts: 2
Reputation:
Solved Threads: 0
Java Syntax (Toggle Plain Text)
package argui; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * <p>Title: </p> * * <p>Description: </p> * * <p>Copyright: Copyright (c) 2006</p> * * <p>Company: </p> * * @author not attributable * @version 1.0 */ public class ArrayGUIPanel extends JPanel { private JLabel inputLabel, outputLabel, resultLabel, countLabel, averageLabel; private JTextField nums; private JButton average, clear; private int count; private double avg; //----------------------------------------------------------------- // Constructor: Sets up the main GUI components. //----------------------------------------------------------------- public ArrayGUIPanel() { count = 0; avg=0.0; inputLabel = new JLabel ("Enter integers: " ); resultLabel = new JLabel ("---"); countLabel = new JLabel ("Count: " + count); averageLabel = new JLabel ("Average: " + avg); average = new JButton ("AVERAGE"); clear = new JButton ("CLEAR"); AvgListener listener = new AvgListener(); average.addActionListener (listener); clear.addActionListener(listener); add (inputLabel); add (average); add (clear); add (resultLabel); add (countLabel); add (averageLabel); setPreferredSize (new Dimension(300, 75)); setBackground (Color.white); } //***************************************************************** // Represents an action listener for the average input field. //***************************************************************** private class AvgListener implements ActionListener { //-------------------------------------------------------------- // Performs the conversion when the enter key is pressed in // the text field. //-------------------------------------------------------------- public void actionPerformed (ActionEvent event) { int numsTemp, averageTemp; 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.
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
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
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
![]() |
Similar Threads
- Java Noob Question (Java)
- Another Java Noob :( (Java)
- Type mismatch: cannot convert from int to ResultSet (JSP)
Other Threads in the Java Forum
- Previous Thread: Java with Blue J Programming Help
- Next Thread: object stream and byte array conversion
| Thread Tools | Search this Thread |
Tag cloud for Java
actionlistener android api apple applet application apps arguments array arrays automation balls binary bluetooth card chat class classes client code component consumer database draw eclipse ee error event exception file fractal free game gameprogramming gis givemetehcodez graphics gui helpwithhomework html ide image input integer j2me j2seprojects java javaprojects jmf jni jpanel julia jvm linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie nls notdisplaying number object oracle print problem program programming project recursion recursive scanner screen security server set size sms socket sort spamblocker sql sqlite string sun swing terminal test threads time tree trolltech windows






