Java 侦听器 - 不侦听



我正在尝试编译此代码(如下),但不断收到一条错误消息,指出

此行处有多个标记

  • 类型qq必须实现继承的抽象方法ActionListener.actionPerformed(ActionEvent)
  • 可序列化类qq不声明类型为 longstatic final serialVersionUID 字段

我对Java还很陌生,我真的不知道发生了什么,你们都碰巧对我如何纠正这种不幸的情况有任何见解吗?

public class qq extends JFrame implements ActionListener, ItemListener {
    // many fields here
    public qq() {
        // components initializing
        // other code for window closing etc.
    }
    // actionPerformed is ActionListener interface method
    // which responds to action event of selecting
    // combo box or radio button
    public void ationPerformed(ActionEvent e){
        if (e.getSource() instanceof JComboBox){
            System.out.println("Customer shops: " + freqButton.getSelectedItem());
        }
        else if (e.getSource() instanceof JRadioButton){
            if (age1.isSelected() ){
                System.out.println("Customer is under 20");
            }
            else if (age2.isSelected() ){
                System.out.println("Customer is 20 - 39");
            }
            else if (age3.isSelected() ){
                System.out.println("Customer is 39 - 59");
            }
            else if (age4.isSelected() ){
                System.out.println("Customer is over 60");
            }
        }
    }
    // itemStateChanged is ItemListener interface method
    // which responds to item event of clicking checkbox
    public void itemStateChanged (ItemEvent e){
        if (e.getSource() instanceof JCheckBox){
            JCheckBox buttonLabel = (JCheckBox)
                    e.getItemSelectable();
            if (buttonLabel == tradeButton){
                if (e.getStateChange() == e.SELECTED) {
                    System.out.println("Customer is trade");
                }
                else
                {
                    System.out.println("Customer is not trade");
                }
            }
        }
    }
    public static void main(String args[]) {
        qq cd = new qq();
        // other code setting up the window
    }
}

public void ationPerformed(ActionEvent e)中更正您的拼写错误,并在"action"中添加缺少的"c",这将处理错误消息。

您可以忽略有关 serialVersionUID 的警告,稍后当您了解有关序列化的更多信息时,请回到该警告。

您需要实现 actionPerformed 方法。您似乎已经实现了它ationPerformed因此,您需要修复该拼写。由于尚未正确实现接口,因此不能将该类用作操作侦听器。

至于可序列化的问题 - 这与JFrame实现Serializable接口有关,这需要一个serialVersionUID。您可以在没有一个的情况下进行编译,但 IDE 会抱怨。[更多信息,请参阅此处]

作为旁注,通常您不想扩展 JFrame,而是在您的类中使用实例。

您的方法名称中有拼写错误 ation已执行 它应该是 操作已执行

最新更新