繁体   English   中英

Java侦听器-不侦听

[英]Java Listener- not listening

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

这行有多个标记

  • 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
    }

}

您需要实现actionPerformed方法。 您似乎已将其实现为ationPerformed因此,您需要修复该拼写。 由于尚未正确实现接口,因此无法将此类用作ActionListener。

关于可序列化的问题-这与JFrame实现了Serializable接口的事实有关,该接口需要serialVersionUID。 您可以不使用它进行编译,但是IDE会抱怨。 [有关更多信息,请参见此处 ]

附带说明一下,通常您不想扩展JFrame,而是在类中使用实例。

您的方法名称ation中有输入错误,应该为actionPerformed

改正您在public void ationPerformed(ActionEvent e)的错字并在“ action”中添加缺少的“ c”,这将解决错误消息。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM