반응형

자바에서 각 이벤트는 하나의 이벤트 타입에 속하게 됩니다.

사용자가 JButton 컴포넌트를 클릭할 때 발생하는 actionPerformed 이벤트는 ActionEvent라고 하는 이벤트 타입에

속합니다. 여기에서 이벤트 타입은 EventObject 클래스에서 파생되는 클래스로 정의됩니다.

그리고 각 이벤트 타입에 속하는 모든 이벤트는 리스너 인터페이스의 멤버가 됩니다.

예를 들어 ActionEvent 이벤트 타입에 속하는 actionPerformed 이벤트는 ActionListener라고 하는 리스너

인터페이스의 멤버가 되는 것이지요.

 

결국 하나의 이벤트 타입에 대하여 하나의 리스터 인터페이스가 있는 셈입니다.

 

Interface

Class

ActionListener

ActionEvent

AdjustmentListener

AdjustmentEvent

AWTEventListener

AWTEvenetListenerProxy

ComponentListener

ComponentAdapter

ContainerListener

ComponentEvent

FocusListener

ContainerAdapter

HierachyBoundsListener

ContainerEvent

HierachyListener

FocusAdapter

InputMethodListener

FocusEvent

ItemListener

HierachyBoundsAdapter

KeyListener

HierachyEvent

MouseListener

InputEvent

MouseMotionListener

InputMethodEvent

MouseWheelListener

InvocationEvent

TextListener

ItemEvent

WindowFocusListener

KeyAdapter

WindowListener

KeyEvent

WindowStateListener

MouseAdapter

 

MouseEvent

 

MouseMotionAdapter

 

MouseWheelEvent

 

PaintEvent

 

TextEvent

 

WindowAdapter

 

WindowEvent

 

 

하나의 리스너 인터페이스에 여러 메소드 즉, 여러 이벤트가 포함될 수 있습니다.

예를 들어 창과 관련된 이벤트에서는 windowActivated, windowClosed, windowClosing, windowDeactivated,

windowDeiconified windowIconified, windowOpened 등이 있으며, 이들 이벤트는 모두 windowListener라고 하는

리스너 인터페이스의 멤버가 됩니다.

 

이러한 리스너 인터페이스를 구현하는 클래스를 어댑터(Adapter)라고 하며, 어댑터의 인스턴스를 리스너라고 합니다.

그리고 리스너 인터페이스를 구현하고 있는 리스너를 이벤트 소스에 등록시켜야 합니다.

이벤트 소스란 이벤트를 발생시키는 객체입니다.

public class MyPanel extends Jpanel implements ActionListener{

public void actionPerformed(ActionEvent e){

JoptionPane.showMessageDialog(

this,

getSource().ToString() + “ 를 클릭했습니다.”,

“actionPerformed 이벤트”,

JoptionPane.OK_OPTION);

}

public MyPanel(){

jButton = new Jbutton(“클릭하세요.”);

jButton.addActionListener(this);

}

}

 

public class MyPanel extends Panel{

ActionAdapter actionAdapter = new ActionAdapter();

public MyPanel(){

jButton = new Jbutton(“클릭하세요.”);

jButton.addActionListener(actionAdapter);

}

}

 

class WindowClosing extends WindowAdapter{

    public void windowClosing(WindowEvent e){

        System.exit(0);

    }

}

 

class MyFrame extends JFrame{

    public MyFrame(){

        WindowClosing listener = new WindowClosing();

        addWindowListener(listener);

    }

}

 

class MyFrame extends JFrame{

    public MyFrame(){

        addWindowListener(new WindowAdapter(){

            public void windowClosing(WindowEvent e){

                System.exit(0);

            }

        });

    }

}

 

* “예전에 누가 Adapter를 사용하면 리스너에 있는 method()를 모두 구현하지 않아도

되며 필요한 method()구현하면 된다고 했었는데, 사실은 Adapter 클래스를

상속받아서 새로운 리스너를 구현하는 것입니.”

+ Recent posts