1. 이벤트 핸들러
 1) 이벤트 소스 ex)버튼 
 2) 이벤트 ex)ActionEvent
 3) 이벤트 리스너 ex) addActionListener()
 4) 이벤트 핸들러  ex) actionPerformed(ActionEvent e)

2. ex) 혼자서 생성과 리스너 모두 실행하는 경우

 import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Button;
import java.awt.event.*;  //1. 패키지 임포트하자.
//2. 리스너 implements 하자.
public class EventModel3 implements WindowListener,ActionListener{
 private Frame f ;
 private Button btn ;
 private Font font;
 private void go(){
  f= new Frame("Event Model1");
  //4. 이벤트 소스에 이벤트 리스너등록하자.
  f.addWindowListener(this); //자신이 리스너가 된다.
  f.setLayout(new FlowLayout());
  font = new Font("Serif",Font.BOLD,30);
  btn = new Button("빨간색");
  btn.setFont(font);
  btn.addActionListener(this);
  btn.setBackground(Color.red);
  f.add(btn); 
  f.setSize(500, 400);
  f.setVisible(true);
 }
 //3. 이벤트 핸들러 메소드 오버라이드하자.
 public void windowClosing(WindowEvent e){ //종료 이벤트
  System.exit(0);
 } 
 public void actionPerformed(ActionEvent e){
  String str = e.getActionCommand();
  if (str.equals("빨간색")){
   f.setBackground(Color.red);
   btn.setLabel("파란색");
   btn.setBackground(Color.blue);
   btn.setForeground(Color.red);
   System.out.println( e.getActionCommand());
  }else{
   f.setBackground(Color.blue);
   btn.setLabel("빨간색");
   btn.setBackground(Color.red);
   btn.setForeground(Color.blue);
   System.out.println( e.getActionCommand());
  }   
 }  
 public void windowClosed (WindowEvent e){}
 public void windowOpened(WindowEvent e){}
 public void windowActivated(WindowEvent e){}
 public void windowDeactivated(WindowEvent e){}
 public void windowIconified(WindowEvent e){}
 public void windowDeiconified(WindowEvent e){} 
 public static void main(String[] args) {
  new EventModel3().go();
 }
}

 

2. 이벤트 종류. (자바는 이벤트 종류가 많지않다.)

 Action

 ActionListener

 actionPerformed(ActionEvent ActionEvent)

 Item

 ItemListener

 itemStateChanged(ItemEvent ItemEvent)

 Mouse

 MouseListener

 mousePressed(MouseEvent MouseEvent)
mouseReleased(MouseEvent MouseEvent)
mouseEntered(MouseEvent MouseEvent)
mouseExited(MouseEvent MouseEvent)
mouseClicked(MouseEvent MouseEvent)

 MouseMotion

 MouseMotionLi
stener

 mouseDragged(MouseEvent MouseEvent)
 mouseMoved(MouseEvent MouseEvent)

 Key

 KeyListener

 keyPressed(KeyEvent KeyEvent)

 keyReleased(KeyEvent KeyEvent)

 keyTyped(KeyEvent KeyEvent)

 Focus

 FocusListener

 focusGained(FocusEvent FocusEvent)

 focusLost(FocusEvent FocusEvent)

 Adjustment

 AdjustmentListener

 adjustmentValueChanged(Adjustment AdjustmentEvent Event)

 Component

 ComponnetListener

 componentMoved(ComponentEvent ComponentEvent)

 componentHidden(ComponentEventcomponentHidden(ComponentEvent)

 componentResized(ComponentEventcomponentResized(ComponentEvent)

 componentShown(ComponentEvent ComponentEvent)

 Window

 WindowListener

 windowClosing(WindowEvent WindowEvent) 

 windowOpened(WindowEvent WindowEvent)

 windowIconified(WindowEvent WindowEvent)

 windowDeiconfied(WindowEvent WindowEvent)

 windowClosed(WindowEvent WindowEvent)

 windowActivated(WindowEvent WindowEvent)

 windowDeactivated(WindowEvent WindowEvent)

 Container

 ContainerListener

 componentAdded(ContainerEvent ContainerEvent)

 componentRemoved(ContainerEvent ContainerEvent)

 Text

 TextListener

 textValueChanged(TextEvent TextEvent)

 


3. Adapter

 1) 이벤트 Listener들을 이미 상속받은 클래스로 필요한 메소드만 재정의 하여 쓸 수 있게된다. (코드 간소화)


'Programming > JAVA' 카테고리의 다른 글

Java Call by value, Call by reference  (2) 2011.03.24
The AWT Component Library  (1) 2011.03.24
Building GUIs & Java Graphics  (0) 2011.03.24
Java Core Packages  (2) 2011.03.24
Java Collections Framework  (2) 2011.03.24

+ Recent posts