我有一个名为“ FormPanel.java”的类,用于扩展JPanel。此类创建一个带有一些JTextField,一个ComboBoxes,一个JList和一个名为“ submitBtn”的JButton的信息输入表单。填写表格后,应按下JButton处理该信息。因此,我向JButton添加了一个动作监听器。由于我还有一些其他组件需要侦听器来处理其事件,因此我在FormPanel类中定义了addListeners()方法,并在其中创建了JButton侦听器,如下所示:
public class FormPanel extends JPanel {
/* some codes here*/
private void addListeners() {
submitBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
FormEvent = new FormEvent(this,emptxtField,emptxtField);
/* and there are some lines of codes here */
System.out.println(this);
formEventAccured(ev);
//this.formEventAccured(ev);
}
});
/*and some other listeners in here*/
}
/* some codes here*/
public void formEventAccured(FormEvent e) {
//…
}
}
formEventAccured(FormEvent e)是FormPanel类中的公共方法。问题就在这里:在actionPerformed方法中,如果我将formEventAccured方法称为“ formEventAccured(ev)”,一切都会很好,但是如果我将其称为“ this.formEventAccured(ev)”,则会导致错误!即使我打印“此”对象,并且它是FormPanel类,也不是ActionListener!
sysout结果:
FormPanel$1@1bc7e86
错误:未为new ActionListener(){}类型定义方法formEventAccured(FormEvent)
因为formEventAccured是FormPanel类的公共方法,并且打印“ this”表明它是FormPanel对象,所以为什么将其称为“ this.formEventAccured(ev)”会导致错误? 谢谢。