java 为什么我的程序说有一个错误:“此处需要接口”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25226931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:38:06  来源:igfitidea点击:

Why is my program saying there is an error: "interface expected here"?

java

提问by TessellatingPi

Whenever I type this code, java always underlines the class declaration sentence and the error message is "interface expected here". How can I fix this?

每当我键入此代码时,java 总是在类声明语句下加下划线,错误消息是“此处需要接口”。我怎样才能解决这个问题?

package tempconverter;
import javax.swing.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import static java.lang.System.out;
public class TempConverter extends JFrame implements ActionListener, ActionEvent{
        static JFrame f = new JFrame();
        static JTextField enter = new JTextField(3);
        static JButton confirm = new JButton("Convert");

    public static void main(String[] args) {
        f.setLayout(new FlowLayout());
        f.setSize(100, 50);
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(enter); 
        f.add(confirm);
        f.setVisible(true);

    }
    @Override
    public void actionPerformed (ActionEvent e) {
        double toConvert = Float.parseFloat(enter.getText());
        double inF, inK;
        inF = toConvert / 5 * 9 + 32;
        inK = toConvert + 273;
        out.println("In degrees Fahrenheit, " + toConvert + "degrees Celsius would be " +         inF   + "degrees.");
        out.println("In degrees Kelvin, " + toConvert + "degrees Celsius would be " + inK     + "degrees.");
    }
}

回答by afzalex

You are trying to implement a class

您正在尝试实现一个类

ActionEventis a class not interface.

ActionEvent是一个类而不是接口。

And in your program you don't need to extend TempConverterwith JFrame

而在你的程序中你不需要扩展TempConverterJFrame

Hence you can use it as :

因此,您可以将其用作:

public class TempConverter extends ActionEvent implements ActionListener

and to extend ActionEventyou will need to create a Cunstructor of TempConverter because ActionEventis containing a public cunstructor in it.

并扩展ActionEvent您将需要创建 TempConverter 的 Cunstructor,因为ActionEvent其中包含一个公共cunstructor 。

回答by christopher

As per the Java Documentation, ActionEventis a class. You can only implement interfaces, hence the very clear error message.

根据Java 文档ActionEvent是一个class. 您只能实现interfaces,因此错误消息非常清楚。

What I think you meant..

我想你是什么意思..

Normally, when I want to implement some custom ActionListener, I take a similar approach:

通常,当我想实现一些 custom 时ActionListener,我会采用类似的方法:

public class MyAction implements ActionListener {
     public void actionPerformed(ActionEvent e) {
         // Your code here.
     }
}

There is no need to take on the methods in ActionEvent. ActionEventis passed in when the event is called. By doing this, you can then access MyActionas follows:

无需采用 中的方法ActionEventActionEvent在调用事件时传入。通过这样做,您可以MyAction按以下方式访问:

JButton button = new JButton("Click me!");
button.addActionListener(new MyAction());

And whatever code you put into the actionPerformedmethod, will be called on button click.

无论您在actionPerformed方法中放入什么代码,都将在单击按钮时调用。

回答by AlexR

It is because ActionEventis not an interface but you wrote implements ActionEvent. Java compiler expects interface name after keyword implementsand this is what it says.

那是因为ActionEvent不是接口而是你写的implements ActionEvent。Java 编译器在关键字之后需要接口名称implements,这就是它所说的。

In your case just remove ActionEvent. You do not need it at all.

在您的情况下,只需删除ActionEvent. 你根本不需要它。

回答by sajjad Yosefi

You are implementsan Abstract While you have to extendsit

你是implements一个抽象的,虽然你必须这样extends