Build a GUI for Integer Input with Average and Clear Buttons - Java Help

  • Context: Java 
  • Thread starter Thread starter Santa_Laws
  • Start date Start date
  • Tags Tags
    Building Gui Java
Click For Summary

Discussion Overview

The discussion focuses on building a graphical user interface (GUI) in Java for accepting integer inputs, calculating their average, and providing a clear function. Participants explore design choices, coding practices, and potential improvements to the initial implementation.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests that the class should contain an instance variable for a size 15 array to store values, implying that each GUI instance should manage its own data.
  • Another participant proposes that the count variable is unnecessary, as the length of the array can be derived directly from the array object itself.
  • A participant recommends using a single JTextField for input, allowing users to enter multiple integers separated by commas, and provides a method for parsing this input.
  • There is a suggestion to use an anonymous inner class for action listening instead of defining a separate listener class, which some participants find confusing but potentially beneficial for simplicity.
  • One participant comments on coding style, noting that it is not customary to declare multiple JLabel variables in a single line and suggests defining them within the constructor instead.

Areas of Agreement / Disagreement

Participants generally agree on the direction of the implementation but express differing opinions on specific design choices and coding practices. No consensus is reached on the best approach to handle the count variable or the structure of the action listener.

Contextual Notes

Some suggestions involve assumptions about the user's familiarity with Java and GUI programming, which may not apply universally. The discussion also reflects varying levels of comfort with Java conventions and coding styles.

Santa_Laws
Messages
1
Reaction score
0
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
abel;

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


}

am i going right direction here?
 
Technology news on Phys.org
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 written 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
 
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

then
Code:
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:
JButton button = new JButton("Click me!");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
				System.out.println("You clicked me:)");
				
			}

});

panel.add(button);
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:
P.S.: In Java it is not customary to do

private JLabel inputLabel, outputLabel, resultLabel, countLabel, averageL
abel;

but
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 27 ·
Replies
27
Views
24K
  • · Replies 3 ·
Replies
3
Views
4K