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

In summary, the ArrayGUIPanel has a JLabel for each input field, a JTextField to hold the data entered, a JLabel for the current average, a JButton to toggle the average, and a JLabel to display the count and the current average.
  • #1
Santa_Laws
1
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
  • #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 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
 
  • #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

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:
  • #4
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.
 

1. How do I create a GUI for integer input in Java?

To create a GUI for integer input in Java, you will need to use the Java Swing library. This library contains classes and methods for creating and managing GUI components, such as buttons and text fields.

2. How do I add average and clear buttons to my GUI?

To add average and clear buttons to your GUI, you will need to create JButton objects and add them to your GUI using a LayoutManager. Then, you can add ActionListener objects to your buttons to handle their functionality.

3. How do I ensure that only integers can be inputted into my GUI?

To ensure that only integers can be inputted into your GUI, you can use a JFormattedTextField and set its formatter to an IntegerFormatter. This will restrict the input to only accept integers and will display an error if the user tries to input a non-integer value.

4. Can I customize the appearance of my GUI?

Yes, you can customize the appearance of your GUI by using different LayoutManagers, choosing different fonts and colors for your components, and adding images or icons to your buttons. You can also create custom graphics for your GUI using Java's Graphics and Graphics2D classes.

5. How do I handle errors or exceptions in my GUI?

To handle errors or exceptions in your GUI, you can use try-catch blocks to catch any potential errors and display an appropriate message to the user. You can also use InputVerifier objects to validate user input before it is processed, and display an error message if the input is invalid.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
877
  • Engineering and Comp Sci Homework Help
Replies
1
Views
898
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
9
Views
3K
  • Programming and Computer Science
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
27
Views
23K
Back
Top