Computer_IT/JAVA

[JAVA5] ActionEvent Example

고급코드 2006. 9. 14. 14:10
반응형
TextField 의 내용을 Console로 출력한다.

파일명 : ActionEvent1.java
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. public class ActionEvent1 extends Frame implements ActionListener {
  4.   TextField tf;
  5.   public ActionEvent1(String title) {
  6.     super(title);
  7.     tf=new TextField(20);
  8.    
  9.     // this는 Frame이면서, ActionListener 객체이다.
  10.     tf.addActionListener(this);
  11.    
  12.     //  텍스트 필드를 프레임(this)에 추가한다.
  13.     add(tf);   
  14.   }
  15.   public void actionPerformed(ActionEvent e) {
  16.     // 이벤트 소스가 TextField형 객체이면
  17.     if(e.getSource() instanceof TextField) {
  18.      
  19.       // 이벤트 소스를 가져온다.
  20.       TextField temp = (TextField)e.getSource();
  21.       // 화면에 temp의 문자열 출력
  22.       System.out.println(temp.getText());
  23.      
  24.       // temp의 내용을 지운다.  
  25.       temp.setText("");
  26.     }
  27.   }
  28.   public static void main(String args[]) {
  29.     ActionEvent1 me = new ActionEvent1("액션 이벤트 처리");
  30.     me.pack();
  31.     me.setVisible(true);
  32.   }
  33. }
 
반응형