2014-02-14 48 views
0

我有這樣的代碼:嘗試運行簡單的Java ActionListener示例並獲取錯誤?

import javax.swing.*; 
import java.util.*; 
import java.awt.*; 
public class Stu { 


public static void main(String[] args){ 
    int dl = 3000; 
    ActionListener taskPerformer = new ActionListener() { 
    public void actionPerformed(ActionEvent evt) { 
     System.out.print("do");//do a task 
    } 

}; 

} 

} 

,這讓我這個錯誤:

Stu.java:9: error: cannot find symbol 
ActionListener taskPerformer = new ActionListener() { 

我需要幫助瞭解如何工作的。 ActionListener是否需要GUI來運行?謝謝

+1

您尚未導入'ActionListener'。 –

+0

@SotiriosDelimanolis - 好的,我會這樣做的。謝謝!! – Coffee

回答

3

The ActionListener interface is in java.awt.event;導入它(ActionEvent相同)。

但即使你這樣做,你的代碼也不會做任何事情。沒有什麼是您的actionPerformed方法。只有在將ActionListener添加到其他產生ActionEvent的對象時,實現ActionListener纔有意義,這意味着要創建GUI,因爲其他GUI類支持此機制。

+0

非常感謝!你是對的,我跑了,但它什麼也沒做。好的,我會進一步研究。謝謝!! – Coffee

1

正如Sotirios所說,我需要導入ActionListener(也是,ActionEvent)。

import javax.swing.Timer; 
import javax.swing.*; 
import java.util.*; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
public class Stu { 


public static void main(String[] args){ 
    /* same as before */ 
} 
} 
相關問題