0

I have a class here that when I try to run gives me the error:

The type GUI must implement the inherited abstract method ActionListener.actionPerformed(actionEvent)
void is an invalid type for the variable actionPerformed
Syntax error on token "(", ; expected
Syntax error on token ")", ; expected

I don't understand what I'm doing wrong at this point. I have my GUI class implementing ActionListener. Any help would be greatly appreciated. Thanks!

GUI Class:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Enumeration;

public class GUI extends JFrame implements ActionListener {
    // RadioButtons & ButtonGroup
    private JRadioButton jrbStudent = new JRadioButton("Student");
    private JRadioButton jrbGraduate = new JRadioButton("Graduate Student");
    private ButtonGroup group = new ButtonGroup();

    // JTextFields
    private JTextField name = new JTextField(20);
    private JTextField address = new JTextField(20);
    private JTextField balance = new JTextField(20);
    private JTextField major = new JTextField(20);

    // Submit Button
    private JButton jbtSubmit = new JButton("Submit");

    // echoStudent output area
    private JTextArea echoStudent = new JTextArea();

    // Specific Buttons
    private JButton printStudentNames = new JButton("Print Student's Names");
    private JButton printGradStudentNames = new JButton(
            "Print Graduate Student's Names");
    private JButton calcBalance = new JButton(
            "Calculate Average Balance of All Students");
    private JButton compSciMajor = new JButton(
            "Displays Computer Science Major Students");

    // ScrollPane
    private JScrollPane scrollPane = new JScrollPane(echoStudent);

    private Manager m1 = new Manager();

    public GUI () {
        super("College Admission Information Interface");

        // Creates panel P1 and adds the components
        JPanel p1 = new JPanel(new GridLayout(9, 1, 10, 10));
        p1.add(new JLabel("Name: "));
        p1.add(name);
        p1.add(new JLabel("Address: "));
        p1.add(address);
        p1.add(new JLabel("Balance: "));
        p1.add(balance);
        p1.add(new JLabel("Major: "));
        p1.add(major);
        p1.add(jrbStudent);
        p1.add(jrbGraduate);
        p1.add(new JLabel("Submit Button: "));
        p1.add(jbtSubmit);
        p1.add(printStudentNames);
        p1.add(printGradStudentNames);
        p1.add(calcBalance);
        p1.add(compSciMajor);
        p1.add(new JLabel("Submitted Text: "));

        // Creates a radio-button group to group both buttons
        group.add(jrbStudent);
        group.add(jrbGraduate);

        // Registers listeners
        jrbStudent.addActionListener(this);
        jrbGraduate.addActionListener(this);
        jbtSubmit.addActionListener(this);
        printStudentNames.addActionListener(this);
        printGradStudentNames.addActionListener(this);
        calcBalance.addActionListener(this);
        compSciMajor.addActionListener(this);

        // GUI settings
        setTitle("Information Interface");
        setSize(1200, 900);
        setLocationRelativeTo(null); // Center the frame
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        // Adds the panel and text area to the frame
        add(p1);
        p1.add(echoStudent);
        echoStudent.setEditable(false);

        public void actionPerformed(ActionEvent event) {
            if(event.getSource() == jbtSubmit) {
                if(jrbStudent.isSelected()) {
                    Student s1 = Manager.instance().createStudent(name.getText(),
                            address.getText(), major.getText(),
                            Double.parseDouble(balance.getText()));
                    echoStudent.append("\n\nCreated Student: " + s1.toString());
                }

                else if(jrbGraduate.isSelected()) {
                    GradStudent g1 = Manager.instance().createGradStudent(name.getText(),
                            address.getText(), major.getText(),
                            Double.parseDouble(balance.getText()));
                    echoStudent.append("\n\nCreated Graduate Student: " + g1.toString());

                }
            }
            else if(event.getSource() == printStudentNames) {
                echoStudent.append(Manager.instance().listStudents());
            }
            else if(event.getSource() == printGradStudentNames) {
                echoStudent.append(Manager.instance().listGrads());
            }
            else if(event.getSource() == calcBalance) {
                echoStudent.append(Manager.instance().aveBalance());
            }
            else if(event.getSource() == compSciMajor) {
                echoStudent.append(Manager.instance().listCsci());
            }
        }
    }

    public static void main(String[] args) {
        new GUI();

    }

}
2
  • 1
    You can't declare methods within methods/constructors. Commented Apr 14, 2014 at 19:58
  • I have my GUI class implementing ActionListener. No you don't. Check again. Commented Apr 14, 2014 at 19:59

1 Answer 1

1

You need to move your actionPerformed declaration outside of your constructor.

Currently you have this:

public class GUI extends JFrame implements ActionListener {
    ...
    public GUI() {
        ...
        public void actionPerformed(ActionEvent event) {
            ...
        }
    }
}

It needs to be like:

public class GUI extends JFrame implements ActionListener {
    ...
    public GUI() {
        ...
    }
    public void actionPerformed(ActionEvent event) {
        ...
    }
}

I have not tested your code but that is the first obvious problem.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm not sure that I understand. I don't see where the third "public class GUI..." is.
@Ben sorry I did a copy/paste fail. Fixed it.
Ah, I see now. Thank you! I needed to move a couple of brackets around.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.