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

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

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(&quot;xxxx&quot;);
    JOptionPane.showMessageDialog(null, &quot;Quit program?&quot;);
    System.exit(0);
    
  }
}

class TalkClock
{
  public void start(int interval, boolean beep){
    ActionListener listener = new ActionListener() 
      {
        
        public void actionPerformed(ActionEvent event) {
          System.out.println(&quot;At the tone, the time is &quot; + 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: &lt;anonymous TalkClock$1&gt; 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.

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:

确定