Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., Java 기초 ( 컴포넌트와 이벤트 처리 프로그래밍 ) 2009. 11. 27 Choi,

Slides:



Advertisements
Similar presentations
Inner Classes in Java CMSC 432 Shon Vick. 2 Conceptual Structure package p 1 class A 1 class A 2... class A n... package n class A 1 class A 2... class.
Advertisements

Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
For IST410 Students only Events-1 Event Handling.
Event-Driven Programming
CS3157 Java UI Recitation. Material Covered: Overview of AWT components, Event Handling, creating applets, and sample UI. Not covered in recitation: Drawing,
OOP Java1 Event Handling Overview Listeners, Adapters and Event Sources Inner classes Event Handling Details Applets and GUI Applications Event.
Intermediate Java1 An example that uses inner classes Week Four Continued.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
GUI Event Handling Nithya Raman. What is an Event? GUI components communicate with the rest of the applications through events. The source of an event.
9/21/99www.cs.vt.edu/wwtut/1 Introduction To Abstract Windowing Toolkit (AWT) Part 3 – AWT Marc Abrams Virginia Tech CS Dept courses.cs.vt.edu/wwwtut/
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Events Here, we review event handling one more time. To understand how events work in Java, we have to look closely at how we use GUIs. When you interact.
Veri Transferi Kes ve Yapıştır 1. import java.awt.*; import java.awt.datatransfer.*; import java.awt.event.*; /** * This program demonstrates how to add.
1 CSE 331 More Events (Mouse, Keyboard, Window, Focus, Change, Document...) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D.
Java Programming 1 Java Programming II Events, AWT, and Swing.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.
Event Handling Mohanraj S AP / IT Angel College of Engg & Tech.,
OOP (Java): GUI II/ OOP Objectives – –describe some more GUI features: JPanel, and mouse listeners/adapters Semester 2,
GUI 프로그래밍과 AWT(2) 컴퓨터 공학실험 (I) 인공지능 연구실. 2 AWT Event Model  Java 의 Event Model 1.Event : 사용자가 Program 에 가한 행동 2.Event Source : Event 가 일어난 근원 3.Event.
Java's Graphical User Interface Toolkit
Graphics in Java CS 21b. The paint() Method Method in a visual component that specifies what that component looks like Particularly important for applets,
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
Event Handling. Event Driven Programming Flow of programs is determined by events. The Operating system recognizes events and passes them to the particular.
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
© Marty Hall, Larry Brown Web core programming 1 Handling Mouse and Keyboard Events.
Event Handling. The signals that a program receives from the operating system as a result of the actions are called events. A window based program is.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Events in JAVA Needed for Applets and GUI based programs. Applet uses GUI to interact with user. Most of the events are generated by Mouse, Keyboard.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Canvas Button Event Check Box Choice Label Object Component List
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CS Lecture 04 Mice Lynda Thomas
13. Advanced GUI Components
2/17/2016 DEPT OF CSE 1 EVENT HANDLING. 2/17/2016 DEPT OF CSE 2 Event handling is at the core of successful applet programming There are several types.
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Event Listeners ActionListener –button,list AdjustmentListener-scroll bar ComponentListener-when component hidden…. ContainerListener-comp added or removed.
Event and GUI programming 1. Event Handling in Java An object resides in a particular state until it is made to transit to other state. This transition.
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Event Handling and Listeners in SWING The practice of event handling.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
IB103 Week 9 Free Standing Programs Chapter 18. Applets vs. Applications Applications are free standing See Program “Greeting” which prints out Hello.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
Events and Event Handling
Chapter 14 Event-Driven Programming
Lecture 8 Object Oriented Programming Using Java
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
CHAPTER Reacting to the user.
Chapter 12 Event-Driven Programming
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Event Handling Chapter 2 Objectives
Java Programming Language (6)
INFSY 547: WEB-Based Technologies
Computer Science 209 Graphics and GUIs.
GUI III IS
Programming in Java Event Handling
GUI Event Handling Nithya Raman.
CSE Software Engineering Fall 1999 Updated by J. Brown
Event-driven programming for GUI
Unit I: Chapter 1 Event handling.
Java 기초 (AWT 컴포넌트와 이벤트 처리 프로그래밍2)
Chapter 16 Event-Driven Programming
Programming Graphical User Interface (GUI)
Presentation transcript:

Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., Java 기초 ( 컴포넌트와 이벤트 처리 프로그래밍 ) Choi, Namseok

2 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., Last  Panel 클래스  주변 클래스들  AWT 관련 컴포넌트

3 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., Contents  AWT 관련 컴포넌트  AWT 컴포넌트 이벤트 처리  Event 관련 패키지와 적용범위  Event 작성법  자주 사용되는 Event 클래스

4 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., Event 관련 패키지와 적용범위  Event Handler 란 ? (java.awt.event)  각 컴포넌트에 대해 특정 행위를 하였을 때에 대한 작업을 처리할 수 있는 것.  각 컴포넌트 별 처리 이벤트  add 로 시작하여 Listener 로 끝나는 메서드  이벤트 관련 클래스  Listener 인터페이스, Adapter 클래스, Event 클래스

5 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., Event 작성법  Event Handler 클래스 생성법 4 가지  Listener 클래스를 구현하는 방법  Adapter 클래스를 상속받는 방법  Frame 클래스에 Listener 를 구현하는 방법  익명 중첩 클래스를 사용하는 방법  Event 작성법  처리하고자 하는 종류의 Event 클래스 생성  컴포넌트에 대해 관련 Event 를 추가  이벤트 관련 클래스  Listener 인터페이스, Adapter 클래스, Event 클래스

6 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., Event 작성법  Listener 클래스를 구현하는 방법

7 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., Event 작성법  Listener 클래스를 구현하는 방법 import java.awt.*; import java.awt.event.*; class Exam_01_Sub extends Frame{ private Button bt = new Button(" 확인 "); private GridBagLayout gbl = new GridBagLayout(); public Exam_01_Sub(String title) { super(title); this.init();// 화면초기화 this.start();// Event 추가 or Thread 추가 }

8 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., Event 작성법  Listener 클래스를 구현하는 방법 public void init() { this.setLayout(gbl); this.add(bt); } public void start() { A ap = new A(); bt.addActionListener(ap);// 버튼이 클릭하는 이벤트가 일어나 게 되면 ?? } class A implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); }

9 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., Event 작성법  Adapter 클래스를 상속받는 방법

10 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., Event 작성법  Adapter 클래스를 상속받는 방법 import java.awt.*; import java.awt.event.*; class Exam_01_Sub extends Frame{ private Button bt = new Button(" 확인 "); private GridBagLayout gbl = new GridBagLayout(); public Exam_01_Sub(String title) { super(title); this.init();// 화면초기화 this.start();// Event 추가 or Thread 추가 }

11 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., Event 작성법  Adapter 클래스를 상속받는 방법 public void init() { this.setLayout(gbl); this.add(bt); } public void start() { A ap = new A(); bt.addMouseListener(ap);// 버튼이 클릭하는 이벤트가 일어나 게 되면 ?? } class A extends MouseAdapter { public void mouseClicked(MouseEvent e) { System.exit(0); }

12 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., Event 작성법  Frame 클래스에 Listener 를 구현하는 방법 import java.awt.*; import java.awt.event.*; class Exam_01_Sub extends Frame implements MouseListener{ private Button bt = new Button(" 확인 "); private GridBagLayout gbl = new GridBagLayout(); public Exam_01_Sub(String title) { super(title); this.init();// 화면초기화 this.start();// Event 추가 or Thread 추가 }

13 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., Event 작성법  Frame 클래스에 Listener 를 구현하는 방법 public void init() { this.setLayout(gbl); this.add(bt); } public void start() { bt.addMouseListener(this);// 버튼이 클릭하는 이벤트가 일어나 게 되면 ?? } public void mouseClicked(MouseEvent e) { System.exit(0); } public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} }

14 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., Event 작성법  익명 중첩 클래스를 사용하는 방법 import java.awt.*; import java.awt.event.*; class Exam_01_Sub extends Frame { private Button bt = new Button(" 확인 "); private GridBagLayout gbl = new GridBagLayout(); public Exam_01_Sub(String title) { super(title); this.init();// 화면초기화 this.start();// Event 추가 or Thread 추가 }

15 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., Event 작성법  익명 중첩 클래스를 사용하는 방법 public void init() { this.setLayout(gbl); this.add(bt); } public void start() { MouseAdapter ma = new MouseAdapter() { public void mouseClicked(MouseEvent e) { System.exit(0); } }; bt.addMouseListener(ma);// 버튼이 클릭하는 이벤트가 일어나 게 되면 ?? }

16 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., 자주 사용되는 Event 클래스  Event 활용 범위  ActionListener : 버튼클릭, 메뉴선택 등  WindowAdapter 와 WindowListener : Frame 관련  MouseAdapter 와 MouseListener : 마우스 클릭 등 마우스 관련  MouseMotionAdapter 와 MouseMotionListener : 마우스 움직임 관련  KeyAdapter 와 KeyListener : 키 관련  FocusAdapter 와 FocusListener : 포커스 관련  ItemListener : List 나 Choice 에서의 아이템 관련

17 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., 자주 사용되는 Event 클래스  ActionListener : 버튼클릭, 메뉴선택 등 import java.awt.*; import java.awt.event.*; class Exam_01_Sub extends Frame { private Button bt = new Button(" 확인 "); private GridBagLayout gbl = new GridBagLayout(); public Exam_01_Sub(String title) { super(title); this.init();// 화면초기화 this.start();// Event 추가 or Thread 추가 }

18 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., 자주 사용되는 Event 클래스  ActionListener : 버튼클릭, 메뉴선택 등 import java.awt.*; import java.awt.event.*; class Exam_02_Sub extends Frame implements ActionListener{ private MenuBar mb = new MenuBar(); private Menu file = new Menu("FILE"); private MenuItem fopen = new MenuItem("OPEN"); private MenuItem fsave = new MenuItem("SAVE"); private FileDialog fdlg1 = new FileDialog(this, " 내꺼 열기 ", FileDialog.LOAD); private FileDialog fdlg2 = new FileDialog(this, " 내꺼 저장 ", FileDialog.SAVE);

19 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., 자주 사용되는 Event 클래스  ActionListener : 버튼클릭, 메뉴선택 등 public Exam_02_Sub(String title) { super(title); this.init(); this.start(); super.setSize(300, 200); Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frm = super.getSize(); int xpos = (int)(screen.getWidth() / 2 - frm.getWidth() / 2); int ypos = (int)(screen.getHeight() / 2 - frm.getHeight() / 2); super.setLocation(xpos, ypos); super.setResizable(false); super.setVisible(true); }

20 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., 자주 사용되는 Event 클래스  ActionListener : 버튼클릭, 메뉴선택 등 public void init() { file.add(fopen); file.add(fsave); mb.add(file); this.setMenuBar(mb); } public void start() { fopen.addActionListener(this); fsave.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource() == fopen) fdlg1.setVisible(true); else if(e.getSource() == fsave) fdlg2.setVisible(true); }

21 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., 자주 사용되는 Event 클래스  ActionListener : 버튼클릭, 메뉴선택 등 public class Exam_02 { public static void main(String[] ar) { Exam_02_Sub ex = new Exam_02_Sub(" 제목 "); }

22 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., 자주 사용되는 Event 클래스  WindowAdapter 와 WindowListener : Frame 관련 import java.awt.*; import java.awt.event.*; class Exam_02_Sub extends Frame implements ActionListener,WindowListener { public void start() { this.addWindowListener(this); } public void windowClosing(WindowEvent e) { System.exit(0); } 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) {} }

23 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., 자주 사용되는 Event 클래스  MouseAdapter 와 MouseListener : 마우스 클릭 등 마우스 관련 import java.awt.*; import java.awt.event.*; class Exam_03_Sub extends Frame implements MouseListener, MouseMotionListener{ private Label lb = new Label("x = 000, y = 000"); private Button bt = new Button(" 확인 "); private BorderLayout bl = new BorderLayout(); public Exam_03_Sub(String title) { super(title); this.init(); this.start();

24 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., 자주 사용되는 Event 클래스  MouseAdapter 와 MouseListener : 마우스 클릭 등 마우스 관련 super.setSize(300, 200); Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frm = super.getSize(); int xpos = (int)(screen.getWidth() / 2 - frm.getWidth() / 2); int ypos = (int)(screen.getHeight() / 2 - frm.getHeight() / 2); super.setLocation(xpos, ypos); super.setResizable(false); super.setVisible(true); } public void init() { this.setLayout(bl); this.add("North", lb); this.add("South", bt); }

25 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., 자주 사용되는 Event 클래스  MouseAdapter 와 MouseListener : 마우스 클릭 등 마우스 관련 public void start() { bt.addMouseListener(this); this.addMouseMotionListener(this); } public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) { if(e.getSource() == bt) { bt.setLabel(" 버튼위에 마우스가 있네요 !"); } public void mouseExited(MouseEvent e) { if(e.getSource() == bt) { bt.setLabel(" 확인 "); }

26 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., 자주 사용되는 Event 클래스  MouseAdapter 와 MouseListener : 마우스 클릭 등 마우스 관련 public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseDragged(MouseEvent e) { if(e.getSource() == this) { int x = e.getX(); int y = e.getY(); lb.setText("x = " + x + ", y = " + y); } public void mouseMoved(MouseEvent e) {} } public class Exam_03 { public static void main(String[] ar) { Exam_03_Sub ex = new Exam_03_Sub(" 제목 "); }

27 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., 자주 사용되는 Event 클래스  KeyAdapter 와 KeyListener : 키 관련  FocusAdapter 와 FocusListener : 포커스 관련  ItemListener : List 나 Choice 에서의 아이템 관련 class Exam_04_Sub extends Frame implements FocusListener, KeyListener, ItemListener{ private TextField tf1 = new TextField(); private TextField tf2 = new TextField(); private Label lb1 = new Label("NONE : ", Label.RIGHT); private Label lb2 = new Label("NONE"); private Choice ch = new Choice(); private Label lb3 = new Label("NONE"); private GridLayout gl = new GridLayout(4, 1); private Panel p1 = new Panel(); private GridLayout gl1 = new GridLayout(1, 2, 5, 5); private GridLayout gl2 = new GridLayout(1, 2, 5, 5); private Panel p2 = new Panel();

28 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., 자주 사용되는 Event 클래스  키 관련, 포커스 관련, List 나 Choice 에서의 아이템 관련 public Exam_04_Sub(String title) { super(title); this.init(); this.start(); super.setSize(250, 120); Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frm = super.getSize(); int xpos = (int)(screen.getWidth() / 2 - frm.getWidth() / 2); int ypos = (int)(screen.getHeight() / 2 - frm.getHeight() / 2); super.setLocation(xpos, ypos); super.setResizable(false); super.setVisible(true); }

29 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., 자주 사용되는 Event 클래스  키 관련, 포커스 관련, List 나 Choice 에서의 아이템 관련 public void init() { this.setLayout(gl); p1.setLayout(gl1); p1.add(tf1); p1.add(tf2); this.add(p1); p2.setLayout(gl2); p2.add(lb1); p2.add(lb2); this.add(p2); ch.add("NONE"); for(char c = 'A'; c <= 'Z'; c++) { String str = "" + c + c + c; ch.add(str); } this.add(ch); this.add(lb3); }

30 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., 자주 사용되는 Event 클래스  키 관련, 포커스 관련, List 나 Choice 에서의 아이템 관련 public void start() { tf1.addFocusListener(this); tf2.addFocusListener(this); tf1.addKeyListener(this); tf2.addKeyListener(this); ch.addItemListener(this); } public void focusGained(FocusEvent e) { if(e.getSource() == tf1) { lb1.setText(" 첫번째 TextField 위치 : "); } else if(e.getSource() == tf2) { lb1.setText(" 두번째 TextField 위치 : "); }

31 Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., 자주 사용되는 Event 클래스  키 관련, 포커스 관련, List 나 Choice 에서의 아이템 관련 public void focusLost(FocusEvent e) {} public void keyPressed(KeyEvent e) {} public void keyReleased(KeyEvent e) { if(e.getSource() == tf1) { int cnt = tf1.getText().trim().length(); lb2.setText(cnt + " 개 "); } else if(e.getSource() == tf2) { int cnt = tf2.getText().trim().length(); lb2.setText(cnt + " 개 "); } public void keyTyped(KeyEvent e) {} public void itemStateChanged(ItemEvent e) { if(e.getSource() == ch) { String str = ch.getSelectedItem(); lb3.setText(" 선택된 내용 = " + str); }}}