note

Adapter Ex 본문

자바/AWT

Adapter Ex

투한 2011. 12. 29. 16:29
package com.adapter;

import java.awt.*;
import java.awt.event.*;

public class AdapterEx extends Frame implements ActionListener{

	Panel p1,p2,p3;

	TextField tf;
	TextArea ta;

	Button b1,b2;

	public AdapterEx(){
		
		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);

		//KeyEventHandlers 가 다른 클래스이기 때문에 tf,ta값을 보낸다(?)
		tf.addKeyListener(new KeyEventHandlers(tf,ta));

		addWindowListener(new WindowEventHandlers());

	}

	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 AdapterEx();
	}
}
//KeyEvent를 처리하는 클래스
class KeyEventHandlers extends KeyAdapter{
	TextField tf;
	TextArea ta;
	
	public KeyEventHandlers(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("");
		}
	}
}
//Window Event를 처리하는 클래스

class WindowEventHandlers extends WindowAdapter{
	public void windowClosing(WindowEvent e){
		System.exit(0);
	}
}




package com.adapter2;

import java.awt.*;
import java.awt.event.*;

public class AdapterEx extends Frame
implements ActionListener{
	Panel p1,p2,p3;

	TextField tf;
	TextArea ta;

	Button b1,b2;

	public AdapterEx(){
		super("Adapte 테스트");

		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);

		tf.addKeyListener(new KeyEventHandlers());

		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 AdapterEx();
	}
	class KeyEventHandlers extends KeyAdapter{

		public void keyTyped(KeyEvent e){
			if(e.getKeyChar()==KeyEvent.VK_ENTER){
				ta.append(tf.getText()+"\n");
				tf.setText("");
			}
		}
	}
}


package com.adapter3;

import java.awt.*;
import java.awt.event.*;

public class AdapterEx extends Frame
implements ActionListener{
	Panel p1,p2,p3;

	TextField tf;
	TextArea ta;

	Button b1,b2;

	public AdapterEx(){
		super("Adapte 테스트");

		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);

		//KeyEvent를 처리하는 익명내부클래스 형태의 이벤트 처리
		tf.addKeyListener(new KeyAdapter(){
			public void keyTyped(KeyEvent e){
				if(e.getKeyChar()==KeyEvent.VK_ENTER){
					ta.append(tf.getText()+"\n");
					tf.setText("");
				}
			}
		});

		//WindowEvent를 처리하는 익명내부클래스형태의 이벤트처리
		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 AdapterEx();
	}
}


'자바 > AWT' 카테고리의 다른 글

Graphic Color  (0) 2011.12.29
Graphic Font  (0) 2011.12.29
Window Event  (0) 2011.12.29
Mouse Motion  (0) 2011.12.29
Key Event  (0) 2011.12.29