为什么编译器告诉我“actionPerformed方法未实现”?

huangapple go评论106阅读模式
英文:

Why does the compiler tell me "actionPerformed method not implemented"?

问题

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.*;
  4. import javax.swing.*;
  5. import javax.swing.Timer;
  6. import javafx.event.ActionEvent;
  7. public class hello {
  8. public static void main(String[] args) {
  9. TalkClock clock = new TalkClock();
  10. clock.start(1000, true);
  11. System.out.println("xxxx");
  12. JOptionPane.showMessageDialog(null, "Quit program?");
  13. System.exit(0);
  14. }
  15. }
  16. class TalkClock {
  17. public void start(int interval, boolean beep) {
  18. ActionListener listener = new ActionListener() {
  19. public void actionPerformed(ActionEvent event) {
  20. System.out.println("At the tone, the time is " + new Date());
  21. if (beep) Toolkit.getDefaultToolkit().beep();
  22. }
  23. };
  24. Timer t = new Timer(interval, listener);
  25. t.start();
  26. }
  27. }

错误信息:

  1. hello.java:24: 错误: <anonymous TalkClock$1> 不是抽象的, 并且不覆盖 抽象方法 actionPerformed(ActionEvent) ActionListener
  2. {
  3. ^
  4. 1 个错误
英文:
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.*;
  4. import javax.swing.*;
  5. import javax.swing.Timer;
  6. import javafx.event.ActionEvent;
  7. public class hello {
  8. public static void main(String[] args) {
  9. TalkClock clock = new TalkClock();
  10. clock.start(1000, true);
  11. System.out.println(&quot;xxxx&quot;);
  12. JOptionPane.showMessageDialog(null, &quot;Quit program?&quot;);
  13. System.exit(0);
  14. }
  15. }
  16. class TalkClock
  17. {
  18. public void start(int interval, boolean beep){
  19. ActionListener listener = new ActionListener()
  20. {
  21. public void actionPerformed(ActionEvent event) {
  22. System.out.println(&quot;At the tone, the time is &quot; + new Date());
  23. if (beep) Toolkit.getDefaultToolkit().beep();
  24. }
  25. };
  26. Timer t = new Timer(interval, listener);
  27. t.start();
  28. }
  29. }

I run it in my Mac. The error is

  1. hello.java:24: error: &lt;anonymous TalkClock$1&gt; is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
  2. {
  3. ^
  4. 1 error

答案1

得分: 2

你从JavaFX导入了ActionEvent。你需要导入swing类。

英文:

You import the ActionEvent from JavaFX. You need to import the swing class.

huangapple
  • 本文由 发表于 2020年4月5日 18:33:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/61041228.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定