Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java help

  1. Nov 9, 2006 #1
    Build a GUI to accept integers (when the ENTER key is depressed) into an arr
    ay 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 CLEA
    R; when CLEAR is selected, count is set to 0 and so are all the array elemen
    ts and then there is another display: count is 0 and average is 0.0.

    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, averageL

    private JTextField nums;
    private JButton average, clear;
    private int count;
    private double avg;

    // Constructor: Sets up the main GUI components.
    public ArrayGUIPanel()

    count = 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);

    add (inputLabel);
    add (average);
    add (clear);
    add (resultLabel);
    add (countLabel);
    add (averageLabel);

    setPreferredSize (new Dimension(300, 75));
    setBackground (Color.white);


    am i going right direction here?
  2. jcsd
  3. Nov 9, 2006 #2
    I would say yes as a general consensus, but tis hard for me to understand what you plan on adding. I would ask, Shouldn't this class contain an instance variable that is a size 15 Array. Then each instance of your Gui has its own array to store its values into. Also I assume you are planning on defining your own class AverageListener that will extend ActionListener, Ususlly when I have writen simle Gui's I tended to just have my Gui class extend Jpanel as well as Actionlistener, pass "this" as the listener for the buttons (that is to say the gui acts as its own listener) and override the public void actionperformed within the code of your adding the listener. Other than that It looks like your planning is going fine, I'd be interested to see how this turns out
  4. Nov 10, 2006 #3
    You need not the count variable. You can get the length of an Array from the Array itself, note an array is an object in Java.

    You can just make one JTextField and parse the results. e.g.

    "1,2,3,4,5" -> written in the JTextField

    Code (Text):

    String input = jTextField.getText();

    String[] valuesAsStrings = input.split(","); //will return {"1","2","3","4","5"}
    You can get the number of values as valuesAsStrings.length // returns 5 in the above example. All what is left is to convert the Strings to ints and you are pretty much done.

    You need not to define your own class for action listening but you can use an anonymous inner class, this might sound confusing but
    Code (Text):

    JButton button = new JButton("Click me!");
    button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
                    System.out.println("You clicked me:)");


    this way might be a bit confusing at fist but after a few months it will be clear to you what you have done, since if you can create a new Class anonymously - that is - it is not assigned to a variable and put it as a parameter - then you can also override some methods.
    Last edited: Nov 10, 2006
  5. Nov 10, 2006 #4
    P.S.: In Java it is not customary to do

    private JLabel inputLabel, outputLabel, resultLabel, countLabel, averageL

    private JLable inputLabel;
    private JLabel outputLabel;

    you need not to splecify this as field of the class, you can define this inside your constructor. There is a difference between defining a JLabel outside any methods - it is a field of a Class, or inside a method - it used just locally inside the metod.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Java help
  1. Java help (Replies: 19)

  2. Java help (Replies: 6)