I am having some serious trouble.
My program that I have to write is a GUI that essentially asks for:
- A Course (ex: cpsc130)
- The name (ex: computer programming 2)
- The amount of credits you receive for the class (ex 3)
- And your grade (ex A, B).
I'm not very good with ActionListeners, escentially I have no idea what I'm doing with it. I have to make an ArrayList for the information
- One ArrayList<Course>(instance variable) e.g. courseList, to store the courses you add;
- Four inner ActionListenerclasses, with each of them implements its method of actionPerformed. Specically,
- AddCourseListener: read the inputs and create a course object, add the course object into the courseList, and append this into the output area;
- CalGPAListener: read all course credits and grades from the courseList, add them up and compute the overall GPA. You are assuming that A is 4, B is 3, C is 2, D is 1, and E is 0. GPA = Sum(creditpoint)=totalCredits. For instance (Figure 1), you have taken 3 courses: 130 (3 credits, grade of A), 131 ( 3 credits, grade of B), and 370 (4 credits, grade of B), then your GPA = (3*4+3*3+4*3)/(3+3+4) = 3.3.
- ResetInputListener: reset all input elds;
- ResetOutputListener: reset output area.
Those are all the ActionListeners I need. I will show you my code so far. When compiled it will show all the Buttons and TextAreas, the ActionListeners are the only thing I need help with.
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class DegreeWorksFrame extends JFrame  {
    private JLabel courseCodeLabel;
    private JTextField courseCodeField;
    private JLabel courseNameLabel;
    private JTextField courseNameField;
    private JLabel courseCreditLabel;
    private JTextField courseCreditField;
    private JLabel courseGradeLabel;
    private JTextField courseGradeField;
    private JTextArea resultArea;
    private double sum =0;    
    private double totalCredits=0;
    private String code = "";
    private String name = "";
    private String credit = "";
    private String grade = "";
    private String heading = ("Code\tName\tCredit\tGrade" + "\n");
    private ArrayList<Course> courseList;
    private JButton AddCourse;
    private JButton CalculateGPA;
    private JButton ResetInput;
    private JButton ResetOutput;
    private static final int AREA_ROWS = 15;
    private static final int AREA_COLUMNS = 35;
    private final int FRAME_HEIGHT =400;
    private final int FRAME_WIDTH = 500;
    final int FIELD_WIDTH = 30;
    public DegreeWorksFrame() {
        resultArea = new JTextArea(AREA_ROWS, AREA_COLUMNS);
        resultArea.setText(heading);
        resultArea.setEditable(false);
        createTextField1();
        createTextField2();
        createTextField3();
        createTextField4();
        createButtonAddCourse();
        createButtonCalculateGPA();
        createButtonResetInput();
        createButtonResetOutput();
        createPanel();
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
    }
    private void createTextField1() {
        courseCodeLabel = new JLabel("Enter Course Code: ");
        courseCodeField = new JTextField(FIELD_WIDTH);
        courseCodeField.setText(code);
    }
    private void createTextField2() {
        courseNameLabel = new JLabel("Enter Course Name: ");
        courseNameField = new JTextField(FIELD_WIDTH);
        courseNameField.setText(name);
    }
    private void createTextField3() {
        courseCreditLabel = new JLabel("Enter Course Credit: ");
        courseCreditField = new JTextField(FIELD_WIDTH);
        courseCreditField.setText(credit);
    }
    private void createTextField4() {
        courseGradeLabel = new JLabel("Enter Course Grade: ");
        courseGradeField = new JTextField(FIELD_WIDTH);
        courseGradeField.setText(grade);
    }
    private void createButtonAddCourse() {
        AddCourse = new JButton ("Add Courses: ");
        ActionListener listener = new addCourseListener();
        AddCourse.addActionListener(listener);
    }
    class addCourseListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            courseList = new ArrayList();
            String receiveList = userList.get
        }
    }
    private void createButtonCalculateGPA() {
        CalculateGPA = new JButton ("Calculate GPA");
        ActionListener listener = new addCourseListener();
        AddCourse.addActionListener(listener);
    }
    class calculateGPAListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
        }
    }
    private void createButtonResetInput() {
        ResetInput = new JButton ("Reset Input");
        ActionListener listener = new addCourseListener();
        AddCourse.addActionListener(listener);
    }
    class resetInputListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
        }
    }
    private void createButtonResetOutput() {
        ResetOutput = new JButton ("Reset Output");
        ActionListener listener = new addCourseListener();
        AddCourse.addActionListener(listener);
    }
    class resetOutputListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
        }
    }
    private void createPanel() {
        JPanel panel = new JPanel();
        panel.add(courseCodeLabel);
        panel.add(courseCodeField);
        panel.add(courseNameLabel);
        panel.add(courseNameField);
        panel.add(courseCreditLabel);
        panel.add(courseCreditField);
        panel.add(courseGradeLabel);
        panel.add(courseGradeField);
        //buttons
        panel.add(AddCourse);
        panel.add(CalculateGPA);
        panel.add(ResetInput);
        panel.add(ResetOutput);
        //Scroll bar
        JScrollPane scrollPane = new JScrollPane(resultArea);
        panel.add(scrollPane);
        add(panel);
    }
}
 
     
    