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

AI Thread Summary
The discussion focuses on building a graphical user interface (GUI) in Java that accepts integers into an array of 15 elements, calculates their average, and displays the count and average when the "AVERAGE" button is clicked. It also includes a "CLEAR" button to reset the count and array elements to zero. Suggestions include creating an instance variable for the array to store values, using an anonymous inner class for action listeners instead of defining a separate class, and simplifying input handling by allowing comma-separated integers in a single JTextField. The importance of proper variable declaration and scope in Java is also emphasized, recommending that GUI components be defined within the constructor for better organization. Overall, the consensus is that the initial approach is on the right track, with practical advice for improvements and best practices in Java GUI development.
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
2
Views
2K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
6
Views
2K
Replies
2
Views
4K
Replies
9
Views
4K
Replies
3
Views
3K
Replies
3
Views
4K
Back
Top