[] STUDY ³»¿ë

ÀÛ¼ºÀÏ 2003-10-21
ÀÛ¼ºÀÚ park
Á¶È¸¼ö 919
Á¦ ¸ñ À̺¥Æ®ÀÇ Á¾·ù

¸ñ·ÏÀ¸·Î | ¼öÁ¤ | »èÁ¦ | Á¤º¸Ãß°¡[reply] | ½Å±ÔÀÔ·Â

À̺¥Æ®


À̺¥Æ®(Event)¶õ À©µµ¿ì ½Ã½ºÅÛ¿¡¼­ ÀϾ´Â ¸ðµç »ç°ÇµéÀ» ¶æÇÏ´Â ¿ë¾îÀÌ´Ù. ÀÚ¹Ù¿¡¼­´Â ÀÌ·¯ÇÑ À̺¥Æ®¸¦ ó¸®Çϱâ À§ÇØ java.awt.event ÆÐÅ°Áö¿¡ Á¤ÀÇÇÏ°í ÀÖ´Ù.

¶ÇÇÑ ÀÚ¹Ù¿¡¼­´Â ÀÌ·±ÇÑ À̺¥Æ®¸¦ °´Ã¼¸¦ ÅëÇØ Àü´ÞÇÏ°í ¹ÝÀÀÇÑ´Ù.

1. ÀÚ¹Ù À̺¥Æ®ÀÇ Á¾·ù

1) Àú¼öÁØ À̺¥Æ®(AWT ÄÄÆ÷³ÍÆ®¿¡¼­ ÀϾ´Â À̺¥Æ®)

À̺¥Æ® Ŭ·¡½º

³»¿ë

(1) WindowEvent

À©µµ¿ì°¡ È°¼ºÈ­, ¶Ç´Â ´ÝÈú ¶§ ¹ß»ý.

(2) ComponentEvent

ÄÄÆÛ³ÍÆ®ÀÇ Ãß°¡/»èÁ¦¿¡ ÀÇÇØ ÄÁÅ×À̳ÊÀÇ ³»¿ëÀÌ º¯°æµÇ¾úÀ»´ë ¹ß»ý.

(3) ContainerEvent

ÄÁÅ×À̳ʿ¡ ÄÄÆ÷³ÍÆ®°¡ Ãß°¡/»èÁ¦µÇ´Â °æ¿ì ¹ß»ý.

(4) HierarchyEvent

ÄÄÆ÷³ÍÆ®ÀÇ °èÃþ±¸Á¶°¡ ¹Ù²ð ¶§ ¹ß»ý.

(5) MouseEvent

¸¶¿ì½ºÀÇ ¿òÁ÷ÀÓ¿¡ ÀÇÇØ ¹ß»ý.

(6) MouseWheelEvent

¸¶¿ì½º ÈÙÀÇ ¿òÁ÷ÀÓ¿¡ ÀÇÇØ ¹ß»ý.

(7) KeyEvent

Ä¿º¸µåÀÇ ÀԷ¿¡ ÀÇÇØ ¹ß»ý.

(8) InputMethodEvnet

ÀÔ·ÂÇÒ ¼ö ÀÖ´Â ¸â¼Òµå¿¡¼­ À̺¥Æ®°¡ ¹ß»ýÇßÀ» ¶§.

(9) FocusEvent

ÄÄÆ÷³ÍÆ®¿¡ Æ÷°Å½º°¡ µÇ¾úÀ» ¶§.

(10) InputEvent

°ÅÀÇ ¸ðµç ÀԷ¿¡ ´ëÇÑ À̺¥Æ®°¡ ¹ß»ýÇßÀ»¶§.

(11) PaintEvent

ÄÄÆ÷³ÍÆ®°¡ ±×·ÁÁú ¶§ ¹ß»ý.


2) °í¼öÁØ À̺¥Æ®(»ç¿ëÀÚÀÇ ¹ÝÀÀ¿¡ ÀÇÇØ ÀϾ´Â À̺¥Æ®)

(1) ActionEvent

¹öÆ° Ŭ¸¯µîÀÇ µ¿ÀÛ

(2) AdjustmentEvent

½ºÅ©·Ñ¹ÙµîÀÇ À§Ä¡°¡ ¹Ù²î¾úÀ»¶§

(3) TextEvent

ÅؽºÆ® ³»¿ëÀÌ ¹Ù²î¾úÀ»¶§

(4) ItemEvent

Ç׸ñÀÇ »óÅ°¡ ¹Ù²î¾úÀ»¶§




2. Event¸¦ ó¸®Çϱâ À§ÇÑ ÀýÂ÷

3. ÄÄÆ÷³ÍÆ®¿Í À̺¥Æ®ÀÇ Á¾·ù

ÄÄÆ÷³ÍÆ®

¹ß»ý À̺¥Æ®

Adju

Acti

Cont

Focu

Key

Mous

Comp

Item

Wind

Text

1.Adjustable

¡Û

2. Applet

¡Û

¡Û

¡Û

¡Û

¡Û

3. Button

¡Û

¡Û

¡Û

¡Û

¡Û

4. Canvas

¡Û

¡Û

¡Û

¡Û

5. Checkbox

¡Û

¡Û

¡Û

¡Û

¡Û

6. CheckboxMenuItem

¡Û

¡Û

7. Choice

¡Û

¡Û

¡Û

¡Û

¡Û

8. Component

¡Û

¡Û

¡Û

¡Û

9. Container

¡Û

¡Û

¡Û

¡Û

¡Û

10. Dialog

¡Û

¡Û

¡Û

¡Û

¡Û

¡Û

11. FileDialog

¡Û

¡Û

¡Û

¡Û

¡Û

¡Û

12. Frame

¡Û

¡Û

¡Û

¡Û

¡Û

¡Û

13. Label

¡Û

¡Û

¡Û

¡Û

14. List

¡Û

¡Û

¡Û

¡Û

¡Û

¡Û

15. Menu

¡Û

16. MenuItem

¡Û

17. Panel

¡Û

¡Û

¡Û

¡Û

¡Û

18. PopupMenu

¡Û

19. Scrollbar

¡Û

¡Û

¡Û

¡Û

¡Û

20. ScrollPane

¡Û

¡Û

¡Û

¡Û

¡Û

ÄÄÆ÷³ÍÆ®

¹ß»ý À̺¥Æ®

Adju

Acti

Cont

Focu

Key

Mous

Comp

Item

Wind

Text

21. TextArea

¡Û

¡Û

¡Û

¡Û

¡Û

22. TextComponent

¡Û

¡Û

¡Û

¡Û

¡Û

23. TextField

¡Û

¡Û

¡Û

¡Û

¡Û

¡Û

24. Window

¡Û

¡Û

¡Û

¡Û

¡Û

¡Û

ºñ°í)

Adju : AdjustmentEvent

Acti : ActionEvnet

Cont : ContainerEvnet

Focu : FocusEvent

Key : KeyEvent

Mous : MouseEvent

Comp : ComponentEvent

item : ItemEvent

Wind : WindowEvent

Text : TextEvent

4. À̺¥Æ® Çڵ鷯ÀÇ Á¾·ù

1) Listener
2) Adapter : Listener Ŭ·¡½º¸¦ ºÎ¸ð Ŭ·¡½º·ÎÇÏ¿© ÆÄ»ýµÇ¾úÀ¸¸ç Listener¿¡¼­ ºÒÇÊ¿äÇÏ°Ô ±â¼úµÇ´ø ¸Þ¼ÒµåµéÀ» Ãß»óÀ¸·Î Á¤ÀÇÇÏ°í Àֱ⠶§¹®¿¡ Adapter Çڵ鷯 ¹æ½Ä¿¡¼­´Â ºÒÇÊ¿äÇÑ ¸Þ¼Òµå¸¦ ±â¼úÇÏÁö ¾Ê¾Æµµ µÇµµ·Ï ¸¸µé¾îÁ³´Ù.

5. À̺¥Æ®¿Í À̺¥Æ® Çڵ鷯

´ëºÎºÐÀÇ À̺¥Æ® Çڵ鷯´Â À̺¥Æ® À̸§ÀÇ "Event"¶õ ´Ü¾î ´ë½Å¿¡ "Listener" ¶Ç´Â "Adapter"¸¦ ºÙ¿© À̺¥Æ®¿¡ ´ëÇÑ À̺¥Æ® Çڵ鷯¸íÀ¸·Î »ç¿ëÇÑ´Ù. ¿¹¸¦ µé¾î MouseEvent¶ó¸é Çڵ鷯´Â MouseListener¿Í MouseAdapter°¡ ÀÖ´Ù.

Listener or Adapter

±¸ÇöÇØ¾ß ÇÒ ¸Þ¼Òµå

ActionListener

actionPerformed(ActionEvent e)

AdjustmentListener

adjustmentValueChange(AdjustmentEvent e)

ComponentListener
ComponentAdapter

componentHidden(ComponentEvent e)
componentShown(ComponentEvent e)
componentMoved(ComponentEvent e)
componentResized(ComponentEvent e)

ContainerListener
ContainerAdapter

componentAdded(ContainerEvent e)
componentRemoved(ContainerEvent e)

FocusListener
FocosAdapter

focusGained(FocusEvent e)
focusLost(FocusEvent e)

ItemListener

itemStateChanged(ItemEvent e)

KeyListener
KeyAdapter

keyPressed(KeyEvent e)
keyReleased(KeyEvent e)
keyTyped(KeyEvent e)

MouseListener
MouseAdapter

mouseClicked(MouseEvent e)
mouseEntered(MouseEvent e)
mouseExited(MouseEvent e)
mousePressed(MouseEvent e)
mouseReleased(MouseEvent e)

MouseMotionListener
MouseMotionAdapter

mouseDragged(MouseEvent e)
mouseMoved(MouseEvent e)

WindowListener
WindowAdapter

windowOpened(WindowEvent e)
windowClosing(WindowEvent e)
windowClosed(WindowEvent e)
windowActivated(WindowEvent e)
windowDeactivated(WindowEvent e)
windowIconfied(WindowEvent e)
windowDeiconfied(WindowEvent e)

6. À̺¥Æ®º° ¸Þ¼Òµå

1) ActionListener

(1) ÁÖ¿ä ¸Þ¼Òµå

void actionPerformed(ActionEvent e)

¹Ýµå½Ã ±¸ÇöÇØ¾ß Çϸç ÀÌ°÷¿¡¼­ À̺¥Æ®¿¡ ´ëÇÑ Ã³¸®¸¦ ÇÑ´Ù.

Object event_obj.getSource()

À̺¥Æ®°¡ ¹ß»ýÇÑ °´Ã¼¸¦ ¹ÝȯÇÑ´Ù.

String event_obj.getActionCommand()

À̺¥Æ®°¡ ¹ß»ýÇÑ °´Ã¼ÀÇ À̸§À» ¹ÝȯÇÑ´Ù.

(2) »ç¿ëÀýÂ÷

°¡) class¿¡ ÀÎÅÍÆäÀ̽º¸¦ Ãß°¡ÇÑ´Ù.

public class ActionListenerEx implements ActionListener

³ª) À̺¥Æ®¸¦ ¹ß»ý½Ãų °´Ã¼¿¡ ¸®½º³Ê¸¦ Ãß°¡ÇÑ´Ù.

Button b=new Button("Event Test Button");
b.addActionListener(this); // this´Â °´Ã¼°¡ Æ÷ÇÔµÈ ÃÖ »óÀ§ À©µµ¿ì °´Ã¼ÀÓ.

´Ù) ActionListener¿¡¼­ Çʼö·Î ±¸ÇöÇؾßÇÒ actionPerformed() ¸Þ¼Òµå¸¦ ±¸ÇöÇÑ´Ù.

public void actionPerformed(ActionEvent e){ ... }

(3) »ç¿ë¿¹

// ================================================================

// ActionListener Example

// make : pwg

// date : 2003

// note : ¹öÆ°À» Ŭ¸¯Çϰųª, ¸®½ºÆ®Ã¢¿¡¼­ ´õºíŬ¸¯Çϸé

// À̺¥Æ®°¡ ¹ß»ýÇÏ¿© ÅؽºÆ® â¿¡ ³»¿ëÀÌ Ç¥½ÃµÈ´Ù.

// ----------------------------------------------------------------

// ActionEvent¸¦ »ç¿ëÇÒ ¼ö ÀÖ´Â ÄÄÆ÷³ÍÆ®

// Button, List, TextField, MenuItem, CheckboxMenuItem

// ================================================================

import java.awt.*;

import java.awt.event.*; // Çʼö

public class ActionListenerEx extends Frame implements ActionListener

{

Button exitButton,cancelButton;

List list;

TextArea text;

Panel panelA, panelB;

public ActionListenerEx()

{

setTitle("ActionListener");

panelA = new Panel(new FlowLayout(FlowLayout.LEFT,0,0));

panelB = new Panel(new GridLayout(2,1));

// panelA

exitButton = new Button("exit");

cancelButton = new Button("cancel");

panelA.add(exitButton);

panelA.add(cancelButton);

// panelB

list = new List(10);

text = new TextArea(5,40);

// list item

list.add("È£¶ûÀÌ");

list.add("°í¾çÀÌ");

list.add("°­¾ÆÁö");

list.add("¸Á¾ÆÁö");

list.add("»õ¾ÓÁã");

//

panelB.add(list);

panelB.add(text);

// Event Ãß°¡

exitButton.addActionListener(this);

cancelButton.addActionListener(this);

list.addActionListener(this);

//

add(panelA,BorderLayout.NORTH);

add(panelB,BorderLayout.CENTER);

}

public static void main(String args[])

{

ActionListenerEx f=new ActionListenerEx();

f.setSize(500,300);

f.setVisible(true);

}

public void actionPerformed(ActionEvent e) // ActionListener ¿¡¼­´Â ¹Ýµå½Ã ±¸Çö

{

int i;

if(e.getSource() == exitButton)

{

dispose();

System.exit(0);

}

if(e.getSource() == cancelButton)

{

text.setText("cancel");

}

if(e.getSource() == list)

{

i = list.getSelectedIndex();

text.append("list -> " + e.getActionCommand());

text.append(" , Index ->" + i);

}

text.append("\n");

validate();

}

}

2) MouseListener / MouseMotionListener

(1) MouseListener ÁÖ¿ä ¸Þ¼Òµå

void mouseClicked(MouseEvent e)

¸¶¿ì½º°¡ Ŭ¸¯µÇ¾úÀ»¶§

void mouseEntered(MouseEvent e)

¸¶¿ì½º°¡ ÄÄÆ÷³ÍÆ® ³»·Î µé¾î¿ÔÀ»¶§

void mouseExited(MouseEvent e)

¸¶¿ì½º°¡ ÄÄÆ÷³ÍÆ® ¹ÛÀ¸·Î ³ª°¬À»¶§

void mousePressed(MouseEvent e)

¸¶¿ì½ºÀÇ ¹öÆ°ÀÌ ´­·¶À» ¶§.mouseClicked()º¸´Ù ¸ÕÀú ¹ß»ýµÊ.

void mouseReleased(MouseEvent e)

¸¶¿ì½º ¹öÆ°À» ³ùÀ»¶§

(2) MouseMotionListener ÁÖ¿ä ¸Þ¼Òµå

void mouseDragged(MouseEvent e)

¸¶¿ì½º¸¦ µå·¡±× ÇÒ¶§

void mouseMoved(MouseEvent e)

¸¶¿ì½º¸¦ À̵¿ÇßÀ»¶§

(2) »ç¿ëÀýÂ÷

°¡) class¿¡ MouseListener ¿Í MouseMotionListener ÀÎÅÍÆäÀ̽º¸¦ Ãß°¡ÇÑ´Ù.

public class MouseListenerEx implements MouseListener, MouseMotionListener

³ª) À̺¥Æ®¸¦ ¹ß»ý½Ãų °´Ã¼¿¡ ¸®½º³Ê¸¦ Ãß°¡ÇÑ´Ù.

Button b=new Button("Event Test Button");
b.addMouseListener(this); // this´Â °´Ã¼°¡ Æ÷ÇÔµÈ ÃÖ »óÀ§ À©µµ¿ì °´Ã¼ÀÓ.

´Ù) MouseListener¿Í MouseMotionListener¿¡¼­ Çʼö·Î ±¸ÇöÇؾßÇÒ ¸Þ¼Òµå¸¦ ±¸ÇöÇÑ´Ù.

public void mouseClicked(MouseEvent e){ ... }
public void mousePressed(MouseEvent e({ ... }
...

(3) »ç¿ë¿¹

// =====================================================

// MouseEvent Example

// make : pwg

// date : 2003

// note : ÇÁ·¹ÀÓÀ̳ª °´Ã¼¿¡¼­ ¸¶¿ì½º¸¦ Á¶ÀÛÇϸé À̺¥Æ®°¡

// ¹ß»ýÇÏ¿© ¸®½ºÆ®Ã¢¿¡ ³»¿ëÀÌ Ç¥½ÃµÈ´Ù.

// =====================================================

import java.awt.*;

import java.awt.event.*;

public class MouseEventEx extends Frame implements MouseListener,MouseMotionListener

{

Button exitButton, testButton,moveButton;

TextArea text;

Panel topPanel;

int bx=0, by=0; // ¸¶¿ì½º ÁÂÇ¥.

public MouseEventEx()

{

topPanel = new Panel(null);

topPanel.addMouseMotionListener(this);

topPanel.setBounds(0,30,100,25);

text = new TextArea();

exitButton = new Button("exit");

testButton = new Button("test");

moveButton = new Button("move");

exitButton.setBounds(0,0,30,20);

testButton.setBounds(30,0,30,20);

moveButton.setBounds(60,0,30,20);

// add

topPanel.add(exitButton);

topPanel.add(testButton);

topPanel.add(moveButton);

// Listener

exitButton.addMouseListener(this);

testButton.addMouseListener(this);

moveButton.addMouseListener(this);

topPanel.addMouseMotionListener(this);

add(topPanel,BorderLayout.NORTH);

add(text,BorderLayout.CENTER);

}

// MouseListener Method

public void mouseClicked(MouseEvent e)

{

text.append("mouseClicked...");

text.append("x=" + e.getX());

text.append(", y=" + e.getY());

text.append(", Source=" + e.getSource());

text.append(", Key =" + e.getModifiers());

text.append("\n");

validate();

bx=e.getX();

by=e.getY();

if(e.getSource()==exitButton) System.exit(0);

}

public void mousePressed(MouseEvent e)

{

text.append("mousePressed...\n");

validate();

}

public void mouseEntered(MouseEvent e)

{

text.append("mouseEntered...\n");

validate();

}

public void mouseExited(MouseEvent e)

{

text.append("mouseExited...\n");

validate();

}

public void mouseReleased(MouseEvent e)

{

text.append("mouseReleased..." + e.getSource() + "\n");

validate();

}

public void mouseDragged(MouseEvent e)

{

text.append("mouse Dragged ...\n");

validate();

}

public void mouseMoved(MouseEvent e)

{

bx=e.getX();

by=e.getY();

text.append("mouse Moved ..." + bx + " , " + by + "\n");

validate();

}

public static void main(String[] args)

{

MouseEventEx f=new MouseEventEx();

f.setSize(600,300);

f.setVisible(true);

}

}

3) AdjustmentListener

(1) Çʼö ¸Þ¼Òµå

void adjustmentValueChange(AdjustmentEvent e)

AdjustmentEvent°¡ ¹ß»ýµÇ¸é callµÇ´Â Çʼö ¸Þ¼Òµå

(2) ÁÖ¿ä ¸Þ¼Òµå

int e.getValue()

°´Ã¼ÀÇ ÇöÀç °ªÀ» ¸®ÅÏ

(3) »ç¿ëÀýÂ÷

°¡) class¿¡ ÀÎÅÍÆäÀ̽º¸¦ Ãß°¡ÇÑ´Ù.

public class AdjustmentListenerEx implements AdjustmentListener

³ª) À̺¥Æ®¸¦ ¹ß»ý½Ãų °´Ã¼¿¡ ¸®½º³Ê¸¦ Ãß°¡ÇÑ´Ù.

Scrollbar rScrollbar = new Scrollbar();
sScrollbar.addAdjustmentListener(this);

´Ù) Çʼö·Î ±¸ÇöÇؾßÇÏ´Â ¸Þ¼Òµå¸¦ ±¸ÇöÇÏ°í ¸Þ¼Òµå¾È¿¡ À̺¥Æ®¿¡ ÇØ´çÇϴ ó¸® ³»¿ëÀ» ±â¼úÇÑ´Ù.

public adjustmentValueChange(AdjustmentEvent e) { ... }

(4) »ç¿ë¿¹

// ==============================================================

// AdjustmentListener Example

// make : pwg

// date : 2003

// --------------------------------------------------------------

// note : ½ºÅ©·Ñ¹Ù¸¦ µå·¡±×ÇÏ¿© ÆгÎÀÇ »ö»óÀ» ¹Ù²Û´Ù.

// ==============================================================

import java.awt.*;

import java.awt.event.*;

public class AdjustmentListenerEx extends Frame implements ActionListener, AdjustmentListener

{

Panel pCenter, pEast;

Scrollbar rScrollbar, gScrollbar, bScrollbar;

Button exitButton;

int r=100,g=100,b=100;

public AdjustmentListenerEx()

{

// panel

pCenter = new Panel();

pEast = new Panel(new GridLayout(0,3));

// component

rScrollbar = new Scrollbar(Scrollbar.VERTICAL, 100,50,0,305);

gScrollbar = new Scrollbar(Scrollbar.VERTICAL, 100,50,0,305);

bScrollbar = new Scrollbar(Scrollbar.VERTICAL, 100,50,0,305);

rScrollbar.setBackground(new Color(255,0,0));

gScrollbar.setBackground(new Color(0,255,0));

bScrollbar.setBackground(new Color(0,0,255));

exitButton = new Button("exit");

// Listener

exitButton.addActionListener(this);

rScrollbar.addAdjustmentListener(this);

gScrollbar.addAdjustmentListener(this);

bScrollbar.addAdjustmentListener(this);

pCenter.setBackground(new Color(r,g,b));

pEast.add(rScrollbar);

pEast.add(gScrollbar);

pEast.add(bScrollbar);

add(pCenter, BorderLayout.CENTER);

add(pEast,BorderLayout.EAST);

add(exitButton, BorderLayout.NORTH);

}

// Listener Method

public void actionPerformed(ActionEvent e)

{

dispose();

System.exit(0);

}

public void adjustmentValueChanged(AdjustmentEvent e)

{

if(e.getSource() == rScrollbar) r=e.getValue();

if(e.getSource() == gScrollbar) g=e.getValue();

if(e.getSource() == bScrollbar) b=e.getValue();

pCenter.setBackground(new Color(r,g,b));

validate();

}

public static void main(String[] args)

{

AdjustmentListenerEx f= new AdjustmentListenerEx();

f.setSize(300,300);

f.setVisible(true);

}

}

4) KeyListener

(1) Çʼö ¸Þ¼Òµå

void keyPressde(KeyEvent e)

Å°°¡ ´­·ÁÁö¸é ¹ß»ý.

void keyReleased(KeyEvent e)

´­·ÁÁø Å°°¡ ¶ç¾îÁö¸é ¹ß»ý.

void KeyTyped(KeyEvent e)

ŸÀÚµÇ¸é ¹ß»ý.

(2) ÁÖ¿ä ¸Þ¼Òµå

char getKeyChar()

ÀÔ·ÂµÈ ¹®ÀÚ

int getKeyCode()

ÀÔ·ÁµÈ ¹®ÀÚÀÇ »ó¼ö°ª.(»ó¼ö°ªÀº ³»Á¤µÇ¾î ÀÖÀ½)

(3) ÁÖ¿ä »ó¼ö°ª

VK_ENTER

VK_UP

VK_ALT

VK_DOWN

VK_CONTROL

VK_LEFT

VK_SHIFT

VK_RIGHT

(4) »ç¿ë¿¹

// ==============================================

// KeyListener Example

// make : pwg

// date : 2003

// ----------------------------------------------

// note : Å°°¡ ´­·ÁÁö¸é ÇØ´ç Å°ÀÇ ³»¿ëÀÌ

// ¸®½ºÆ®Ã¢¿¡ Ç¥½ÃµÈ´Ù.

// ==============================================

import java.awt.*;

import java.awt.event.*;

public class KeyListenerEx extends Frame implements ActionListener, KeyListener

{

Panel topPanel;

Button exitButton;

TextArea textArea;

TextField textField;

public KeyListenerEx()

{

Panel topPanel = new Panel();

exitButton = new Button("Exit");

textArea = new TextArea(10,30);

textField = new TextField(20);

// add_on Panel

topPanel.add(textField);

topPanel.add(exitButton);

add(textArea,BorderLayout.CENTER);

add(topPanel,BorderLayout.NORTH);

// add Listener

textField.addKeyListener(this);

exitButton.addActionListener(this);

}

public void actionPerformed(ActionEvent e)

{

System.exit(0);

}

public void keyTyped(KeyEvent e)

{

textArea.append("" + e.getKeyChar() + "\n");

}

public void keyPressed(KeyEvent e)

{

if(e.getKeyCode() == KeyEvent.VK_ENTER)

{

textArea.append(textField.getText() + "\n");

textField.setText("");

}

if(e.getKeyCode() == KeyEvent.VK_ALT)

{

textArea.append(" alt key in \n");

}

if(e.getKeyCode() == KeyEvent.VK_CONTROL)

{

textArea.append(" control key in \n");

}

}

public void keyReleased(KeyEvent e)

{

}

public static void main(String[] args)

{

KeyListenerEx f= new KeyListenerEx();

f.setSize(500,300);

f.setVisible(true);

}

}

5) WindowListener

(1) Çʼö ¸Þ¼Òµå

void windowOpend(WindowEvent e)

âÀÌ ¿­·ÈÀ»¶§

void windowClosing(WindowEvent e)

âÀÌ ´ÝÈ÷°í ÀÖÀ»¶§

void windowClosed(WindowEvent e)

âÀÌ ´ÝÇûÀ»¶§

void windowActivated(WidnowEvent e)

È°¼ºÈ­ µÇ¾úÀ»¶§

void windowDeactivated(WindowEvent e)

ºñÈ°¼ºÈ­ µÇ¾úÀ»¶§

void windowIconified(WidnowEvent e)

ÃÖ¼ÒÈ­ µÇ¾úÀ»¶§

void windowDeiconified(WindowEvent e)

âÀÌ ´Ù½Ã º¹±ÍµÇ¾úÀ»¶§

(2) »ç¿ë¿¹

// ============================================

// WindowListener Example

// make : pwg

// date : 2003

// --------------------------------------------

// note :

// ============================================

import java.awt.*;

import java.awt.event.*;

public class WindowListenerEx extends Frame implements WindowListener, ActionListener

{

Button exitButton;

public WindowListenerEx()

{

exitButton = new Button("exit");

add(exitButton);

exitButton.addActionListener(this);

addWindowListener(this);

}

public void actionPerformed(ActionEvent e)

{

System.exit(0);

}

// WindowListener Method

public void windowActivated(WindowEvent e)

{

System.out.println("Window Active");

}

public void windowClosed(WindowEvent e)

{

System.out.println("Window Closed");

}

public void windowClosing(WindowEvent e)

{

System.out.println("Window Closing");

dispose();

System.exit(0);

}

public void windowDeactivated(WindowEvent e)

{

System.out.println("Window Deactive");

}

public void windowIconified(WindowEvent e)

{

System.out.println("Window Icon");

}

public void windowDeiconified(WindowEvent e)

{

System.out.println("Window Deicon");

}

public void windowOpened(WindowEvent e)

{

System.out.println("Window Open");

}

public static void main(String[] args)

{

WindowListenerEx f= new WindowListenerEx();

f.setSize(300,300);

f.setVisible(true);

}

}

7. Adapter

À̺¥Æ®¸¦ ó¸®Çϱâ À§ÇØ À̺¥Æ® Çڵ鷯ÀÎ Listener¸¦ »ç¿äÇÏ¿´´Ù. ListenerÀÇ ´ÜÁ¡Àº ¸Þ¼Òµå¸¦ »ç¿ëÇÏ´ø »ç¿äÇÏÁö ¾Ê´ø ¸ðµç ¸Þ¼Òµå¸¦ ±â¼úÇØ¾ß µÈ´Ù´Â Á¡ÀÌ´Ù.

WindowListener¸¦ »ý°¢ÇØ º¸ÀÚ.

°¡Àå ¸¹ÀÌ »ç¿ëÇÏ´Â ¸Þ¼Òµå°¡ windowClosed()ÀÌ´Ù. âÀ» ´Ý±â À§ÇØ »ç¿ëÇÏ´Â ¸Þ¼ÒµåÀÌÁö¸¸ ÀÌ ¸Þ¼Òµå¸¦ »ç¿ëÇϱâ À§ÇØ WindowListener¸¦ »ç¿ëÇÏ¿´´Ù¸é »ç¿ëÇÏÁö ¾Ê´Â ´Ù¸¥ 6°¡ÁöÀÇ ¸Þ¼Òµå¸¦ ¸ðµÎ ±â¼úÇØ ÁÖ¾î¾ß ÇÑ´Ù.

ÀÌ·± ºÒÆíÇÑÁ¡À» °³¼±ÇÏ¿© ÀÚ¹Ù´Â Adapter¶ó´Â À̺¥Æ® Çڵ鷯µé ¸¸µé¾î µÎ¾ú´Ù.

1) »ç¿ë ÀýÂ÷

Listener Çڵ鷯¿¡¼­´Â »ç¿ëÇÏ·Á´Â Listener¸¦ implements·Î »ó¼Ó¹Þ¾Æ »ç¿ëÇÏ´Â °ÍÀÌ º¸ÆíÀûÀÎ ¹æ¹ýÀ̶ó¸é, Adapter´Â ÇØ´ç Adapter¸¦ extends·Î »ó¼Ó¹Þ¾Æ ³»ºÎ Ŭ·¡½º¸¦ ¸¸µé¾î »ç¿ëÇÏ´Â °ÍÀÌ º¸ÆíÀûÀÎ ¹æ¹ýÀÌ´Ù.

³»ºÎ Ŭ·¡½º·Î ¸¸µé¾î »ç¿ëÇÏ¸é ¿ÜºÎ Å¬·¡½º¸¦ ¸¸µé¾î »ç¿ëÇÒ ¶§ º¸´Ù ÁÁÀºÁ¡Àº °¢ ÄÄÆ÷³ÍÆ®³ª º¯¼öµéÀ» Á¢±ÙÇÏ¿© ó¸®Çϱ⠽±±â ¶§¹®ÀÌ´Ù.

(1) ³»ºÎ Ŭ·¡½º·Î ¸¸µé¾îÁø Ŭ·¡½º °´Ã¼¸¦ Àμö·Î ÇÏ´Â Listener¸¦ À̺¥Æ® ´ë»ó¿¡ ´õÇÑ´Ù.

Button exitButton=new Button("Exit");
exitButton.addMouseListener(new MyMouseAdapter()); // MyMouseAdater()´Â ³»ºÎ À̺¥Æ® Ŭ·¡½º

(2) ³»ºÎ Ŭ·¡½º¸¦ ÇØ´ç À̺¥Æ®¿¡ ´ëÇÑ Adapter¸¦ »ó¼Ó¹Þ¾Æ ÀÛ¼ºÇÑ´Ù.

class MyMouseAdapter extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
.....
}
}

2) »ç¿ë¿¹

// ===============================================
// WindowAdapter Example
// make : pwg
// date : 2003
// note :
// ===============================================
import java.awt.*;
import java.awt.event.*;

public class WindowAdapterEx extends Frame
{
Button b;
public WindowAdapterEx()
{
addWindowListener(new myWinAdapter());
b=new Button("exit");
b.addMouseListener(new myMouseAdapter());
add(b);
}

public static void main(String[] args)
{
WindowAdapterEx f= new WindowAdapterEx();
f.setSize(300,300);
f.setVisible(true);
}

// ³»ºÎ Ŭ·¡½º
class myWinAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
String str=b.getLabel();
System.out.println("window closed event...");
System.out.println(str + "¹öÆ°µµ Àִµ¥...");
System.exit(0);
}
}

class myMouseAdapter extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
System.out.println("windowClosing µµ Àִµ¥...");
System.exit(0);
}
};
}


À̺¥Æ®ÀÇ Á¾·ù °ú(¿Í) °ü·ÃµÈ Ãß°¡ Á¤º¸

  • MouseMotionListener Sample(¸¶¿ì½º ÇöÀç ÁÂÇ¥°ª Ãâ·Â) [2004-04-02] [park´Ô Àç°ø] [Á¶È¸:809]
  • À̺¥Æ®ÀÇ Á¾·ù [2003-10-21] [park´Ô Àç°ø] [Á¶È¸:919]

    ¸ñ·ÏÀ¸·Î | ¼öÁ¤ | »èÁ¦ | Á¤º¸Ãß°¡[reply] | ½Å±ÔÀÔ·Â