๐Ÿ’ป ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ

12086 ๋‹จ์–ด JavaJava

๐Ÿ“Œ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ์˜ ๊ฐœ์š”

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

1. ์ด๋ฒคํŠธ ๋ฆฌ์Šคํ„ฐ ํด๋ž˜์Šค ์ž‘์„ฑ

ActionListener์—์„œ actionPerformed()๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋งŒ์ด ์ •์˜๋˜์–ด ์žˆ๋‹ค.
์ด ๋ฉ”์†Œ๋“œ๋Š” ์•ก์…˜์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋œ๋‹ค.
ActionListener ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” MyListener๋ผ๊ณ  ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

class MyListener implements ActionListener {
//์•ก์…˜ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํ˜ธ์ถœ๋œ๋‹ค.
    public void actionPerformed(ActionEvent e) {
    	...	//Action ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ์—ฌ๊ธฐ์— ๋“ค์–ด๊ฐ„๋‹ค.
    }
}

2. ์ด๋ฒคํŠธ ๋ฆฌ์Šคํ„ฐ ์ด๋ฒคํŠธ ์†Œ์Šค์— ๋“ฑ๋กํ•œ๋‹ค.

์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋Š” ๊ฐ์ฒด๋ฅผ ์ด๋ฒคํŠธ ์†Œ์Šค์— ๋“ฑ๋กํ•˜๋Š” ๋‹จ๊ณ„

public class MyFrame  extends JFrame   //ํ”„๋ ˆ์ž„ ์ƒ์†ํ•ด์„œ MyFrame ์„ ์–ธ
    public MyFrame() {  //์ƒ์„ฑ์ž ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ถ”๊ฐ€ํ•œ๋‹ค.
    {  
    	JButton button = new JButton();     //๋ฒ„ํŠผ์ƒ์„ฑ
        button.addActionListener(new MyListener()); 
        //์ด๋ฒคํŠธ๋ฆฌ์Šค๋„ˆ ๊ฐ์ฒด๋ฅผ new ๋ฅผ ์ด์šฉํ•˜์—ฌ์„œ ์ƒ์„ฑํ•˜๊ณ  ๋ฒ„ํŠผ์— ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ๊ฐ์ฒด๋ฅผ ๋“ฑ๋ก
    }
}

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

์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋Š” ์ด๋ฒคํŠธ ๊ฐ์ฒด๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ฐ€์ง„๋‹ค.์ด๋ฒคํŠธ ๊ฐ์ฒด๋ฅผ ๋ฐœ์ƒํ•œ ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ๋ชจ๋“  ์ •๋ณด๋ฅผ ๋ฆฌ์Šค๋„ˆ๋กœ ์ „๋‹ฌ
๋ชจ๋“  ๊ฐ์ฒด๋Š” EventObject ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค.
EventObject ํด๋ž˜์Šค๋Š” getSource()๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ ์ด ๋ฉ”์†Œ๋“œ๋Š” ์ด๋ฒคํŠธ ๋ฐœ์ƒํ•œ ์ด๋ฒคํŠธ ์†Œ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
๊ทธ๋ฆฌ๊ณ  Object ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ ์ด๊ฒƒ์„ ํ•„์š”ํ•œ ํƒ€์ž…์œผ๋กœ ํ˜•๋ณ€ํ™˜ํ•˜์—ฌ์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

public void actionPerformed(MouseEvent e){
	button = (JButton)e.getSource();	//ํ˜•๋ณ€ํ™˜
}

โญ๋ฆฌ์Šค๋„ˆ๋ฅผ ๋…๋ฆฝ์ ์ธ ํด๋ž˜์Šค๋กœ ์ž‘์„ฑ

  • ๋ฒ„ํŠผ์ด ๋ˆŒ๋ ค์ง€๋ฉด ๋ฒ„ํŠผ์˜ ํ…์ŠคํŠธ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑ
    -MyListener ํด๋ž˜์Šค ์ž‘์„ฑ
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class MyListener implements ActionListener {	
//๋ณ„๋„์˜ ํด๋ž˜์Šค๋กœ ์ •์˜ํ•œ๋‹ค.ActionListner ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.
    @Override
    public void actionPerformed(ActionEvent e) {
//      ...	Action ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ์—ฌ๊ธฐ์— ๋“ค์–ด๊ฐ„๋‹ค.
        JButton button = (JButton)e.getSource();
        button.setText("๋งˆ์นจ๋‚ด ๋ฒ„ํŠผ์ด ๋ˆŒ๋Ÿฌ์กŒ์Šต๋‹ˆ๋‹ค.");
    }
}
  • ActionEventTest ํด๋ž˜์Šค ์ž‘์„ฑ
import javax.swing.*;
import java.awt.*;
class MyFrame  extends JFrame {  //ํ”„๋ ˆ์ž„ ์ƒ์†ํ•ด์„œ MyFrame ์„ ์–ธ
   private JButton button;
    public MyFrame() {  //์ƒ์„ฑ์ž ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ถ”๊ฐ€ํ•œ๋‹ค.
        this.setSize(300,200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("์ด๋ฒคํŠธ์˜ˆ์ œ");
        this.setLayout(new FlowLayout());
        button = new JButton("๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด์‹œ์˜ค");
        button.addActionListener(new MyListener()); //๋ฒ„ํŠผ์— ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ๋“ฑ๋ก
        this.add(button);
        this.setVisible(true);
        //์ด๋ฒคํŠธ๋ฆฌ์Šค๋„ˆ ๊ฐ์ฒด๋ฅผ new ๋ฅผ ์ด์šฉํ•˜์—ฌ์„œ ์ƒ์„ฑํ•˜๊ณ  ๋ฒ„ํŠผ์— ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ๊ฐ์ฒด๋ฅผ ๋“ฑ๋ก
        //...
    }
}
public class P395_ActionEventTest {
    public static void main(String[] args) {
        MyFrame t = new MyFrame();
    }
}

๐Ÿ“Œ ์•ก์…˜ ์ด๋ฒคํŠธ

  • ์‚ฌ์šฉ์ž๊ฐ€ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋Š” ๊ฒฝ์šฐ
  • ์‚ฌ์šฉ์ž๊ฐ€ ๋ฉ”๋‰ด ํ•ญ๋ชฉ์„ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ
  • ์‚ฌ์šฉ์ž๊ฐ€ ํ…Œ์ŠคํŠธ ํ•„๋“œ์—์„œ ์—”ํ„ฐํ‚ค๋ฅผ ๋ˆ„๋ฅด๋Š” ๊ฒฝ์šฐ

๐Ÿ“Œ ์–ด๋Žํ„ฐ ํด๋ž˜์Šค

  • ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฆฌ์Šค๋„ˆ ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ์ •์˜๋˜์–ด ์žˆ๋Š” ๋ชจ๋“  ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•ด์„œ ๋‚ด๊ฐ€ ์ž‘์„ฑํ•˜๊ณ  ์‹ถ์€ ๋ฉ”์†Œ๋“œ๊ฐ€ 1๊ฐœ ๋ฟ์ด๋ผ๋„ ๋ชจ๋“  ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ๋ถˆํŽธํ•จ์ด ์žˆ์—ˆ๋‹ค.
  • ์ด๋Ÿฌํ•œ ๋ถˆํŽธํ•จ์„ ํ•ด์†Œํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์ด ๊ฐ ๋ฆฌ์Šค๋„ˆ์— ๋Œ€์‘ํ•˜๋Š” ์–ด๋Œ‘ํ„ฐ ํด๋ž˜์Šค
  • ๋ฆฌ์Šค๋„ˆ๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ด๊ณ  ์–ด๋Œ‘ํ„ฐ๋Š” ํด๋ž˜์Šค์˜ ํ˜•ํƒœ๋กœ ์ œ๊ณต๋œ๋‹ค.
  • ๋ฆฌ์Šค๋„ˆ๋ฅผ ์‚ฌ์šฉํ• ๋•Œ๋Š” implement๋ฅผ, ์–ด๋Œ‘ํ„ฐ๋ฅผ ์‚ฌ์šฉํ• ๋•Œ๋Š” extends

โญ ์–ด๋Žํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

  • extends KeyAdaptor ๋ฅผ ์‚ฌ์šฉ

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ