Results 1 to 5 of 5
  1. #1
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default GUI Array help...test score program

    Hello,

    I am tyring to make a GUI program that gets 5 test scores from the user, stores them into an array, then has a few check boxes to display average, min, max, letter grade etc... I cannot figure out how to get the test scores which are entered to be stored in the array.

    Also I cannot figure out how to get my reset button to clear the check boxes. My code is below. I appreciate any help.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.Scanner;
    
    
    public class TextField extends JPanel
    {
    	private JTextField[] inputBox;
    	double testGrade;
    
    
    	public TextField()
    	{
    		setLayout(new GridLayout(1,5));
    		inputBox = new JTextField[5];
    
    		setBorder(BorderFactory.createTitledBorder("Test Scores"));
    
    		for (int i = 0; i < inputBox.length; i++)
    		{
    			inputBox[i] = new JTextField();
    
    
    			add(inputBox[i]);
    		}
    	}
    }
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
    
    public class TestCalculator extends JFrame
    {
    	private StatisticsPanel stats;
    	private JPanel buttonPanel;
    	private JButton calcButton;
    	private JButton resetButton;
    
    	public TestCalculator()
    	{
    		setTitle(" ");
    
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		setLayout(new BorderLayout());
    
    		stats = new StatisticsPanel();
    		TextField inputBox = new TextField();
    
    		buildButtonPanel();
    
    		add(inputBox, BorderLayout.NORTH);
    		add(stats, BorderLayout.CENTER);
    		add(buttonPanel, BorderLayout.SOUTH);
    
    		pack();
    		setVisible(true);
    	}
    
    
    	private void buildButtonPanel()
    	{
    		buttonPanel = new JPanel();
    
    		calcButton = new JButton("Calculate Statistics");
    		resetButton = new JButton("Reset");
    
    		calcButton.addActionListener(new CalcButtonListener());
    		resetButton.addActionListener(new ResetButtonListener());
    
    		buttonPanel.add(calcButton);
    		buttonPanel.add(resetButton);
    	}
    
    	private class CalcButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			double average, max, min;
    
    			JOptionPane.showMessageDialog(null, "STATISTICS:" +
    													"\nAverage: " +
    													"\nMaximum: " + "\nMinimum: "
    													+ "\nLetter Grade: ");
    
    		}
    	}
    
    	private class ResetButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			if (e.getSource() == true)
    			{
    
    				e.setSource(false);
    			}
    
    		}
    	}
    }
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class StatisticsPanel extends JPanel
    {
    	private JRadioButton dropLowest;
    	private JCheckBox average;
    	private JCheckBox max;
    	private JCheckBox min;
    	private JCheckBox letterGrade;
    
    	private ButtonGroup bg;
    
    
    	public StatisticsPanel()
    	{
    
    		setLayout(new GridLayout(5,1));
    
    
    
    		dropLowest = new JRadioButton("Average Drops Lowest Test");
    
    		bg = new ButtonGroup();
    		bg.add(dropLowest);
    
    		average = new JCheckBox("Average");
    		max = new JCheckBox("Maximum");
    		min = new JCheckBox("Minimum");
    		letterGrade = new JCheckBox("Letter Grade");
    
    		setBorder(BorderFactory.createTitledBorder("Statistics"));
    
    		add(dropLowest);
    		add(average);
    		add(max);
    		add(min);
    		add(letterGrade);
    	}
    }
    Java Code:
    public class Calculator
    {
    	public static void main(String[] args)
    	{
    		new TestCalculator();
    	}
    }
    Last edited by VettesRus; 07-17-2012 at 09:08 AM.

  2. #2
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: GUI Array help...test score program

    Let's make at first RESET button workable.

    1. In your TextField class:

    a) write method getInputBox that returns JTextField[] variable inputBox

    b) write method getInputBoxAtIndex(int index) that returns variable inputBox at current index

    c) write method getInputBoxLength that returns lenght or inputBox variable

    2. In your class TestCalculator add one line of code, and put a comment tags on one line of code, or delete that line of code:
    Java Code:
    public class TestCalculator extends JFrame {
    	private StatisticsPanel stats;
    	private JPanel buttonPanel;
    	private JButton calcButton;
    	private JButton resetButton;
    	private TextField inputBox = new TextField(); // add this line of code
    	
    	public TestCalculator() {
    		setTitle(" ");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLayout(new BorderLayout());
    		
    		stats = new StatisticsPanel();
    		// TextField inputBox = new TextField(); put comment on this line of code or delete this line of code
    3. in your private class ResetButtonListener delete your code in method actionPerformed. It should be something like this:
    Java Code:
           private class ResetButtonListener implements ActionListener {
    
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			
    		}
    	}
    4. Write code it actionPerformed method:

    a) loop index from 0 to inputBox lenght (you have already written that method in 1. c) )

    b) inside that loop get inputBoxAtIndex(index) (you have already written that method in 1. b) ) and setText to "";
    Last edited by cselic; 07-18-2012 at 02:09 AM.

  3. #3
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Re: GUI Array help...test score program

    I just do not understand where the ScorePanel() comes from. Dont I need a ScorePanel class for that to work?

  4. #4
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: GUI Array help...test score program

    ^Sorry, everywhere where you see ScorePanel change it with TextField. Working with your code I have changed name of your TextField class with new name ScorePanel because I like that name more, but nevermind.

  5. #5
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Re: GUI Array help...test score program

    So I think I am on the right track, but I cannot get my findMaximum to compare the grades. I get a compiler error that says

    error: cannot find symbol
    if( grade[i].getGrade().compareTo( maxValue.getGrade()) > 0)
    ^
    symbol: method compareTo(JTextField)
    location: class JTextField
    1 error

    Tool completed with exit code 1


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
    
    public class TestCalculator extends JFrame
    {
    	private StatisticsPanel stats;
    	private JPanel buttonPanel;
    	private JButton calcButton;
    	private JButton resetButton;
    
    	TextField[] inputBox = new TextField[5];
    
    	public TestCalculator()
    	{
    		setTitle("");
    
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		setLayout(new BorderLayout());
    
    		stats = new StatisticsPanel();
    		TextField inputBox = new TextField();
    
    		buildButtonPanel();
    
    		add(inputBox, BorderLayout.NORTH);
    		add(stats, BorderLayout.CENTER);
    		add(buttonPanel, BorderLayout.SOUTH);
    
    		pack();
    		setVisible(true);
    	}
    
    
    	private void buildButtonPanel()
    	{
    		buttonPanel = new JPanel();
    
    		calcButton = new JButton("Calculate Statistics");
    		resetButton = new JButton("Reset");
    
    		calcButton.addActionListener(new CalcButtonListener());
    		resetButton.addActionListener(new ResetButtonListener());
    
    		buttonPanel.add(calcButton);
    		buttonPanel.add(resetButton);
    	}
    
    	public static void findMaximum(TextField[] grade)
    	{
    		TextField maxValue = grade[0];
    
    		for( int i = 0; i < grade.length; i++ )
    		{
    			if( grade[i].getGrade().compareTo( maxValue.getGrade()) > 0)
    			{
    				// Store new high grade into maxValue
    				maxValue = grade[i];
    			}
    		}
    
    	}
    
    	public static void findMinimum()
    	{
    	}
    
    	public static void findAverage()
    	{
    	}
    
    	public static void findLetterGrade()
    	{
    	}
    
    
    
    	private class CalcButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			double average, max, min;
    
    
    			JOptionPane.showMessageDialog(null, "STATISTICS:" +
    													"\nAverage: " +
    													"\nMaximum: " + "\nMinimum: "
    													+ "\nLetter Grade: ");
    
    		}
    	}
    
    	private class ResetButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			for( int i = 0; i < inputBox.length; i++ )
    			{
    
    			}
    
    
    		}
    	}
    
    }
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.Scanner;
    
    
    public class TextField extends JPanel
    {
    	private JTextField[] inputBox;
    	JTextField grade;
    
    	public TextField()
    	{
    		setLayout(new GridLayout(1,5));
    		inputBox = new JTextField[5];
    
    		setBorder(BorderFactory.createTitledBorder("Test Scores"));
    
    		for (int i = 0; i < inputBox.length; i++)
    		{
    			inputBox[i] = new JTextField();
    
    			add(inputBox[i]);
    		}
    	}
    
    	public JTextField getGrade()
    	{
    		JTextField grade = inputBox[0];
    
    		for (int i = 0; i < inputBox.length; i++ )
    		{
    
    			grade = inputBox[i];
    
    		}
    
    		return grade;
    	}
    
    }
    Last edited by VettesRus; 07-18-2012 at 08:46 PM.

Similar Threads

  1. Test Score
    By alvina32 in forum New To Java
    Replies: 2
    Last Post: 04-20-2012, 11:28 AM
  2. Replies: 2
    Last Post: 03-13-2012, 09:07 PM
  3. Storing high score and sorting the array
    By Implode in forum New To Java
    Replies: 8
    Last Post: 09-28-2009, 01:43 AM
  4. Basic high score program
    By Implode in forum New To Java
    Replies: 5
    Last Post: 09-03-2009, 06:21 PM
  5. Test score average
    By ryn21 in forum New To Java
    Replies: 11
    Last Post: 10-17-2008, 06:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •