2014-02-14 1 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 - OK 제가 할 것입니다. 감사!! – Coffee

답변

3

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 */ 
} 
} 
관련 문제