每当我键入此代码时,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.");
}
}
Run Code Online (Sandbox Code Playgroud)
您正在尝试实现一个类
ActionEvent 是一个类而不是接口。
而在你的程序中你不需要扩展TempConverter与JFrame
因此,您可以将其用作:
public class TempConverter extends ActionEvent implements ActionListener
Run Code Online (Sandbox Code Playgroud)
并扩展ActionEvent您将需要创建 TempConverter 的 Cunstructor,因为ActionEvent其中包含一个公共cunstructor 。
这是因为ActionEvent不是接口而是你写的implements ActionEvent。Java 编译器期望关键字后面有接口名称implements,这就是它的意思。
在你的情况下只需删除ActionEvent. 你根本不需要它。
| 归档时间: |
|
| 查看次数: |
17420 次 |
| 最近记录: |