Yeah, here ya go, and thank you.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.lang.Math;
public class StudentScoresApp {
public static void main(String[] args) {
System.out.println(" List of Student Scores Entered");
JFrame frame = new StudentScoresFrame();
frame.setVisible(true);
}
}
class StudentScoresFrame extends JFrame {
public StudentScoresFrame() {
setTitle("Test Scores");
setSize(310, 220);
centerWindow(this);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new StudentScoresPanel();
this.add(panel);
}
private void centerWindow(Window w) {
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2);
}
}
class StudentScoresPanel extends JPanel implements ActionListener {
private JTextField scoreTextField,
countTextField,
averageTextField,
bestTextField,
lowestTextField;
private JLabel scoreLabel,
countLabel,
averageLabel,
bestLabel,
lowestLabel;
private JButton enterScoreButton,
clearButton,
exitButton;
private int count = 0;
private double total = 0;
// total is a double to prevent truncation when
// calculating the average
private double average = 0;
private int best = 0;
private int lowest = 100;
public StudentScoresPanel() {
// display panel
JPanel displayPanel = new JPanel();
displayPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
// score label
scoreLabel = new JLabel("Test Score:");
displayPanel.add(scoreLabel);
// score text field
scoreTextField = new JTextField(13);
displayPanel.add(scoreTextField);
scoreTextField.addMouseListener(new AutoSelect());
// count label
countLabel = new JLabel("Number of Scores:");
displayPanel.add(countLabel);
// count text field
countTextField = new JTextField(10);
countTextField.setEditable(false);
countTextField.setFocusable(false);
displayPanel.add(countTextField);
// average label
averageLabel = new JLabel("Average Score:");
displayPanel.add(averageLabel);
// average text field
averageTextField = new JTextField(13);
averageTextField.setEditable(false);
averageTextField.setFocusable(false);
displayPanel.add(averageTextField);
// best label
bestLabel = new JLabel("Highest score:");
displayPanel.add(bestLabel);
// best text field
bestTextField = new JTextField(12);
bestTextField.setEditable(false);
bestTextField.setFocusable(false);
displayPanel.add(bestTextField);
// lowest label
lowestLabel = new JLabel("Lowest score:");
displayPanel.add(lowestLabel);
// lowest text field
lowestTextField = new JTextField(10);
lowestTextField.setEditable(false);
lowestTextField.setFocusable(false);
displayPanel.add(lowestTextField);
// button panel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
// enter score button
enterScoreButton = new JButton("Enter Score");
enterScoreButton.addActionListener(this);
buttonPanel.add(enterScoreButton);
// clear button
clearButton = new JButton("End/Clear Entries");
clearButton.addActionListener(this);
buttonPanel.add(clearButton);
// exit button
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
buttonPanel.add(exitButton);
// add panels to main panel
this.setLayout(new BorderLayout());
this.add(displayPanel, BorderLayout.CENTER);
this.add(buttonPanel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == exitButton){
if (count == 0){
lowest = 0;}
NumberFormat ef = NumberFormat.getNumberInstance();
ef = new DecimalFormat("###.00");
System.out.println("\n Highest: " + best + " Lowest: " + lowest + " Average: " + ef.format(average) + "\n");
System.out.printf(" Program terminated by the User.");
System.out.printf("\n");
System.exit(0);
System.exit(0);
}
else if (source == enterScoreButton) {
// parse the score
int score = Integer.parseInt(scoreTextField.getText());
// Validate the number is in proper range
if (score < 0)
JOptionPane.showMessageDialog(null, "The lowest possible score for data entry is 0-Points. Please re-enter.", "Invalid Entry", JOptionPane.ERROR_MESSAGE);
else if (score > 100)
JOptionPane.showMessageDialog(null, "The highest possible score for data entry is 100-Points. Please re-enter.", "Invalid Entry", JOptionPane.ERROR_MESSAGE);
else
// calculate the results
total += score;
count++;
average = total / count;
if (score > best)
best = score;
if (score < lowest)
lowest = score;
NumberFormat df = NumberFormat.getNumberInstance();
NumberFormat nf = NumberFormat.getNumberInstance();
df = new DecimalFormat("###.00");
countTextField.setText(nf.format(count));
averageTextField.setText(df.format(average));
bestTextField.setText(nf.format(best));
lowestTextField.setText(nf.format(lowest));
System.out.println(" " + nf.format(count) + ". " + score);
}
else if (source == clearButton) {
if (count == 0)
lowest = 0;
NumberFormat df = NumberFormat.getNumberInstance();
df = new DecimalFormat("###.00");
System.out.println("\n Highest: " + best + " Lowest: " + lowest + " Average: " + df.format(average) + "\n");
System.out.println(" Scores were cleared...\n Next Set:");
count = 0;
total = 0;
average = 0;
best = 0;
lowest = 0;
scoreTextField.setText("");
countTextField.setText("");
averageTextField.setText("");
bestTextField.setText("");
lowestTextField.setText("");
}
}
}
class AutoSelect extends MouseAdapter {
public void mouseClicked(MouseEvent e)
{
if (e.getComponent() instanceof JTextField)
{
JTextField t = (JTextField) e.getComponent();
t.selectAll();
}
}
}