Presentation is loading. Please wait.

Presentation is loading. Please wait.

제7장 이벤트 프로그래밍.

Similar presentations


Presentation on theme: "제7장 이벤트 프로그래밍."— Presentation transcript:

1 제7장 이벤트 프로그래밍

2 7.1이벤트-구동 프로그래밍

3 이벤트-구동 프로그래밍 마우스 버튼 클릭, 더블 클릭, 마우스 이동 등에 응답하는 형태로 작성하는 프로그래밍
윈도우 환경에서는 대부분의 응용프로그램이 이벤트-구동 프로그래밍 방식으로 작성된다. 일반 프로그램 이벤트-구동 프로그램

4 이벤트-구동 프로그래밍 GUI 프로그램은 컴포넌트에서 발생된 이벤트를 처리해야 한다. GUI 프로그램 구성 이벤트 리스너
마우스 클릭과 같은 사용자 액션을 알리는 시그널. 리스너 컴포넌트로부터 발생된 이벤트를 기다리고 처리한다. 클래스 형태로 정의된다. GUI 프로그램 구성 the code that presents the GUI to the user the listeners that wait for events to occur the specific code that is executed when events occur

5 GUI 프로그램 모델 이벤트 리스너 이벤트 처리 리스너 등록 GUI 컴포넌트 이벤트 처리 코드 이벤트 효과

6 이벤트 리스너 Listener 인터페이스 이벤트 리스너(처리기)
각 이벤트에 대해 정의되어 있다. 각 Listener 인터페이스는 이벤트 처리에 필요한 추상 메소드를 포함하고 있다. 이벤트 리스너(처리기) 특정 Listener 인터페이스를 구현한 클래스 형태로 작성한다. 이벤트 리스너(처리기)는 GUI 컴포넌트에 등록(add)해야 한다. 컴포넌트에서 이벤트가 발생하면 해당 이벤트 리스너의 해당 메소드가 수행된다.

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

8 7.2 AWT 이벤트

9 AWT 이벤트 종류

10 AWT 이벤트 종류

11 예:버튼에서 액션 이벤트를 처리 ActionListener 인터페이스를 구현해야 한다.
actionPerformed() 메소드를 구현해야 한다. ActionListener 인터페이스를 구현한 클래스(이벤트 리스너)를 버튼에 등록해야 한다. 버튼 클릭이 발생하면 등록된 이벤트 처리기 클래스의 actionPerformed() 메소드가 호출되어 실행된다.

12 리스너 구현 및 등록 컴포넌트에 이벤트 리스너(처리기) 등록
Listener 인터페이스를 구현한 이벤드 처리기를 addXXXListener() 메소드를 이용해 등록한다. Listener 인터페이스를 미리 구현한 Adapter 클래스가 존재하는 경우 상속을 받아서 원하는 함수만 새로 정의하면 된다.

13 리스너 인테페이스 및 메소드

14 리스너 인테페이스 및 메소드

15 ActionEvent 버튼을 마우스로 클릭하면 ActionEvent가 발생 예제 : ButtonAppletD.java ……….
5 public class ButtonAppletD extends Applet implements ActionListener { Button button1,button2,button3; String msg = ""; 8 public void init() { button1 = new Button("One"); button1.addActionListener(this); button1.setActionCommand("One"); …………. public void actionPerformed (ActionEvent e) { String cmd = e.getActionCommand(); if(cmd.equals("One")) { msg = "One"; } else if(cmd.equals("Two")) { msg = "Two";

16 ActionEvent 텍스트 필드에서 엔터 키를 입력하면 ActionEvent 발생
예제 : TextFieldAppletD.java ………. 5 public class TextFieldAppletD extends Applet implements ActionListener { TextField tf1,tf2; 7 public void init() { setLayout(new BorderLayout()); tf1 = new TextField("How are you ?", 10); tf1.addActionListener(this); ……. public void actionPerformed(ActionEvent e) { System.out.println(e.getActionCommand()); if(e.getSource() instanceof TextField) { TextField t = (TextField) e.getSource(); if(t == tf1) { String msg = tf2.getText(); ………...

17 ItemEvent ItemEvent 체크박스 ItemEvent 처리 ItemListener 인터페이스
ItemListener 인터페이스를 구현한 이벤트 처리기를 addItemListener() 메소드를 이용해서 등록해야 한다. ItemListener 인터페이스 itemStateChanged() 메소드 체크박스 체크박스를 선택하는 경우에 ItemEvent가 발생

18 ItemEvent 예제 : CheckboxAppletD.java 1 import java.awt.*;
2 import java.awt.event.*; 3 import java.applet.Applet; 4 5 public class CheckboxAppletD extends Applet implements ItemListener { Checkbox check1, check2, check3 ; String msg = ""; 8 public void init() { setLayout(new FlowLayout(FlowLayout.RIGHT)); check1 = new Checkbox("One"); check1.addItemListener(this); check2 = new Checkbox("Two"); check2.addItemListener(this); ……….

19 ItemEvent 22 public void itemStateChanged(ItemEvent e) {
if(e.getItem().equals("One")) { msg = "Hello ?"; } else if(e.getItem().equals("Two")) { msg = (String)e.getItem(); } else if(e.getItem().equals("Three")) { if(e.getStateChange() == ItemEvent.SELECTED) { msg = e.getItem() + " is selected."; } else { msg = e.getItem() + " is deselected."; } } repaint(); } ………...

20 ItemEvent 라디오 버튼을 선택하는 경우에 ItemEvent가 발생 예제 : RadioButtonD.java …...
5 public class RadioButtonD extends Applet implements ItemListener { CheckboxGroup cbg; Checkbox check1, check2, check3; String msg = ""; 9 public void init() { cbg = new CheckboxGroup(); add(check1 = new Checkbox("One",cbg,true)); check1.addItemListener(this); add(check2 = new Checkbox("Two",cbg,false)); ……… public void itemStateChanged(ItemEvent e) { msg = (String) e.getItem(); repaint(); }

21 ItemEvent 리스트의 아이템을 더블 클릭하는 경우에는 ActionEvent가,한번 클릭하는 경우에는 ItemEvent가 발생한다. 예제 : ListAppletD.java 1 import java.awt.*; 2 import java.awt.event.*; 3 import java.applet.Applet; 4 5 public class ListAppletD extends Applet implements ActionListener, ItemListener { List list; String msg = ""; 8 public void init() { list = new List(3, true); list.addActionListener(this); list.addItemListener(this); list.add("One"); ……...

22 ItemEvent 20 public void actionPerformed(ActionEvent e) {
msg = (String) e.getActionCommand(); repaint(); } 24 public void itemStateChanged(ItemEvent e) { Component source = (Component) e.getSource(); if(e.getStateChange() == ItemEvent.SELECTED) if(source instanceof List) List l = (List) source; msg = l.getItem(((Integer)e.getItem()).intValue()) + " is selected."; } }else { msg = e.getItem() + " is deselected."; } repaint(); } ……...

23 WindowEvent WindowEvent는 다이얼로그/프레임/윈도우에서 발생할 수 있다. 예제 : FrameED.java
1 import java.awt.*; 2 import java.awt.event.*; 3 4 public class FrameED extends Frame implements ActionListener { Button exit; 6 public FrameED() { super(); add("South", exit = new Button("exit")); exit.addActionListener(this); exit.setActionCommand("exit"); addWindowListener(new WindowHandler()); ……...

24 WindowEvent 17 public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand(); if(cmd.equals("exit")) { setVisible(false); dispose(); System.exit(0); } } 25 public class WindowHandler extends WindowAdapter { public void windowClosing(WindowEvent e) { Window w = e.getWindow(); w.setVisible(false); w.dispose(); System.exit(0); } } ……….

25 MouseEvent 마우스 이벤트의 형태 마우스 이벤트 프로그래밍 마우스 클릭과 관련된 형태 마우스 이동에 관한 것
MouseListener 인터페이스에서 관리 마우스 이동에 관한 것 MouseMotionListener 인터페이스에서 관리 마우스 이벤트 프로그래밍 MouseListener 인터페이스와 MouseMotionListener 인터페이스는 모두 많은 메소드들을 선언하고 있다. 편의를 위해 각각 MouseAdapter와 MouseMotionAdapter 클래스들을 제공한다. 프로그래머는 인터페이스를 직접 구현하지 않고 두 클래스를 상속 받아서 이벤트 처리기를 작성할 수 있다.

26 MouseEvent 예제 : DrawRecD.java ………..
5 public class DrawRecD extends Applet { int startX, startY, w, h; 7 public void init() { addMouseListener(new MouseEventHandler()); addMouseMotionListener(new MouseMotionHandler()); public class MouseMotionHandler extends MouseMotionAdapter { public void mouseDragged(MouseEvent e) { w = Math.abs(startX - e.getX()); public class MouseEventHandler extends MouseAdapter { public void mousePressed(MouseEvent e) { startX = e.getX(); startY = e.getY(); }

27 7.3 GEditor JDK 1.1 이벤트 모델을 이용해서 그래픽 에디터를 만드는 예제 프로그램
버튼, 라벨, 텍스트영역, 텍스트필드 모양을 그려주는 그래픽 에디터 GEditor 에서는 각 그림들의 이동과 크기 변경이 가능

28 7.3 GEditor EDrawable 인터페이스 GEDrawableRectangle 클래스
그래픽 에디터에서 그림을 핸들링하기 위한 기본적인 메소드들을 선언 GEDrawableRectangle 클래스 그래픽 에디터에서 가장 많이 사용될 수 있는 그림 클래스 Bbutton, Blabel, BtextArea, BtextField 클래스 버튼, 라벨, 텍스트 영역, 텍스트 필드 모양을 그려주기 위한 클래스 GEditorCanvas 클래스 그림을 그려주는 캔버스 영역 GEditor 클래스 각 클래스를 모아 작성한 그래픽 에디터 GEditor 예제 프로그램은 간단하지만, 그래픽 에디터나 인터페이스 빌더 등의 응용 분야에 적용될 수 있을 것이다. 프로그램 Text pp


Download ppt "제7장 이벤트 프로그래밍."

Similar presentations


Ads by Google