n Listener 1 GUI Component Listener 2 n

  • Slides: 28
Download presentation

리스너 등록 n 하나의 컴포넌트에 여러 개의 리스너 등록 Listener 1 GUI Component Listener

리스너 등록 n 하나의 컴포넌트에 여러 개의 리스너 등록 Listener 1 GUI Component Listener 2 n 하나의 리스너를 여러 컴포넌트들에 등록 GUI Component 1 Listener 1 GUI Component 2 7

Action. Event n 버튼을 마우스로 클릭하면 Action. Event가 발생 예제 : Button. Applet. D.

Action. Event n 버튼을 마우스로 클릭하면 Action. Event가 발생 예제 : Button. Applet. D. java ………. 5 public class Button. Applet. D extends Applet implements Action. Listener { 6 Button button 1, button 2, button 3; 7 String msg = ""; 8 9 public void init() { 10 button 1 = new Button("One"); 11 button 1. add. Action. Listener(this); 12 button 1. set. Action. Command("One"); …………. 27 public void action. Performed (Action. Event e) { 28 String cmd = e. get. Action. Command(); 29 if(cmd. equals("One")) { 30 msg = "One"; 31 } else if(cmd. equals("Two")) { 32 msg = "Two"; ………….

Action. Event n 텍스트 필드에서 엔터 키를 입력하면 Action. Event 발생 예제 : Text.

Action. Event n 텍스트 필드에서 엔터 키를 입력하면 Action. Event 발생 예제 : Text. Field. Applet. D. java ………. 5 public class Text. Field. Applet. D extends Applet implements Action. Listener { 6 Text. Field tf 1, tf 2; 7 8 public void init() { 9 set. Layout(new Border. Layout()); 10 tf 1 = new Text. Field("How are you ? ", 10); 11 tf 1. add. Action. Listener(this); ……. 20 public void action. Performed(Action. Event e) { 21 System. out. println(e. get. Action. Command()); 22 if(e. get. Source() instanceof Text. Field) { 23 Text. Field t = (Text. Field) e. get. Source(); 24 if(t == tf 1) { 25 String msg = tf 2. get. Text(); ………. . .

Item. Event 예제 : Checkbox. Applet. D. java 1 import java. awt. *; 2

Item. Event 예제 : Checkbox. Applet. D. java 1 import java. awt. *; 2 import java. awt. event. *; 3 import java. applet. Applet; 4 5 public class Checkbox. Applet. D extends Applet implements Item. Listener { 6 Checkbox check 1, check 2, check 3 ; 7 String msg = ""; 8 9 public void init() { 10 set. Layout(new Flow. Layout(Flow. Layout. RIGHT)); 11 check 1 = new Checkbox("One"); 12 check 1. add. Item. Listener(this); 13 check 2 = new Checkbox("Two"); 14 check 2. add. Item. Listener(this); ……….

Item. Event 22 public void item. State. Changed(Item. Event e) { 23 if(e. get.

Item. Event 22 public void item. State. Changed(Item. Event e) { 23 if(e. get. Item(). equals("One")) { 24 msg = "Hello ? "; 25 } else if(e. get. Item(). equals("Two")) { 26 msg = (String)e. get. Item(); 27 } else if(e. get. Item(). equals("Three")) { 28 if(e. get. State. Change() == Item. Event. SELECTED) { 29 msg = e. get. Item() + " is selected. "; 30 } else { 31 msg = e. get. Item() + " is deselected. "; 32 } 33 } 34 repaint(); 35 } ………. . .

Item. Event n 라디오 버튼을 선택하는 경우에 Item. Event가 발생 예제 : Radio. Button.

Item. Event n 라디오 버튼을 선택하는 경우에 Item. Event가 발생 예제 : Radio. Button. D. java …. . . 5 public class Radio. Button. D extends Applet implements Item. Listener { 6 Checkbox. Group cbg; 7 Checkbox check 1, check 2, check 3; 8 String msg = ""; 9 10 public void init() { 11 cbg = new Checkbox. Group(); 12 add(check 1 = new Checkbox("One", cbg, true)); 13 check 1. add. Item. Listener(this); 14 add(check 2 = new Checkbox("Two", cbg, false)); ……… 20 public void item. State. Changed(Item. Event e) { 21 msg = (String) e. get. Item(); 22 repaint(); 23 }

Item. Event n 리스트의 아이템을 더블 클릭하는 경우에는 Action. Event가, 한번 클릭 하는 경우에는

Item. Event n 리스트의 아이템을 더블 클릭하는 경우에는 Action. Event가, 한번 클릭 하는 경우에는 Item. Event가 발생한다. 예제 : List. Applet. D. java 1 2 3 4 5 import java. awt. *; import java. awt. event. *; import java. applet. Applet; public class List. Applet. D extends Applet implements Action. Listener, Item. Listener { 6 List list; 7 String msg = ""; 8 9 public void init() { 10 list = new List(3, true); 11 list. add. Action. Listener(this); 12 list. add. Item. Listener(this); 13 list. add("One"); ……. . .

Item. Event 20 public void action. Performed(Action. Event e) { 21 msg = (String)

Item. Event 20 public void action. Performed(Action. Event e) { 21 msg = (String) e. get. Action. Command(); 22 repaint(); 23 } 24 25 public void item. State. Changed(Item. Event e) { 26 Component source = (Component) e. get. Source(); 27 if(e. get. State. Change() == Item. Event. SELECTED) 28 if(source instanceof List) 29 List l = (List) source; 30 msg = l. get. Item(((Integer)e. get. Item()). int. Value()) + " is selected. "; 31 } 32 }else { 33 msg = e. get. Item() + " is deselected. "; 34 } 35 repaint(); 36 } ……. . .

Window. Event n Window. Event는 다이얼로그/프레임/윈도우에서 발생할 수 있다. 예제 : Frame. ED. java

Window. Event n Window. Event는 다이얼로그/프레임/윈도우에서 발생할 수 있다. 예제 : Frame. ED. java 1 import java. awt. *; 2 import java. awt. event. *; 3 4 public class Frame. ED extends Frame implements Action. Listener { 5 Button exit; 6 7 public Frame. ED() { 8 super(); 9 add("South", exit = new Button("exit")); 10 exit. add. Action. Listener(this); 11 exit. set. Action. Command("exit"); 12 add. Window. Listener(new Window. Handler()); ……. . .

Window. Event 17 public void action. Performed(Action. Event e) { 18 String cmd =

Window. Event 17 public void action. Performed(Action. Event e) { 18 String cmd = e. get. Action. Command(); 19 if(cmd. equals("exit")) { 20 set. Visible(false); 21 dispose(); 22 System. exit(0); 23 } 24 } 25 26 public class Window. Handler extends Window. Adapter { 27 public void window. Closing(Window. Event e) { 28 Window w = e. get. Window(); 29 w. set. Visible(false); 30 w. dispose(); 31 System. exit(0); 32 } 33 } ……….

Mouse. Event 예제 : Draw. Rec. D. java ………. . 5 public class Draw.

Mouse. Event 예제 : Draw. Rec. D. java ………. . 5 public class Draw. Rec. D extends Applet { 6 int start. X, start. Y, w, h; 7 8 public void init() { 9 add. Mouse. Listener(new Mouse. Event. Handler()); 10 add. Mouse. Motion. Listener(new Mouse. Motion. Handler()); ………. . 21 public class Mouse. Motion. Handler extends Mouse. Motion. Adapter { 22 public void mouse. Dragged(Mouse. Event e) { 23 w = Math. abs(start. X - e. get. X()); ………. . 29 public class Mouse. Event. Handler extends Mouse. Adapter { 30 public void mouse. Pressed(Mouse. Event e) { 31 start. X = e. get. X(); 32 start. Y = e. get. Y(); 33 } ………. .