1 / 141

9. 이벤트 처리 그래픽프로그래밍

9. 이벤트 처리 그래픽프로그래밍. 이벤트 처리. 이벤트 (Events). 이벤트 발생 버튼을 클릭할 때 텍스트필드에 문자를 타이핑할 때 콤보박스에서 아이템을 선택할 때 타이머가 타임아웃될 때 , 등등 ... 버튼 , 텍스트필드 등 GUI 콤포넌트 들이 이벤트 소스 (source) 임. 이벤트 (Events). 프로그래머는 많은 이벤트 중 관심 있는 이벤트에 대해서 만 필요한 반응을 보이도록 프로그램 작성

moe
Télécharger la présentation

9. 이벤트 처리 그래픽프로그래밍

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 9. 이벤트 처리그래픽프로그래밍 강원대학교

  2. 이벤트 처리 강원대학교

  3. 이벤트 (Events) • 이벤트 발생 • 버튼을 클릭할 때 • 텍스트필드에 문자를 타이핑할 때 • 콤보박스에서 아이템을 선택할 때 • 타이머가 타임아웃될 때, 등등... • 버튼, 텍스트필드 등 GUI 콤포넌트들이 이벤트 소스(source)임 강원대학교

  4. 이벤트 (Events) • 프로그래머는 많은 이벤트 중 관심 있는 이벤트에 대해서만 필요한 반응을 보이도록 프로그램 작성 • 특정 GUI 콤포넌트에서 발생하는 특정 이벤트에 대해 어떤 반응을 보이도록 하려면 적절한 이벤트 리스너(listener)를 만들어 이를 해당 콤포넌트에 등록해 줌 • 이벤트 리스너 = 이벤트 처리기 강원대학교

  5. 이벤트 소스와 이벤트 리스너 • Event listener: • 이벤트가 발생될 때 그 사실을 통보받는 객체 • 프로그래머에 의해 작성되는 클래스 객체 • 이벤트가 발생되었을 때 해야 할 작업이 이곳에 정의되어 있음 • 이벤트 리스너가 등록되지 않은 이벤트에 대해서는 아무 반응도 없게 됨 • Event source: • 어떤 컴포넌트에서 이벤트가 발생되면 그 컴포넌트에 등록된 모든 이벤트 리스너들에게 이벤트 발생 사실이 통보됨 강원대학교

  6. 이벤트 소스와 이벤트 리스너 • 이벤트가 여러 리스너에게 전달될 수 있다. • 한 리스너가 여러 소스로부터 이벤트를 받을 수 있다. 강원대학교

  7. ActionListener 예 • 버튼을 클릭하면 action event 발생 (버튼: 이벤트 소스) • 이에 대해 반응을 보이게 하려면 ActionListener 객체를 버튼에 등록해 줌 (이 ActionListener 객체가이벤트 처리기임) • ActionListener 객체 = ActionListener 인터페이스를 구현한 객체 • 버튼 클릭에 따라 Action 이벤트가 발생하면 ActionListener의 actionPerformed 메소드가 실행됨 public interface ActionListener { void actionPerformed(ActionEvent event); } 강원대학교

  8. Output 버튼을 클릭하면 Action 이벤트가 발생함! ActionListener 강원대학교

  9. File ButtonTester.java public class ButtonViewer { public static void main(String[] args) { JFrame frame = new JFrame(); JButton button = new JButton("Click me!"); frame.add(button); button.addActionListener(new ClickListener()); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } 이벤트 처리기 (버튼에서 발생하는 Action 이벤트를 처리함) 강원대학교

  10. ActionListener(Action 이벤트 처리기) 구현 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ClickListener implements ActionListener { public void actionPerformed(ActionEvent event) { System.out.println("I was clicked."); } } • ClickListener 객체는 ActionListener 인터페이스를 구현하고 있음 • (ClickListener 객체는 ActionListener임) • ClickListener 객체를 이벤트 리스너로서 버튼에 등록해 주면 버튼 클릭 때마다 ClickListenr의 actionPerformed 메소드가 실행됨 강원대학교

  11. 이렇게 하는 것도 가능! public class ButtonViewer{ public static void main(String[] args){ JFrame frame = new JFrame(); JButton button = new JButton("Click me!"); frame.add(button); class ClickListener implements ActionListener{ public void actionPerformed(ActionEvent event){ System.out.println("I was clicked."); } } ActionListener listener = new ClickListener(); button.addActionListener(listener); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } local inner class 강원대학교

  12. 예 2 ActionListener 강원대학교

  13. public class ButtonViewer{ public static void main(String[] args){ JFrame frame = new JFrame(); JPanel panel = new JPanel(); JButton button = new JButton("Click me!"); final JTextField textField = new JTextField(20); panel.add(button); panel.add(textField); frame.add(panel); class ClickListener implements ActionListener{ public void actionPerformed(ActionEvent event){ textField.setText("I was clicked."); } } ActionListener listener = new ClickListener(); button.addActionListener(listener); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } local inner class 강원대학교

  14. public class ButtonViewer{ public static void main(String[] args){ JFrame frame = new JFrame(); JPanel panel = new JPanel(); JButton button = new JButton("Click me!"); final JTextField textField = new JTextField(20); panel.add(button); panel.add(textField); frame.add(panel); class ClickListener implements ActionListener{ public void actionPerformed(ActionEvent event){ textField.setText("I was clicked."); } } ... } } Local class 내에서 그 local class를 포함하고 있는 메소드의 지역변수를 사용하려면 그 지역변수가 final로 선언되어 있어야 한다. 강원대학교

  15. public class ButtonViewer{ public static void main(String[] args){ JFrame frame = new JFrame(); JPanel panel = new JPanel(); JButton button = new JButton("Click me!"); final JTextField textField = new JTextField(20); panel.add(button); panel.add(textField); frame.add(panel); ActionListener listener = new ClickListener(textField); button.addActionListener(listener); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } local inner class를 사용하지 않는 경우와 비교 강원대학교

  16. public class ClickListener implements ActionListener{ ClickListener(JTextField tf){ textField = tf; } public void actionPerformed(ActionEvent event){ textField.setText("I was clicked."); } JTextField textField; } local inner class를 사용하지 않는 경우와 비교 강원대학교

  17. 이렇게 하는 것도 가능! public class ButtonViewer{ public static void main(String[] args){ JFrame frame = new JFrame(); JPanel panel = new JPanel(); JButton button = new JButton("Click me!"); final JTextField textField = new JTextField(20); panel.add(button); panel.add(textField); frame.add(panel); ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent event){ textField.setText("I was clicked."); } }; button.addActionListener(listener); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } Anonymous inner class • ActionListener 인터페이스를 구현하는 클래스를 선언하면서 동시에 객체 구성 • 클래스 이름 없음 강원대학교

  18. Anonymous inner class ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent event){ textField.setText("I was clicked."); } }; ActionListener 인터페이스를 구현하는 클래스라는 선언 강원대학교

  19. Anonymous inner class ActionListener listener = new MouseAdaptor() { public void mouseClicked(MouseEvent event){ textField.setText("I was clicked."); } }; MouseAdaptor 클래스를 확장하여 구현하는 클래스라는 선언 (서브클래스) 강원대학교

  20. 예 3 • 버튼을 클릭할 때마다 이자가 더해지고 잔액이 화면에 표시됨 강원대학교

  21. 리스너 구현 class AddInterestListener implements ActionListener { public void actionPerformed(ActionEvent event) { double interest = account.getBalance() * INTEREST_RATE / 100; account.deposit(interest); label.setText("balance=" + account.getBalance()); } } 강원대학교

  22. File InvestmentViewer1.java 01: import java.awt.event.ActionEvent;02: import java.awt.event.ActionListener;03: import javax.swing.JButton;04: import javax.swing.JFrame;05: import javax.swing.JLabel;06: import javax.swing.JPanel;07: import javax.swing.JTextField;08: 09: /**10: This program displays the growth of an investment. 11: */12: public class InvestmentViewer113: { 14: public static void main(String[] args)15: { 16: JFrame frame = new JFrame();17: 강원대학교

  23. File InvestmentViewer1.java 18: // The button to trigger the calculation19: JButton button = new JButton("Add Interest");20: 21: // The application adds interest to this bank account22:final BankAccount account = new BankAccount(INITIAL_BALANCE);23: 24: // The label for displaying the results25:final JLabel label = new JLabel(26:"balance=" + account.getBalance());27: 28: // The panel that holds the user interface components29: JPanel panel = new JPanel();30: panel.add(button);31: panel.add(label); 32: frame.add(panel);33: 강원대학교

  24. File InvestmentViewer1.java 34: class AddInterestListener implements ActionListener35: { 36: public void actionPerformed(ActionEvent event) 37: { 38: double interest = account.getBalance() 39: * INTEREST_RATE / 100; 40:account.deposit(interest); 41:label.setText( 42: "balance=" + account.getBalance()); 43: } 44: } // inner class 이므로 account, label 사용 가능! 45: 46:ActionListener listener = new AddInterestListener(); 47: button.addActionListener(listener); 48: 49: frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); 50: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 51: frame.setVisible(true); 52: } 강원대학교

  25. File InvestmentViewer1.java 53: 54: private static final double INTEREST_RATE = 10; 55: private static final double INITIAL_BALANCE = 1000; 56: 57: private static final int FRAME_WIDTH = 400; 58: private static final int FRAME_HEIGHT = 100; 59: } 강원대학교

  26. 등록 callback AddInterestListener 객체 actionPerformed() 메소드 재계산 및 디스플레이 강원대학교

  27. 예 4 JTextField 강원대학교

  28. Processing Text Input class AddInterestListener implements ActionListener { public void actionPerformed(ActionEvent event) { double rate = Double.parseDouble(rateField.getText()); . . . } } 강원대학교

  29. File InvestmentViewer2.java 01: import java.awt.event.ActionEvent; 02: import java.awt.event.ActionListener; 03: import javax.swing.JButton; 04: import javax.swing.JFrame; 05: import javax.swing.JLabel; 06: import javax.swing.JPanel; 07: import javax.swing.JTextField; 08: 09: /** 10: This program displays the growth of an investment. 11: */ 12:public class InvestmentViewer2 13: { 14:public static void main(String[] args) 15: { 16: JFrame frame = new JFrame(); 17: 강원대학교

  30. File InvestmentViewer2.java 18: // The label and text field for entering the //interest rate 19: JLabel rateLabel = new JLabel("Interest Rate: "); 20: 21:final int FIELD_WIDTH = 10; 22:final JTextField rateField = new JTextField(FIELD_WIDTH); 23: rateField.setText("" + DEFAULT_RATE); 24: 25: // The button to trigger the calculation 26: JButton button = new JButton("Add Interest"); 27: 28: // The application adds interest to this bank account 29:final BankAccount account = new BankAccount(INITIAL_BALANCE); 30: 31: // The label for displaying the results 32:final JLabel resultLabel = new JLabel( 33: "balance=" + account.getBalance()); 34: 강원대학교

  31. File InvestmentViewer2.java 35: // The panel that holds the user interface components 36: JPanel panel = new JPanel(); 37: panel.add(rateLabel); 38: panel.add(rateField); 39: panel.add(button); 40: panel.add(resultLabel); 41: frame.add(panel); 42: 43:class AddInterestListener implements ActionListener 44: { 45:public void actionPerformed(ActionEvent event) 46: { 47:double rate = Double.parseDouble( 48: rateField.getText()); 49:double interest = account.getBalance() 50: * rate / 100; 51: account.deposit(interest); 강원대학교

  32. File InvestmentViewer2.java 52: resultLabel.setText( 53:"balance=" + account.getBalance()); 54: } 55: } 56: 57: ActionListener listener = new AddInterestListener(); 58: button.addActionListener(listener); 59: 60: frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); 61: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 62: frame.setVisible(true); 63: } 64: 65:private static final double DEFAULT_RATE = 10; 66:private static final double INITIAL_BALANCE = 1000; 67: 68:private static final int FRAME_WIDTH = 500; 69:private static final int FRAME_HEIGHT = 200; 70: } 강원대학교

  33. 타이머이벤트 처리 강원대학교

  34. Processing Timer Events • javax.swing.Timer는 일정 시간 간격으로 액션이벤트를 발생시킨다. • 객체 상태를 일정 시간 간격으로 갱신하고자 할 때 사용 • 타이머에게 액션이벤트 리스너를 등록해 놓으면 액션이벤트가 발생될 때마다 액션이벤트 리스너의 actionPerformed 메소드가 호출됨 - callback! class MyListener implements ActionListener{     void actionPerformed(ActionEvent event)   {      // 타이머에서 액션이벤트가 발생할 때마다 // 이곳에 적어주는 액션이 실행됨    }} 강원대학교

  35. Processing Timer Events • 액션 리스너를 타이머에 등록함 MyListener listener = new MyListener();Timer t = new Timer(interval, listener);t.start();// 타이머 쓰레드 시작 등록 MyListener actionPerformed Timer 주기적으로 이벤트 발생 actionPerformed 메소드 실행 (callback) 강원대학교

  36. Example: Countdown • Example: a timer that counts down to zero 강원대학교

  37. File TimeTester.java 01: import java.awt.event.ActionEvent; 02: import java.awt.event.ActionListener; 03: import javax.swing.JOptionPane; 04: import javax.swing.Timer; 05: 06: /** 07: This program tests the Timer class. 08: */ 09:public class TimerTester 10: { 11:public static void main(String[] args) 12: { 13:classCountDownimplementsActionListener 14: { 15:public CountDown(int initialCount) 16: { 17: count = initialCount; 18: } 강원대학교

  38. File TimeTester.java 19: 20:public void actionPerformed(ActionEvent event) 21: { 22:if (count >= 0) 23: System.out.println(count); 24:if (count == 0) 25: System.out.println("Liftoff!"); 26: count--; 27: } 28: 29:private int count; 30: } 31: 32: CountDown listener = new CountDown(10); 33: 34:final int DELAY = 1000; // Milliseconds between // timer ticks 강원대학교

  39. File TimeTester.java 35: Timer t = new Timer(DELAY, listener); 36: t.start(); 37: 38: JOptionPane.showMessageDialog(null, "Quit?"); 39: System.exit(0); 40: } 41: } 강원대학교

  40. 애니메이션 프로그램 기본 형식 class Mover implements ActionListener { public void actionPerformed(ActionEvent event) { // Move the rectangle } } ActionListener listener = new Mover(); final int DELAY = 100; // Milliseconds between timer ticks Timer t = new Timer(DELAY, listener); t.start(); 강원대학교

  41. Accessing Surrounding Variables • Inner 클래스의 메소드에서는 바깥 클래스의 변수에 접근할 수 있음 public static void main(String[] args) { . . . final Rectangle box = new Rectangle(5, 10, 20, 30); class Mover implements ActionListener { public void actionPerformed(ActionEvent event) { // Move the rectangle box.translate(1, 1); } } . . . } 강원대학교

  42. File TimeTester2.java 01: import java.awt.Rectangle; 02: import java.awt.event.ActionEvent; 03: import java.awt.event.ActionListener; 04: import javax.swing.JOptionPane; 05: import javax.swing.Timer; 06: 07: /** 08: This program uses a timer to move a rectangle once per second. 09: */ 10:public class TimerTester2 11: { 12:public static void main(String[] args) 13: { 14:final Rectangle box = new Rectangle(5, 10, 20, 30); 15: 16:class Mover implements ActionListener 17: { 강원대학교

  43. File TimeTester2.java 18:public void actionPerformed(ActionEvent event) 19: { 20: box.translate(1, 1); 21: System.out.println(box); 22: } 23: } 24: 25: ActionListener listener = new Mover(); 26: 27:final int DELAY = 100; // Milliseconds between timer ticks 28: Timer t = new Timer(DELAY, listener); 29: t.start(); 30: 31: JOptionPane.showMessageDialog(null, "Quit?"); 32: System.out.println("Last box position: " + box); 33: System.exit(0); 34: } 35: } 강원대학교

  44. File TimeTester2.java Output: java.awt.Rectangle[x=6,y=11,width=20,height=30] java.awt.Rectangle[x=7,y=12,width=20,height=30] java.awt.Rectangle[x=8,y=13,width=20,height=30] . . . java.awt.Rectangle[x=28,y=33,width=20,height=30] java.awt.Rectangle[x=29,y=34,width=20,height=30] Last box position: java.awt.Rectangle[x=29,y=34,width=20,height=30] 강원대학교

  45. 배치관리Layout Management 강원대학교

  46. 배치 관리 Layout Management • 컨테이너는 배치관리자(layout manager)를 갖는다. • 배치관리자 • border layout • flow layout • grid layout 강원대학교

  47. Layout Management • JPanel은 기본으로 FlowLayoutmanager를 갖는다. • FlowLayoutmanager는 컴포넌트를 옆으로 차례로 늘어 놓는다. • JPanel이 다른 종류의 layoutmanager를 사용하도록 할 수도 있다. JPanel panel1 = new Jpanel(); panel.setLayout(new BorderLayout()); 강원대학교

  48. Border Layout 강원대학교

  49. Border Layout • Frame (실제로는 frame의 content pane)의 기본 배치관리자는 BorderLayout • 컴포넌트를 넣는 법 • 전체 컨테이너 면적을 꽉 채우도록 각 컴포넌트의 크기를 늘여 조절함 JPanel panel1 = new Jpanel(); panel.setLayout(new BorderLayout()); panel.add(component, BorderLayout.NORTH); 강원대학교

  50. Grid Layout • 전체 영역을 격자형으로 구획하고 차례로 컴포넌트를 삽입 • 컴포넌트들이 같은 크기를 갖도록 크기 조정 JPanel numberPanel = new JPanel(); numberPanel.setLayout(new GridLayout(4, 3)); numberPanel.add(button7); numberPanel.add(button8); numberPanel.add(button9); numberPanel.add(button4); . . . 강원대학교

More Related