ListenerTest.java

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
package Day_06;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class ListenerTest extends Frame implements ActionListenerWindowListener {
    Panel p;
    Button input, exit;
    TextArea ta;
    
    public ListenerTest()
    {
        super("ActionEvent Test");
        p = new Panel();
 
        input = new Button("입력");
        exit = new Button("종료");
        
        ta = new TextArea();
 
        this.addWindowListener(this);
        input.addActionListener(this);
        exit.addActionListener(this);
        
        p.add(input);
        p.add(exit);
 
        add(p, BorderLayout.NORTH);
        add(ta, BorderLayout.CENTER);
        
        setBounds(300300300200);
        setVisible(true);
    }
 
    @Override
    public void actionPerformed(ActionEvent ae) {
        String name;
        name = ae.getActionCommand();
        
        if(name.equals("입력"))
        {
            ta.append("버튼이 입력되었습니다.\n");
        }
        else
        {
            ta.append("프로그램을 종료합니다.\n");
            try {
                Thread.sleep(1000);    
            } catch(Exception e) {
                e.printStackTrace();
            }
            System.exit(0);
        }
    }
    
    
    public static void main(String[] args) {
        ListenerTest lt = new ListenerTest();
    }
 
    @Override
    public void windowOpened(WindowEvent e) { }
 
    @Override
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
 
    @Override
    public void windowClosed(WindowEvent e) { }
 
    @Override
    public void windowIconified(WindowEvent e) { }
 
    @Override
    public void windowDeiconified(WindowEvent e) { }
 
    @Override
    public void windowActivated(WindowEvent e) { }
 
    @Override
    public void windowDeactivated(WindowEvent e) { }
 
}
 
cs



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
package Day_06;
/*
// header - edit "Data/yourJavaHeader" to customize
// contents - edit "EventHandlers/Java file/onCreate" to customize
//
 */
 
import java.awt.*;
import java.awt.event.*;
 
public class AnnoymousEx2 extends Frame implements ActionListener
{
 
    Panel p1, p2, p3;
 
    TextField tf;
    TextArea ta;
 
    Button b1, b2;
 
    public AnnoymousEx2()
    {
 
        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);
 
        // KeyEventHandler -> KeyAdapter (익명 클래스로 바꿈)
        tf.addKeyListener(new KeyAdapter()  
        {
            public void keyTyped(KeyEvent e)
            {
                if(e.getKeyChar() == KeyEvent.VK_ENTER)
                {
                    ta.append(tf.getText()+"\n");
                    tf.setText("");
                }
            } 
        });
        // WindowHandler -> WindowAdapter (익명 클래스로 바꿈)
        addWindowListener(new WindowAdapter() 
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            } 
        });
    }
 
    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);
        }
    }
 
    // 어나니머스(익명) 클래스로 바꾸기
    /*class WindowHandler extends WindowAdapter
    {
        public void windowClosing(WindowEvent e)
        {
            System.exit(0);
        } 
    }*/
    
    // 멤버이너 클래스로 바꾸기
    /*class KeyEventHandler extends KeyAdapter
    {
        TextField tf;
        TextArea ta;
        public KeyEventHandler(TextField tf, TextArea ta)
        {
            this.tf=tf;
            this.ta=ta;
        }
        public void keyTyped(KeyEvent e)
        {
            if(e.getKeyChar() == KeyEvent.VK_ENTER)
            {
                ta.append(tf.getText()+"\n");
                tf.setText("");
            }
        } 
    }*/
    
    public static void main(String[] args)
    {
        new AnnoymousEx2();
    }
    
    /*
    // 어나니머스(익명) 클래스로 바꾸기
    class WindowHandler extends WindowAdapter
    {
        public void windowClosing(WindowEvent e)
        {
            System.exit(0);
        } 
    }
    // 멤버이너 클래스로 바꾸기
    class KeyEventHandler extends KeyAdapter
    {
        TextField tf;
        TextArea ta;
        public KeyEventHandler(TextField tf, TextArea ta)
        {
            this.tf=tf;
            this.ta=ta;
        }
        public void keyTyped(KeyEvent e){
            if(e.getKeyChar() == KeyEvent.VK_ENTER)
            {
                ta.append(tf.getText()+"\n");
                tf.setText("");
            }
        } 
    }
    */
}
 
cs




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
import java.awt.*;
import java.awt.event.*;
 
public class AnnoymousEx3 extends Frame implements ActionListener {
 
   Panel p1, p2, p3;
 
   TextField tf;
 
   TextArea ta;
 
   Button b1, b2;
   
 
 
   public AnnoymousEx3() {
 
      super("Adapter 테스트");
 
      p1 = new Panel();
      p2 = new Panel();
      p3 = new Panel();
 
      tf = new TextField(35);
      ta = new TextArea(1035);
 
      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(300200300300);
      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);
         }
      });
   }
 
   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 AnnoymousEx3();
   }
}
 
cs


'JAVA' 카테고리의 다른 글

[JAVA] Day_04. Inner class  (0) 2018.03.26
제네릭 정리2  (0) 2018.03.22
제네릭 정리1  (0) 2018.03.22
제네릭  (0) 2018.03.22
내부 클래스 2  (0) 2018.03.22
Posted by 너래쟁이
: