[Java] 버튼이벤트

import java.awt.*; //Button,Frame,TextArea 클래스
import java.awt.event.*; //ActionListener 인터페이스
public class ButtonEvent extends Frame
implements ActionListener{
//1단계 => 객체변수 선언
Button btn;
TextArea txt;
//3단계 => 생성자 구현 =>
//초기화(객체생성, 이벤트 연결,컴퍼넌트배치)
public ButtonEvent(){
//객체생성
btn = new Button("버튼을 눌러주세요");
txt = new TextArea();
//이벤트연결
btn.addActionListener(this);
//컴포넌트배치
setLayout(new BorderLayout());
add(btn,BorderLayout.NORTH);
add(txt,BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e){
txt.setText(txt.getText() + "버튼이 눌렸습니다." + "\n");
}
public static void main(String[]args){
//2단계 => 클래스 객체생성 => 생성자 자동호출
ButtonEvent be = new ButtonEvent();
be.setSize(300, 300);
be.setLocation(300, 400);
be.setVisible(true);
}
}