Java中类内的实现和继承问题
我这里有一个类,当我尝试运行时,它会给我一个错误:Java中类内的实现和继承问题,java,class,inheritance,extends,implements,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 ")",
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) {
...
}
}
我没有测试您的代码,但这是第一个明显的问题。您不能在方法/构造函数中声明方法。我的GUI类实现了ActionListener。不,你没有。再检查一遍,我不确定我是否理解。我不知道第三个“公共类GUI…”在哪里。@Ben,很抱歉我复制/粘贴失败了。修好了。啊,我现在明白了。非常感谢。我需要移动几个支架。