๊ด€๋ฆฌ ๋ฉ”๋‰ด

IT’s Portfolio

[Java] Lecture - Day 12 ๋ณธ๋ฌธ

Development Study/Java

[Java] Lecture - Day 12

f1r3_r41n 2024. 3. 22. 15:50
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ–ฅ Basic Java - Day 12

๐Ÿ‘‰๐Ÿป [Chapter 09] ์ž๋ฐ”์˜ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ

์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(Event Driven Programming)

  • ์ด๋ฒคํŠธ ์ข…๋ฅ˜
    • ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ
    • ์„ผ์„œ๋กœ๋ถ€ํ„ฐ์˜ ์ž…๋ ฅ, ๋„คํŠธ์›Œํฌ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ ์†ก์ˆ˜์‹ 
    • ๋‹ค๋ฅธ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์ด๋‚˜ ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ๋กœ๋ถ€ํ„ฐ์˜ ๋ฉ”์‹œ์ง€
  • ์ด๋ฒคํŠธ์˜ ๋ฐœ์ƒ์— ์˜ํ•ด ํ”„๋กœ๊ทธ๋žจ ํ๋ฆ„์ด ๊ฒฐ์ •๋˜๋Š” ๋ฐฉ์‹
    • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฃจํ‹ด(event listener) ์‹คํ–‰
    • ํ”„๋กœ๊ทธ๋žจ ๋‚ด์˜ ์–ด๋–ค ์ฝ”๋“œ๊ฐ€ ์–ธ์ œ ์‹คํ–‰๋  ์ง€ ์ด๋ฒคํŠธ ๋ฐœ์ƒ์— ์˜ํ•ด ์ „์ ์œผ๋กœ ๊ฒฐ์ •
  • ๋ฐ˜๋Œ€ ๊ฐœ๋… : ๋ฐฐ์น˜ ์‹คํ–‰(Batch Programming)
    • ํ”„๋กœ๊ทธ๋žจ์˜ ๊ฐœ๋ฐœ์ž๊ฐ€ ํ”„๋กœ๊ทธ๋žจ์˜ ํ๋ฆ„์„ ๊ฒฐ์ •ํ•˜๋Š” ๋ฐฉ์‹
  • ์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜ ํ”„๋กœ๊ทธ๋žจ์˜ ๊ตฌ์กฐ
    • ๊ฐ ์ด๋ฒคํŠธ๋งˆ๋‹ค ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฆฌ์Šค๋„ˆ ์ฝ”๋“œ๋ฅผ ๋ณด์œ 
    • ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋“ค์˜ ์ง‘ํ•ฉ
  • GUI ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์€ ์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์œผ๋กœ ์ž‘์„ฑ๋จ
    • GUI ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ข…๋ฅ˜
      • Cpp MFC, C# GUI, Visual Basic, X Window, Android ๋“ฑ
      • Java AWT&Swing
  • ์ด๋ฒคํŠธ๊ฐ€ ์ฒ˜๋ฆฌ๋˜๋Š” ๊ณผ์ •
    • ์ด๋ฒคํŠธ ๋ฐœ์ƒ
    • ์ด๋ฒคํŠธ ๊ฐ์ฒด ์ƒ์„ฑ
      • ํ˜„์žฌ ๋ฐœ์ƒํ•œ ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ง„ ๊ฐ์ฒด
    • ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์— ์ž‘์„ฑ๋œ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์ฐพ๊ธฐ
    • ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์‹คํ–‰
      • ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ์— ์ด๋ฒคํŠธ ๊ฐ์ฒด ์ „๋‹ฌ
      • ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์ฝ”๋“œ ์‹คํ–‰

์ด๋ฒคํŠธ ๊ด€๋ จ ์šฉ์–ด

  • ์ด๋ฒคํŠธ ์†Œ์Šค(event source)
    • ์ด๋ฒคํŠธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ GUI ์ปดํฌ๋„ŒํŠธ
  • ์ด๋ฒคํŠธ ๊ฐ์ฒด
    • ๋ฐœ์ƒํ•œ ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ง„ ๊ฐ์ฒด
  • ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ
    • ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ, ํด๋ž˜์Šค๋กœ ์ž‘์„ฑ
    • ์ปดํฌ๋„ŒํŠธ์— ๋“ฑ๋ก๋˜์–ด์•ผ ์ž‘๋™ ๊ฐ€๋Šฅ
  • ์ด๋ฒคํŠธ ๋ถ„๋ฐฐ ์Šค๋ ˆ๋“œ
    • ๋™์ž‘
      • ์ž๋ฐ” ๊ฐ€์ƒ ๋จธ์‹ ์œผ๋กœ๋ถ€ํ„ฐ ์ด๋ฒคํŠธ์˜ ๋ฐœ์ƒ์„ ํ†ต์ง€๋ฐ›์Œ
      • ์ด๋ฒคํŠธ ์†Œ์Šค์™€ ์ด๋ฒคํŠธ ์ข…๋ฅ˜ ๊ฒฐ์ •
      • ์ ์ ˆํ•œ ์ด๋ฒคํŠธ ๊ฐ์ฒด ์ƒ์„ฑ, ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์ฐพ์•„ ํ˜ธ์ถœ
    • ๋ฌดํ•œ ๋ฃจํ”„๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์Šค๋ ˆ๋“œ

์ด๋ฒคํŠธ ๊ฐ์ฒด

  • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ, ๋ฐœ์ƒํ•œ ์ด๋ฒคํŠธ์— ๊ด€ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฐ์ฒด
  • ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ์— ์ „๋‹ฌ๋จ
    • ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์ฝ”๋“œ์—์„œ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ ์ƒํ™ฉ์„ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•จ
  • ๋ชจ๋“  ์ด๋ฒคํŠธ ๊ฐ์ฒด๋Š” java.util.EventObject ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์Œ
  • ์ด๋ฒคํŠธ ๊ฐ์ฒด๊ฐ€ ํฌํ•จํ•˜๋Š” ์ •๋ณด
    • ์ด๋ฒคํŠธ ์ข…๋ฅ˜
    • ์ด๋ฒคํŠธ ์†Œ์Šค
    • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ ํ™”๋ฉด ์ขŒํ‘œ
    • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ ์ปดํฌ๋„ŒํŠธ ๋‚ด ์ขŒํ‘œ
    • ๋ฒ„ํŠผ์ด๋‚˜ ๋ฉ”๋‰ด ์•„์ดํ…œ์— ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ ๋ฒ„ํŠผ์ด๋‚˜ ๋ฉ”๋‰ด ์•„์ดํ…œ์˜ ๋ฌธ์ž์—ด
    • ํด๋ฆญ๋œ ๋งˆ์šฐ์Šค ๋ฒ„ํŠผ ๋ฒˆํ˜ธ
    • ๋งˆ์šฐ์Šค์˜ ํด๋ฆญ ํšŸ์ˆ˜
    • ํ‚ค๊ฐ€ ๋ˆŒ๋Ÿฌ์กŒ๋‹ค๋ฉด ํ‚ค์˜ ์ฝ”๋“œ ๊ฐ’๊ณผ ๋ฌธ์ž ๊ฐ’
    • ์ฒดํฌ๋ฐ•์Šค์™€ ๋ผ๋””์˜ค๋ฒ„ํŠผ ๋“ฑ๊ณผ ๊ฐ™์€ ์ปดํฌ๋„ŒํŠธ์— ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค๋ฉด ์ฒดํฌ ์ƒํƒœ
  • ์ด๋ฒคํŠธ์— ๋”ฐ๋ผ ์กฐ๊ธˆ์”ฉ ๋‹ค๋ฅธ ์ •๋ณด ํฌํ•จ
    • ActionEvent ๊ฐ์ฒด : ์•ก์…˜ ๋ฌธ์ž์—ด
    • MouseEvent ๊ฐ์ฒด : ๋งˆ์šฐ์Šค ์œ„์น˜ ์ •๋ณด, ๋งˆ์šฐ์Šค ๋ฒ„ํŠผ, ํ•จ๊ป˜ ๋ˆŒ๋Ÿฌ์ง„ ํ‚ค ์ •๋ณด ๋“ฑ
    • ItemEvent ๊ฐ์ฒด : ์•„์ดํ…œ์˜ ์ฒดํฌ ์ƒํƒœ
  • ์ด๋ฒคํŠธ ์†Œ์Šค ์•Œ์•„๋‚ด๊ธฐ
    • Object getSource()
      • EventObject ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ
      • ๋ฐœ์ƒํ•œ ์ด๋ฒคํŠธ์˜ ์†Œ์Šค ์ปดํฌ๋„ŒํŠธ ๋ฆฌํ„ด
      • Object ํƒ€์ž…์œผ๋กœ ๋ฆฌํ„ดํ•˜๋ฏ€๋กœ ๋‹ค์šด์บ์ŠคํŒ…ํ•˜์—ฌ ์‚ฌ์šฉ
        • Object obj = event.getSource();
        • Jbutton b = (Jbutton)event.getSource();
      • ๋ชจ๋“  ์ด๋ฒคํŠธ ๊ฐ์ฒด์— ๋Œ€ํ•ด ์ ์šฉ
    • ์ด๋ฒคํŠธ ๊ฐ์ฒด์™€ ์ด๋ฒคํŠธ ์†Œ์Šค

์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ

  • ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ, ํด๋ž˜์Šค๋กœ ์ž‘์„ฑ
  • ์‚ฌ์šฉ์ž์˜ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์ž‘์„ฑ
    • ์ž๋ฐ”์˜ ๋ฆฌ์Šค๋„ˆ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ ๊ตฌํ˜„
    • ๋ฆฌ์Šค๋„ˆ ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋ชจ๋“  ์ถ”์ƒ ๋ฉ”์†Œ๋“œ ๊ตฌํ˜„
  • JDK์—์„œ๋Š” ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์ž‘์„ฑ์„ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ์ธํ„ฐํŽ˜์ด์Šค ์ œ๊ณต
    • java.awt.event.*
interface ActionListener {  // ์•„๋ž˜ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐœ๋ฐœ์ž๊ฐ€ ๊ตฌํ˜„ํ•ด์•ผ ํ•จ
    public void actionPerformed(ActionEvent e); // Action ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ ํ˜ธ์ถœ๋จ
}
  • ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ActionListener ์ธํ„ฐํŽ˜์ด์Šค
interface MouseListener {   // ์•„๋ž˜์˜ 5๊ฐœ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐœ๋ฐœ์ž๊ฐ€ ๊ตฌํ˜„ํ•ด์•ผ ํ•จ
    public void mousePressed(MouseEvent e); // ๋งˆ์šฐ์Šค ๋ฒ„ํŠผ์ด ๋ˆŒ๋Ÿฌ์ง€๋Š” ์ˆœ๊ฐ„ ํ˜ธ์ถœ
    public void mouseReleased(MouseEvent e);    // ๋ˆŒ๋Ÿฌ์ง„ ๋งˆ์šฐ์Šค ๋ฒ„ํŠผ์ด ๋–ผ์–ด์ง€๋Š” ์ˆœ๊ฐ„ ํ˜ธ์ถœ
    public void mouseClicked(MouseEvent e); // ๋งˆ์šฐ์Šค๊ฐ€ ํด๋ฆญ๋˜๋Š” ์ˆœ๊ฐ„ ํ˜ธ์ถœ
    public void mouseEntered(MouseEvent e); // ๋งˆ์šฐ์Šค๊ฐ€ ์ปดํฌ๋„ŒํŠธ ์œ„์— ์˜ฌ๋ผ๊ฐ€๋Š” ์ˆœ๊ฐ„ ํ˜ธ์ถœ
    public void mouseExited(MouseEvent e);  // ๋งˆ์šฐ์Šค๊ฐ€ ์ปดํฌ๋„ŒํŠธ ์œ„์—์„œ ๋‚ด๋ ค์˜ค๋Š” ์ˆœ๊ฐ„ ํ˜ธ์ถœ
}
  • ๋งˆ์šฐ์Šค ์กฐ์ž‘์— ๋”ฐ๋ฅธ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ MouseListener ์ธํ„ฐํŽ˜์ด์Šค

์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์ž‘์„ฑ ๊ณผ์ •

  • ์ด๋ฒคํŠธ์™€ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์„ ํƒ
  • ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ํด๋ž˜์Šค ์ž‘์„ฑ : ActionListener ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„
  • ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ๋“ฑ๋ก
    • ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•˜๊ณ ์ž ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์— ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ๋“ฑ๋ก
    • component.addXXXListener( listener )
      • XXX : ์ด๋ฒคํŠธ ๋ช…
      • listener : ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ๊ฐ์ฒด
    • ์ปดํฌ๋„ŒํŠธ๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ๋ฆฌ์Šค๋„ˆ๋“ค์„ ๋™์‹œ์— ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Œ
    • ์ปดํฌ๋„ŒํŠธ๋Š” ํ•œ ์ด๋ฒคํŠธ์— ๋Œ€ํ•ด ์„œ๋กœ ๋‹ค๋ฅธ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฆฌ์Šค๋„ˆ๋ฅผ ๋™์‹œ์— ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Œ
  • ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์ž‘์„ฑ ๋ฐฉ๋ฒ•
    • ๋…๋ฆฝ ํด๋ž˜์Šค๋กœ ์ž‘์„ฑ
      • ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์™„์ „ํ•œ ํด๋ž˜์Šค๋กœ ์ž‘์„ฑ
      • ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์—ฌ๋Ÿฌ ๊ณณ์—์„œ ์‚ฌ์šฉํ•  ๋•Œ ์ ํ•ฉ
    • ๋‚ด๋ถ€ ํด๋ž˜์Šค(inner class)๋กœ ์ž‘์„ฑ
      • ํด๋ž˜์Šค ์•ˆ์— ๋ฉค๋ฒ„์ฒ˜๋Ÿผ ํด๋ž˜์Šค ์ž‘์„ฑ
      • ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ํŠน์ • ํด๋ž˜์Šค์—์„œ๋งŒ ์‚ฌ์šฉํ•  ๋•Œ ์ ํ•ฉ
    • ์ต๋ช… ํด๋ž˜์Šค(anonymous class)๋กœ ์ž‘์„ฑ
      • ํด๋ž˜์Šค์˜ ์ด๋ฆ„ ์—†์ด ๊ฐ„๋‹จํžˆ ๋ฆฌ์Šค๋„ˆ ์ž‘์„ฑ
      • ํด๋ž˜์Šค์กฐ์ฐจ ๋งŒ๋“ค ํ•„์š” ์—†์ด ๋ฆฌ์Šค๋„ˆ ์ฝ”๋“œ๊ฐ€ ๊ฐ„๋‹จํ•œ ๊ฒฝ์šฐ์— ์ ํ•ฉ
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;

public class IndepClassListenerEx extends JFrame {
    public IndepClassListenerEx() {
        setTitle("Action Listener Example");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container c = getContentPane();
        c.setLayout(new FlowLayout());

        JButton b1 = new JButton("Action");
        b1.addActionListener(new MyActionListener());
        c.add(b1);

        setSize(400, 200);
        setVisible(true);
    }

    public static void main(String[] args) {
        new IndepClassListenerEx();
    }
}

class MyActionListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton b1 = (JButton)e.getSource();
        if(b1.getText().equals("Action"))
            b1.setText("์•ก์…˜");
        else
            b1.setText("Action");
    }
}
  • ๋…๋ฆฝ ํด๋ž˜์Šค๋กœ Action ์ด๋ฒคํŠธ์˜ ๋ฆฌ์Šค๋„ˆ ์ž‘์„ฑ
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;

public class InnerClassListenerEx extends JFrame {
    public InnerClassListenerEx() {
        setTitle("Action Listener Example");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container c = getContentPane();
        c.setLayout(new FlowLayout());

        JButton b1 = new JButton("Action");
        b1.addActionListener(new MyActionListener());
        c.add(b1);

        setSize(400, 200);
        setVisible(true);
    }

    private class MyActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JButton b1 = (JButton)e.getSource();
            if (b1.getText().equals("Action"))
                b1.setText("์•ก์…˜");
            else
                b1.setText("Action");
            setTitle(b1.getText());
        }
    }

    public static void main(String[] args) {
        new InnerClassListenerEx();
    }
}
  • ๋‚ด๋ถ€ ํด๋ž˜์Šค๋กœ Action ์ด๋ฒคํŠธ์˜ ๋ฆฌ์Šค๋„ˆ ์ž‘์„ฑ
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;

public class AnonymousClassListenerEx extends JFrame {
    public AnonymousClassListenerEx() {
        setTitle("Action Listener Example");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container c = getContentPane();
        c.setLayout(new FlowLayout());

        JButton b1 = new JButton("Action");
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton b1 = (JButton)e.getSource();
                if(b1.getText().equals("Action"))
                    b1.setText("์•ก์…˜");
                else
                    b1.setText("Action");
                setTitle(b1.getText());
            }
        });
        c.add(b1);

        setSize(400, 200);
        setVisible(true);
    }

    public static void main(String[] args) {
        new AnonymousClassListenerEx();
    }
}
  • ์ต๋ช… ํด๋ž˜์Šค๋กœ Action ์ด๋ฒคํŠธ์˜ ๋ฆฌ์Šค๋„ˆ ์ž‘์„ฑ
728x90
๋ฐ˜์‘ํ˜•

'Development Study > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Java] Lecture - Day 14  (0) 2024.04.05
[Java] Lecture - Day 13  (1) 2024.03.30
[Java] Lecture - Day 11  (0) 2024.03.15
[Java] Lecture - Day 10  (0) 2023.12.12
[Java] Lecture - Day 9  (1) 2023.12.12
Comments