Results 1 to 5 of 5
- 07-17-2012, 08:00 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 13
- Rep Power
- 0
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 08:08 AM.
- 07-17-2012, 11:05 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
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:
3. in your private class ResetButtonListener delete your code in method actionPerformed. It should be something like this: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
4. Write code it actionPerformed method:Java Code:private class ResetButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { } }
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 01:09 AM.
- 07-18-2012, 12:42 AM #3
Member
- Join Date
- Jun 2012
- Posts
- 13
- Rep Power
- 0
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?
- 07-18-2012, 01:03 AM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
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.
- 07-18-2012, 01:35 AM #5
Member
- Join Date
- Jun 2012
- Posts
- 13
- Rep Power
- 0
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 07:46 PM.
Similar Threads
-
Test Score
By alvina32 in forum New To JavaReplies: 2Last Post: 04-20-2012, 10:28 AM -
how to make program that keeps score and loops till XXX is inputed
By jaki312 in forum New To JavaReplies: 2Last Post: 03-13-2012, 08:07 PM -
Storing high score and sorting the array
By Implode in forum New To JavaReplies: 8Last Post: 09-28-2009, 12:43 AM -
Basic high score program
By Implode in forum New To JavaReplies: 5Last Post: 09-03-2009, 05:21 PM -
Test score average
By ryn21 in forum New To JavaReplies: 11Last Post: 10-17-2008, 05:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks