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
Why is my program saying there is an error: "interface expected here"?
提问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
您正在尝试实现一个类
ActionEvent
is a class not interface.
ActionEvent
是一个类而不是接口。
And in your program you don't need to extend TempConverter
with JFrame
而在你的程序中你不需要扩展TempConverter
与JFrame
Hence you can use it as :
因此,您可以将其用作:
public class TempConverter extends ActionEvent implements ActionListener
and to extend ActionEvent
you will need to create a Cunstructor of TempConverter because ActionEvent
is containing a public cunstructor in it.
并扩展ActionEvent
您将需要创建 TempConverter 的 Cunstructor,因为ActionEvent
其中包含一个公共cunstructor 。
回答by christopher
As per the Java Documentation, ActionEvent
is 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
. ActionEvent
is passed in when the event is called. By doing this, you can then access MyAction
as follows:
无需采用 中的方法ActionEvent
。ActionEvent
在调用事件时传入。通过这样做,您可以MyAction
按以下方式访问:
JButton button = new JButton("Click me!");
button.addActionListener(new MyAction());
And whatever code you put into the actionPerformed
method, will be called on button click.
无论您在actionPerformed
方法中放入什么代码,都将在单击按钮时调用。
回答by AlexR
It is because ActionEvent
is not an interface but you wrote implements ActionEvent
. Java compiler expects interface name after keyword implements
and 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 implements
an Abstract While you have to extends
it
你是implements
一个抽象的,虽然你必须这样extends
做