Im taking Java at my local CC and so far i am so lost. My teacher knows what she is doing as far as programming but she is a horrible teacher, no one in the class understands the material and she will not explain herself or try to teach another way. With that said i have a program i would like help with or corrected if someone wouldn't mind. My code below i cant get to run and also i would like my program to be more organized with the buttons in a certain order and size. Thanks for your time
import java.text.DecimalFormat;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Robert_Bachman_JFrame extends JFrame
{
private static final int WIDTH = 550; private static final int HEIGHT = 430;
private ExitButtonHandler ebHandler; private ClearButtonHandler cbHandler;
private JLabel testL, numOfGradeL, averageL, TopGradeL;
private JButton enterGradeB, clearB, exitB;
private JTextField testTF, numOfGradeTF, averageTF, TopGradeTF;
private EnterGradeButtonHandler esbHandler;
double totalGrade; int topGrade; int numOfGrade;
public Robert_Bachman_JFrame()
{
//Title
setTitle("Test Scores");
// Text fields
averageTF = new JTextField(10);
testTF = new JTextField(10);
TopGradeTF = new JTextField(10);
numOfGradeTF = new JTextField(10);
// labels
TopGradeL = new JLabel("Top score: ", SwingConstants.RIGHT);
numOfGradeL = new JLabel("Number of scores: ", SwingConstants.RIGHT);
averageL = new JLabel("Average score: ", SwingConstants.RIGHT);
testL = new JLabel("Test score: ", SwingConstants.RIGHT);
// Clear button
cbHandler = new ClearButtonHandler();
clearB = new JButton("Clear");
clearB.addActionListener(cbHandler);
//Enter score button
enterGradeB = new JButton("Enter score");
enterGradeB.addActionListener(esbHandler);
esbHandler = new EnterGradeButtonHandler();
//Get container
Container pane = getContentPane();
//Exit button
exitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);
//layout
pane.setLayout(new GridLayout(6, 3));
//Set size
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//Place components
pane.add(testL); pane.add(testTF);
pane.add(numOfGradeL); pane.add(numOfGradeTF);
pane.add(averageL); pane.add(averageTF);
pane.add(TopGradeL); pane.add(TopGradeTF);
pane.add(enterGradeB); pane.add(clearB);
pane.add(exitB);
//Disable text
numOfGradeTF.setEditable(false);
averageTF.setEditable(false);
TopGradeTF.setEditable(false);
}
private class ClearButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
testTF.setText("");
topGrade = 0;
averageTF.setText("");
numOfGradeTF.setText("");
numOfGrade = 0;
TopGradeTF.setText("");
testTF.requestFocusInWindow();
}
}
private class EnterGradeButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double test, average;
DecimalFormat oneDecimal = new DecimalFormat("0.0");
test = Double.parseDouble(testTF.getText());
totalGrade += test;
numOfGrade++;
average = (totalGrade / numOfGrade);
topGrade = (int) larger(topGrade, test);
numOfGradeTF.setText("" + numOfGrade);
averageTF.setText("" + oneDecimal.format(average));
TopGradeTF.setText("" + topGrade);
testTF.requestFocusInWindow();
testTF.selectAll();
}
public double larger(double topGrade, double grade)
{
if (topGrade >= grade)
return topGrade;
else
return grade;
}
}
private class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public class New_Class
{
public void main(String[] args)
{
New_Class A5Object = new New_Class();
}
}
}