본문 바로가기

Programming/[JAVA]

[JAVA] 익명 내부 클래스(AWT 이용)(Anonymouse Inner Class)

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
package pack02;
 
import java.awt.*;
import java.awt.event.*;
 
public class AnonymousEx extends Frame implements ActionListener{
 
    Panel p1, p2, p3;
 
    TextField tf;
    TextArea ta;
 
    Button b1, b2;
    
    public AnonymousEx(){
 
        super("Adapter 테스트");
 
        p1=new Panel();
        p2=new Panel();
        p3=new Panel();
 
        tf=new TextField(35);
        ta=new TextArea(10,35);
 
        b1=new Button("Clear");
        b2=new Button("Exit");
 
        p1.add(tf);
        p2.add(ta);
        p3.add(b1);
        p3.add(b2);
 
        add("North",p1);
        add("Center",p2);
        add("South",p3);
 
        setBounds(300,200,300,300);
        setVisible(true);
 
        b1.addActionListener(this);
        b2.addActionListener(this);
 
        tf.addKeyListener(new KeyAdapter() {
            public void keyTyped(KeyEvent e){
                if(e.getKeyChar() == KeyEvent.VK_ENTER){
                    ta.append(tf.getText()+"\n");
                    tf.setText("");
                }
            } 
        });
 
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e){
                System.exit(0);
            } 
        });
    }
 
    /*class WindowHandler extends WindowAdapter{
        public void windowClosing(WindowEvent e){
            System.exit(0);
        } 
    }*/
    /*class KeyEventHandler extends KeyAdapter{
        
        public void keyTyped(KeyEvent e){
            if(e.getKeyChar() == KeyEvent.VK_ENTER){
                ta.append(tf.getText()+"\n");
                tf.setText("");
            }
        } 
    }*/
 
    public void actionPerformed(ActionEvent e){
        String str=e.getActionCommand();
        if(str.equals("Clear")){
            ta.setText("");
            tf.setText("");
            tf.requestFocus(); 
        }
        else if(str.equals("Exit")){
            System.exit(0);
        }
    }
 
    public static void main(String[] args){
        new AnonymousEx();
    }
}
 
cs