英文:
Why does the compiler tell me "actionPerformed method not implemented"?
问题
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
import javafx.event.ActionEvent;
public class hello {
public static void main(String[] args) {
TalkClock clock = new TalkClock();
clock.start(1000, true);
System.out.println("xxxx");
JOptionPane.showMessageDialog(null, "Quit program?");
System.exit(0);
}
}
class TalkClock {
public void start(int interval, boolean beep) {
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("At the tone, the time is " + new Date());
if (beep) Toolkit.getDefaultToolkit().beep();
}
};
Timer t = new Timer(interval, listener);
t.start();
}
}
错误信息:
hello.java:24: 错误: <anonymous TalkClock$1> 不是抽象的, 并且不覆盖 抽象方法 actionPerformed(ActionEvent) 在 ActionListener 中
{
^
1 个错误
英文:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
import javafx.event.ActionEvent;
public class hello {
public static void main(String[] args) {
TalkClock clock = new TalkClock();
clock.start(1000, true);
System.out.println("xxxx");
JOptionPane.showMessageDialog(null, "Quit program?");
System.exit(0);
}
}
class TalkClock
{
public void start(int interval, boolean beep){
ActionListener listener = new ActionListener()
{
public void actionPerformed(ActionEvent event) {
System.out.println("At the tone, the time is " + new Date());
if (beep) Toolkit.getDefaultToolkit().beep();
}
};
Timer t = new Timer(interval, listener);
t.start();
}
}
I run it in my Mac. The error is
hello.java:24: error: <anonymous TalkClock$1> is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
{
^
1 error
答案1
得分: 2
你从JavaFX导入了ActionEvent。你需要导入swing类。
英文:
You import the ActionEvent from JavaFX. You need to import the swing class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论