java - Java类中的实现和继承问题

标签 java class inheritance extends implements

我这里有一个类,当我尝试运行时出现错误:

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

我现在不明白我做错了什么。我的 GUI 类实现了 ActionListener。任何帮助将不胜感激。谢谢!

GUI 类:

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();

    }

}

最佳答案

您需要将 actionPerformed 声明移至构造函数之外。

目前你有这个:

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

它需要像:

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

我还没有测试过你的代码,但这是第一个明显的问题。

关于java - Java类中的实现和继承问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23069154/

相关文章:

java - 覆盖接口(interface)默认方法

c++ - 为 C 消费包装 C++ 类 API

php - 如何在php中加载类

java - 在java中的3个类中调用另一个类中的方法

java - 为什么 this() 和 super() 不能在构造函数中一起使用?

Java:强制基类使用基类方法而不是重写方法

java - Weblogic Websocket端点不工作

java - 如何从 JPAspersistence.xml 中外部化属性?

java - rJava安装问题

java - 如何制作与多行字符串中的标记匹配的正则表达式?