IT/Java & JSP & FW

java :: GUI 프로그래밍-2

엑수시아 2011. 12. 27. 12:48
 Event
이벤트는 컴포넌트에 사용자가 어떤 사건을 발생시키는 것을 의미한다. 

아래 3 단어가 뜻하는 것이 무엇인지 이해하자.
이벤트소스 (Event Source)
이벤트 (Event)
이벤트처리 (Event Handler)

Event 처리 절차
1. java.awt.event.* 을 import 한다
2. XXXListener 추상클래스 또는 인터페이스를 상속받는다.
3. 상속받은 녀석으로부터 추상메소드를 오버라이딩한다
4. 이벤트 소스와 핸들러를 연결해준다.
    EX) .addXXXListener()메소드사용
5. 오버라이딩한 메소드에 처리코드를 구현한다.

예제
(FrameT.java)
import java.awt.*;
import java.awt.event.*;

// 이벤트 추가를 위해 implements ActionListener를 상속받는다.
public class FrameT extends Frame implements ActionListener {
	
	Button b1,b2,b3;
	
	public FrameT(){
		BorderLayout dl=new BorderLayout();
		
		Panel p1=new Panel();
		FlowLayout fl=new FlowLayout(FlowLayout.CENTER,10,10);
		p1.setLayout(fl);
		add(p1);
		
		//b1, b2, b3가 이벤트의 근원지이다.
		b1=new Button("Left");
		b1.setBackground(Color.red);
		b2=new Button("Center");
		b2.setBackground(Color.blue);
		b3=new Button("Right");
		b3.setBackground(Color.yellow);
		
		p1.add(b1);p1.add(b2);p1.add(b3);
		
		//나 자신이 이벤트를 가지고 있으므로 상대객체를 나 자신을 가르키는 this
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
	}

	@Override
	public Insets insets() {
		return new Insets(40,20,20,20);
	}
	
	//클릭시 이벤트를 위한 설정
	@Override
	public void actionPerformed(ActionEvent e) {
		//연결이 잘 되었는지 확인
		
		Object obj=e.getSource();  //주소값을 이용해서 같은지 아닌지 판단하는 방법
		String str=e.getActionCommand();  // Label의 값을 가져와서 비교하기 위함
//		System.out.println("str:"+str);
		if(obj==b1){
			this.setBackground(Color.red);
			FlowLayout f1=new FlowLayout(FlowLayout.LEFT,10,10);
			this.setLayout(f1);
		}else if(obj==b2){
			this.setBackground(Color.blue);
			FlowLayout f2=new FlowLayout(FlowLayout.CENTER,10,10);
			this.setLayout(f2);
		}else if(obj==b3){
			this.setBackground(Color.yellow);
			FlowLayout f3=new FlowLayout(FlowLayout.RIGHT,10,10);
			this.setLayout(f3);
		}
		this.validate();  // 새로운 정열을 위한 검증
	}
	
	public static void main(String[] args) {
		
		FrameT ft=new FrameT();
		ft.setSize(300,300);
		
		Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();
		int x=(int)dim.width/2-150;
		int y=(int)dim.height/2-150;
		
		ft.setLocation(x, y);
		ft.setVisible(true);
	}

}
예제
(CallTest.java)
import java.awt.*;
import java.awt.event.*;

public class CallTest extends Frame implements ActionListener{
	
	Button b1,b2;
	Label l1,l2,l3;
	TextField tf1,tf2,tf3;
	
	public CallTest(){
		GridLayout gd=new GridLayout(4,2);
		this.setLayout(gd);
		l1=new Label("first number");tf1=new TextField("");
		l2=new Label("second number");tf2=new TextField("");
		l3=new Label("Result");tf3=new TextField("0");
		tf3.setEditable(false);
		b1=new Button("call");b2=new Button("Reset");
		add(l1);add(tf1);
		add(l2);add(tf2);
		add(l3);add(tf3);
		add(b1);add(b2);
		
		b1.addActionListener(this);
		b2.addActionListener(this);
	}

	@Override
	public Insets insets() {
		return new Insets(40,20,20,20);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Object obj=e.getSource();
		if(obj==b1){
			int i=Integer.parseInt(tf1.getText())+Integer.parseInt(tf2.getText());
			//tf3.setText((new Integer(i).toString()));  //정석
			tf3.setText(""+i);  //꼼수로 int 형을 문자열을 붙여서 String으로 바꾸기
		}else if(obj==b2){
			String i="0";
			tf3.setText(i);
			String j=" ";
			tf1.setText(j);
			tf2.setText(j);
		}
		this.validate();
	}
	
	public static void main(String[] args){
		CallTest call=new CallTest();
		call.setSize(300, 300);
		
		Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();
		
		int x=(int)dim.width/2-150;
		int y=(int)dim.height/2-150;
		
		call.setLocation(x, y);
		call.setVisible(true);
	}

}

메뉴바 생성 예제
(MenuTest.java)
import java.awt.*;
import java.awt.event.*;

public class MenuTest extends Frame implements ActionListener{

	MenuBar mb;
	Menu m_file;
	MenuItem mi_load, mi_save, mi_close;
	
	public MenuTest(){
		mb=new MenuBar();  //윈도우는 이미 일정영역이 메뉴바로 할당이 되어 있다
		this.setMenuBar(mb);  //컴포넌트를 붙이듯이 add 대신에 set으로 부착해준다.
		
		m_file=new Menu("파일");
		mb.add(m_file);
		
		mi_load=new MenuItem("열기");
		mi_save=new MenuItem("저장");
		mi_close=new MenuItem("닫기");
		
		m_file.add(mi_load);
		m_file.add(mi_save);
		m_file.addSeparator();  //구분선 넣기
		m_file.add(mi_close);
		
		mi_load.addActionListener(this);
		mi_save.addActionListener(this);
		mi_close.addActionListener(this);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		Object obj=e.getSource();
		if(obj==mi_load){
			FileDialog fd=new FileDialog(this,"파일열기",FileDialog.LOAD);
			fd.setVisible(true);
			System.out.println(fd.getDirectory());
			System.out.println(fd.getFile());
		}else if(obj==mi_save){
			FileDialog fd=new FileDialog(this,"파일저장",FileDialog.SAVE);
			fd.setVisible(true);
		}else if(obj==mi_close){
			System.exit(0);
		}
		
	}
	
	public static void main(String[] args) {
		
		MenuTest mt=new MenuTest();
		mt.setSize(300,300);
		mt.setVisible(true);
	}
}