Java Listener-不听

时间:2013-09-10 09:13:57

标签: java swing action actionlistener

我正在尝试编译此代码(下面),但我一直收到一条错误消息,说明

  

此行有多个标记

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

我还不熟悉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
    }

}

3 个答案:

答案 0 :(得分:2)

您需要实施actionPerformed方法。您似乎已将其实现为ationPerformed,因此您需要修复该拼写。因为您没有正确实现接口,所以不能将该类用作ActionListener。

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

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

答案 1 :(得分:2)

您的方法名称 ationPerformed 中有拼写错误 actionPerformed

答案 2 :(得分:2)

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

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

相关问题