JABA 버튼 이벤트(Button event) 처리


버튼변수이름.addActionListener(new 이벤트클래스());
이벤트 클래스에   ActionListener 인터페이스 구현해야된다.


1. 외부 클래스로 이용하는 방법
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import javax.swing.*;
import java.awt.*;
import java.awt.event.*// 이벤트 처리를 위한 패키지
public class J1215_ActionTest1 extends JFrame{
    
    // 버튼 변수 선언
    private JButton button1, button2;
    
    public J1215_ActionTest1(){
        
        setTitle("event 객체");
        setSize(300,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // 패널
        JPanel panel = new JPanel();
        add(panel);
        setVisible(true);
        
          // 버튼 생성
        button1 = new JButton("1번 버튼");
        button2 = new JButton("2번 버튼");
        
        // 버튼에 이벤트 리스너 등록
        /*     
        버튼을 눌렀을때 처리하는 것을 이벤트 리스너이다. 
        버튼을 활용하기 위해선 인터페이스를 구현해야됨.
        이벤트와 관련된 인터페이스를 구현해야됨    
        */        
        button1.addActionListener(new Mylisten());     // public void addActionListener(ActionListener l)  ActionListener l = Interface ActionListener
        button2.addActionListener(new Mylisten2());    // Adds an ActionListener to the button.
        
        // 패널에 버튼 등록
        panel.add(button1);
        panel.add(button2);
        
    }
    
    public static void main(String args[]){
        
        new J1215_ActionTest1();
    }
}
 // 이벤트 리스너 - 이벤트의 동작을 처리해주는 클래스
 // 이벤트가 발생했을때 이벤트 리스너 내부의 이벤트 처리 메소드가 호출됨.
 // ActionListener 인터페이스 구현
class Mylisten implements ActionListener{
    
        JButton jb;
    
    public void actionPerformed(ActionEvent e){ // 액션이벤트가 발생하면 호출
        
        // getSource() 메소드는 이벤트를 발생한 이벤트 소스를 반환한다.
        // object 타입으로 반환하므로 필요한 타입으로 형변환해서 사용하면된다.
        jb = (JButton)e.getSource(); 
        
        if(jb.getText().equals("눌렀음~~"))
        {
            jb.setText("1번 버튼");
        }
        else{
            jb.setText("눌렀음~~");
        }
        
        
    }
}
class Mylisten2 implements ActionListener{
    
    JButton jb;
    
    public void actionPerformed(ActionEvent e){
        
        jb = (JButton)e.getSource();
        
        if(jb.getText().equals("눌렀음~~"))
        {
            jb.setText("2번 버튼");
        }
        else{
            jb.setText("눌렀음~~");
        }
    }
    
}
cs


2. 내부 클래스를 사용하는 방법
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class J1218_ActionTest2 extends JFrame{
    
    JButton button1, button2;
    
    Label label;
    
    public J1218_ActionTest2(){
        
        setSize(200,200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("event 객체");
        
        setVisible(true);
        
        JPanel panel = new JPanel();
        add(panel);
        
        button1 = new JButton("1번 버튼");
        button2 = new JButton("2번 버튼");
        
        // 버튼에 이벤트 리스너 등록
        button1.addActionListener(new Mylisten());     // public void addActionListener(ActionListener l)  ActionListener l = Interface ActionListener
        button2.addActionListener(new Mylisten());    // Adds an ActionListener to the button.
        
        panel.add(button1);
        panel.add(button2);
        
        Label lb = new Label();
        lb.setText("Lable");
        lb.setSize(350100);
        lb.setAlignment(Label.CENTER);
        lb.setBackground(Color.yellow);
        lb.setForeground(Color.green);
        panel.add(lb);
        
        
    }
    
    class Mylisten implements ActionListener{
    
    public void actionPerformed(ActionEvent e){ 
        
        if(e.getSource() == button1)
        {
            button1.setBackground(Color.red);
            label.setText("1번 버튼");
        }
        if(e.getSource() == button2)
        {
            button2.setBackground(Color.green);
            label.setText("2번 버튼");
        }
        
        
    }
}
    public static void main(String args[]){
        
        new J1218_ActionTest2();
    }
}
cs


3. 클래스를 사용하지 않는 방법

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import javax.swing.*;
import java.awt.*;
import java.awt.event.*// 이벤트 처리를 위한 패키지
public class J1218_ActionTest3 extends JFrame implements ActionListener{
    
    JButton button1, button2;
    
    Label lb;
    
    public J1218_ActionTest3(){
        
        setSize(200,200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("event 객체");
        
        setVisible(true);
        
        JPanel panel = new JPanel();
        add(panel);
        
        button1 = new JButton("1번 버튼");
        button2 = new JButton("2번 버튼");
        
        // 버튼에 이벤트 리스너 등록
        button1.addActionListener(this);     // public void addActionListener(ActionListener l)  ActionListener l = Interface ActionListener
        button2.addActionListener(this);    // Adds an ActionListener to the button.
        
        panel.add(button1);
        panel.add(button2);
        
        lb = new Label();
        lb.setText("Lable");
        lb.setSize(350100);
        lb.setAlignment(Label.CENTER);
        lb.setBackground(Color.yellow);
        lb.setForeground(Color.green);
        panel.add(lb);
    }
    
    public void actionPerformed(ActionEvent e){ 
        
        if(e.getSource() == button1)
        {
            button1.setBackground(Color.red);
            lb.setText("1번 버튼");
        }
        if(e.getSource() == button2)
        {
            button2.setBackground(Color.green);
            lb.setText("2번 버튼");
        }
    }
    public static void main(String args[]){
        
        new J1218_ActionTest3();
    }
}
cs


댓글