본문 시작
eventTest2
- 한진희
- 조회 : 4880
- 등록일 : 2009-10-16
import java.awt.*;
import java.awt.event.*;
class FrameEvent extends Frame {
Button redBtn, blueBtn;
public FrameEvent( ) {
setLayout(new FlowLayout());
redBtn = new Button("빨간 색");
blueBtn = new Button("파란 색");
add(redBtn); //버튼 객체를 프레임에 등록
add(blueBtn); //배치는 배치관리자가 한다.
ButtonListener handler = new ButtonListener(this);
redBtn.addActionListener(handler);
blueBtn.addActionListener(handler);
setSize(300,200);
setVisible(true);
addWindowListener(new WindowAdapter( ){
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}//windowClosing 메서드 끝
} //클래스 정의 끝
); //addWindowListener 메서드 끝
}//생성자 끝
}//프레임 클래스 끝
//2. 처리할 이벤트에 대한 이벤트 리스너 클래스를 설계
class ButtonListener implements ActionListener{
Frame frm=null;
public ButtonListener(){ }
public ButtonListener(Frame value){ frm=value; }
public void actionPerformed(ActionEvent e){
if(e.getActionCommand( )=="빨간 색")
frm.setBackground(Color.red);
else //파란 버튼이 눌리면 프레임의 배경 색을 파란색으로
frm.setBackground(Color.blue);
}
};
public class EventTest02{
public static void main(String[] args) {
new FrameEvent( );
}
}
import java.awt.event.*;
class FrameEvent extends Frame {
Button redBtn, blueBtn;
public FrameEvent( ) {
setLayout(new FlowLayout());
redBtn = new Button("빨간 색");
blueBtn = new Button("파란 색");
add(redBtn); //버튼 객체를 프레임에 등록
add(blueBtn); //배치는 배치관리자가 한다.
ButtonListener handler = new ButtonListener(this);
redBtn.addActionListener(handler);
blueBtn.addActionListener(handler);
setSize(300,200);
setVisible(true);
addWindowListener(new WindowAdapter( ){
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}//windowClosing 메서드 끝
} //클래스 정의 끝
); //addWindowListener 메서드 끝
}//생성자 끝
}//프레임 클래스 끝
//2. 처리할 이벤트에 대한 이벤트 리스너 클래스를 설계
class ButtonListener implements ActionListener{
Frame frm=null;
public ButtonListener(){ }
public ButtonListener(Frame value){ frm=value; }
public void actionPerformed(ActionEvent e){
if(e.getActionCommand( )=="빨간 색")
frm.setBackground(Color.red);
else //파란 버튼이 눌리면 프레임의 배경 색을 파란색으로
frm.setBackground(Color.blue);
}
};
public class EventTest02{
public static void main(String[] args) {
new FrameEvent( );
}
}
- 이전전자장 시험 자료
- 다음 Java Event