Java 代码告诉我需要实现ActionListener,而我已经实现了?

Java 代码告诉我需要实现ActionListener,而我已经实现了?,java,swing,actionlistener,inheritance,Java,Swing,Actionlistener,Inheritance,因此,我将此代码编写为基于用户单击的文件。我唯一的问题是,我在“公共类程序”上出错。prog名称是我得到错误的地方:它说:prog类型必须实现继承的抽象方法ActionListener.actionPerformed(ActionEvent)。当我进行添加未继承方法的快速修复时,它会将action listener方法添加到代码的末尾,但其中没有任何内容。如果我在程序中已经有了动作侦听器,为什么它会告诉我需要实现它们?为什么当我在最后添加它时,即使里面没有任何东西,它也能正常工作 import

因此,我将此代码编写为基于用户单击的文件。我唯一的问题是,我在“公共类程序”上出错。prog名称是我得到错误的地方:它说:prog类型必须实现继承的抽象方法ActionListener.actionPerformed(ActionEvent)。当我进行添加未继承方法的快速修复时,它会将action listener方法添加到代码的末尾,但其中没有任何内容。如果我在程序中已经有了动作侦听器,为什么它会告诉我需要实现它们?为什么当我在最后添加它时,即使里面没有任何东西,它也能正常工作

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.*;

public class prog extends JFrame implements ActionListener {

//create newLine
final String newLine = System.getProperty("line.separator");

//create buttons
JPanel row1 = new JPanel();
JButton oneLeft = new JButton("oneLeft");
JButton oneRight = new JButton("oneRight");

JPanel row2 = new JPanel();
JButton twoLeft = new JButton("twoLeft");
JButton twoRight = new JButton("twoRight");

JPanel row3 = new JPanel();
JButton threeLeft = new JButton("threeLeft");
JButton threeRight = new JButton("threeRight");


public prog() {
    super("Prog");
    setLookAndFeel();
    setSize(400, 800);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GridLayout layout = new GridLayout(3, 2);
    setLayout(layout);

    //create outStream for writing to file
    try {
        final File numClicks = new File("numClicks.properties");
        final FileOutputStream outStream = new FileOutputStream(numClicks);
        //add Listeners
        oneLeft.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    write(outStream, "oneLeft has been clicked.");
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });

        oneRight.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    write(outStream, "oneRight has been clicked.");
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });

        twoLeft.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    write(outStream, "twoLeft has been clicked.");
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });

        twoRight.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    write(outStream, "twoRight has been clicked.");
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });

        threeLeft.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    write(outStream, "threeLeft has been clicked.");
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });

        threeRight.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    write(outStream, "threeRight has been clicked.");
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });
    } catch (IOException ioe) {
        System.out.println("The file could not be written.");
    }




    row1.add(oneLeft);
    row1.add(oneRight);
    row2.add(twoLeft);
    row2.add(twoRight);
    row3.add(threeLeft);
    row3.add(threeRight);
    add(row1);
    add(row2);
    add(row3);



    setVisible(true);
}

private void setLookAndFeel() {
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    } catch (Exception e) {
        //ignore error
    }
}

void write(FileOutputStream stream, String output) throws IOException {
    output = output + newLine;
    byte[] data = output.getBytes();
    stream.write(data, 0, data.length);
}

public static void main(String[] args) {
    prog progApp = new prog();
}
}

您可以
实现ActionListener
它,但实际上并没有实现所需的方法(即
actionPerformed()
)。因此,您的类对编译器无效

您需要一种类似以下的方法:

@Override
public void actionPerformed(ActionEvent e) {
    // ...
}
接口的工作方式是定义它必须实现的类。。。好。。。实施。这样,任何其他进程都可以将其视为
ActionListener
,并知道已经定义了某些方法

这是Java试图让多态性成为朋友的另一种方式

从下面的注释中可以看出,一个类实现一个接口(比如
KeyListener
)并定义方法而不使用它,这其实并不少见

例如,
KeyListener
要求您实现三种不同的方法:

  • public void键按下(KeyEvent e)
  • public void密钥释放(KeyEvent e)
  • public void密钥类型(KeyEvent e)
比方说,我只关心按下
键。那么我的班级可能看起来像这样:

public class MyKeyListener implements KeyListener {

    @Override
    public void keyPressed(KeyEvent e) {
        // do stuff
    }

    @Override
    public void keyReleased(KeyEvent e){}

    @Override
    public void keyTyped(KeyEvent e){}

}

您的类不应实现
ActionListener
。当你说
newactionlistener()

时,你不是在写一个实现接口的顶级类,而是在写一堆小的内联类(称为匿名内部类),这些内联类可以帮你完成这项工作,所以我必须有空方法,即使它不会做任何事情?如果你的类实现接口。最好删除实现声明如果是这种情况,那么您的类就不需要
实现ActionListener
。那么为什么我不需要实现它呢?我刚刚删除了implements语句,它成功了,但是为什么呢?如果我使用action listener,我不需要实现它吗?因此我必须在不使用它的情况下实现actionPerformed方法?@David如果你
实现ActionListener
,那么是的。但看起来你在主课上根本不需要它。您在内部定义的匿名子类中完成所有处理。@David关于这个问题,有一个很好的解释:啊,谢谢!这就向我解释了。我不知道如果它是匿名类,我甚至不需要实现它。我还在学习,所以这些东西很有用。谢谢@大卫:)祝你好运!