본문 바로가기

프로그래밍/시스템

[java] GUI , ActionListener(이벤트)

반응형
 
예제 
 
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import java.awt.*;    // java GUI 패키지
import javax.swing.*// 컴포넌트 패키지
import java.awt.event.*// 이벤트 관련 패키지(ActionListener가 포함됨)
 
class Swt33 extends JFrame implements ActionListener 
{
JButton b;
JTextField tf;
JTextArea ta;
JRadioButton jb1, jb2, jb3;
JCheckBox  c1,c2;
JComboBox c;
JList jl;
 
Swt33(){
 super("예제");
 c= new JComboBox();
 c.addItem("추가");
 c.addItem("핑크");
 c.addItem("초록");
 c.addItem("회색");
 
 c.addActionListener(new A());
 
 
String[] data = {"빨강""파랑""노랑"};
   jl = new JList(data);
   jl.addMouseListener(new m1());
 
   
JPanel p3=new JPanel();
 p3.add(c);
 
 JPanel p4=new JPanel();
 p4.add(jl);
 
c1=new JCheckBox("가");
c2=new JCheckBox("나");
c1.addActionListener(new ck());
c2.addActionListener(new ck());
 
b=new JButton("clear");
 b.addActionListener(this);
 
 
tf=new JTextField(10);
tf.addKeyListener(new Key());
 
 
ta=new JTextArea(10,20);
 
 
jb1=new JRadioButton("AA",true);
jb2=new JRadioButton("BB");
jb3=new JRadioButton("CC"); 
ButtonGroup bt=new ButtonGroup();
bt.add(jb1);bt.add(jb2);bt.add(jb3);
jb1.addActionListener(new R());
jb2.addActionListener(new R());
jb3.addActionListener(new R());
 
 
JScrollPane jsp = new JScrollPane(ta);
JPanel p5=new JPanel();
p5.add(jsp);
 
 
JPanel p2=new JPanel();
//p2.setLayout(new FlowLayout());
p2.add(jb1);p2.add(jb2);p2.add(jb3); p2.add(c1); p2.add(c2);
 
JPanel p1=new JPanel();
//p1.setLayout(new FlowLayout());
p1.add(tf);p1.add(b);  
 
//this.setLayout(new BorderLayout());
this.add("North",p2);
this.add("Center",p5);
this.add("South",p1);
this.add("East", p4);
this.add("West", p3);
 
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  this.pack();
 // this.show();
  this.setVisible(true);
 }
 
 
 
 public static void main(String[] args)
 {
  new Swt33();
 }
 
 
 
class A implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            
            String s = (String)c.getSelectedItem();
    
            if (s.equals("핑크")) ta.setBackground(Color.pink);
            else if (s.equals("초록")) ta.setBackground(Color.green);
            else if (s.equals("회색")) ta.setBackground(Color.gray);
            
            
        }
}
 
class R implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        
        if (jb1.isSelected()) ta.append("AA choice \n");
        else if(jb2.isSelected()) ta.append("BB choice \n");
        else ta.append("CC choice \n");
        
    }
}
 
class ck implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        
                JCheckBox j = (JCheckBox)e.getSource();
                if(j.equals(c1)){
                    if(c1.isSelected()) ta.append("가선택\n");
                    else ta.append("가해지\n");
                }else{
                    if(c2.isSelected()) ta.append("나선택\n");
                    else ta.append("나해지");
                }
    }
}
    
/*
class m1 implements MouseListener {
    
    public void mouseClicked(MouseEvent e) {
        String s = (String)jl.getSelectedValue();
        if(s.equals("빨강")) ta.setForeground(new Color(255,0,0));
        else if (s.equals("파랑")) ta.setForeground(new Color(0,0,255));
        else if (s.equals("초록")) ta.setForeground(new Color(0,255,0));
        else ta.setForeground(Color.yellow);
            
    }
    
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }
    
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }
    
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }
    
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }
            
    
}
*/
 
class m1 extends MouseAdapter{
    @Override 
    public void mouseClicked(MouseEvent e) {
        String s = (String)jl.getSelectedValue();
        if(s.equals("빨강")) ta.setForeground(new Color(255,0,0));
        else if (s.equals("파랑")) ta.setForeground(new Color(0,0,255));
        else if (s.equals("초록")) ta.setForeground(new Color(0,255,0));
        else ta.setForeground(Color.yellow);
            
    }                
    
}//m1
 
 
class Key implements KeyListener{
 
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER)
        {
            String s = tf.getText();
            ta.append(s+"\n");
            tf.setText("");
        }
        
    }
    public void keyTyped(KeyEvent e) {}        
    
    public void keyReleased(KeyEvent e) {}
        
    
}
 
@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    ta.setForeground(new Color(0,0,0));
    jb1.setSelected(true); jb2.setSelected(false);
    jb3.setSelected(false); 
    
    c1.setSelected(false);c2.setSelected(false);
    jl.clearSelection(); tf.setText("");
    
    
    ta.setBackground(new Color(255,255,255));
    ta.setText("");
    c.setSelectedIndex(0);
    
}
}
 
cs

 

@Override : "오버라이드 했다"라는 의미를 가진다.
 
 
 

만든 클래스 

 

 1. class Swt33 extends JFrame implements ActionListener  (메인)

 2. class A implements ActionListener     (콤보박스 이벤트 처리)

 3. class R implements ActionListener     (라디오버튼 이벤트 처리)

 4. class ck implements ActionListener    (체크박스 이벤트 처리)

 5. class m1 extends MouseAdapter       (마우스 클릭 이벤트 처리)

 6. class Key implements KeyListener      (키 입력 이벤트 처리)

 

 

 

 

 

inner 클래스

 

1. 클래스 안에 클래스가 있는것   

 

위의 코드에 클래스들을 그림으로 표현하면

 

                                           

 

 

코드로 구현할 때

 

ex )  c.addActionListener(new A())

jb1.addActionListener(new R())

 

 

 

 

inner 클래스파일은 만들어질 때  메인클래스명 뒤에 $가 붙고 inner 클래스명이 붙어서 만들어진다. 

[ "Swt33$A" ]

 

반응형