这是我编的一个计时器程序:importjava.awt.*;importjava.awt.event.*;importjava.util.*;importjavax.swing.*;importjavax.swing.Timer;publicclasstimeplus{publicstaticvoidmain(S...
这是我编的一个计时器程序:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
public class timeplus
{
public static void main(String[] args)
{
ActionListener listener=new TimePrinter();
Timer t=new Timer(1000,listener);
t.start();
JOptionPane.showMessageDialog(null, "Quit program");
System.exit(0);
}
}
class TimePrinter implements ActionListener
{
public void actionformed(ActionEvent event)
{
Date now=new Date();
System.out.println(now);
Toolkit.getDefaultToolkit().beep();
}
}
编译时报错:
Exception occurred during event dispatching:
java.lang.Error: Unresolved compilation problem:
The type TimePrinter must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)
at TimePrinter.actionPerformed(timeplus.java:18)
at javax.swing.Timer.fireActionPerformed(Unknown Source)
at javax.swing.Timer$DoPostEvent.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.Dialog$1.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
请问这是怎么回事?正确的程序?
展开
本文介绍了一个简单的Java计时器程序,利用javax.swing.Timer类来定时执行任务。程序中定义了一个TimePrinter类实现ActionListener接口,但在编译时出现错误,提示TimePrinter类未实现actionPerformed方法。错误的根源在于遗漏了该接口要求实现的方法。
256

被折叠的 条评论
为什么被折叠?



