ÀÌ ¹®¼´Â hwp Çü½ÄÀÇ ¹®¼¸¦ ÇÑ±Û 96À¸·Î HTML º¯È¯ÇÑ °ÍÀÔ´Ï´Ù. Copyright (c) 1997, Yoon Kyung Koo(yoonforh@yahoo.com), All rights reserved.
AWT ÄÚµå ¾÷±×·¹À̵å
1.0 ¹öÀüÀÇ Äڵ带 1.1 ¹öÀüÀ¸·Î º¯È¯Çϱâ
awt ÆÐÅ°Áö¿¡´Â ¸¹Àº º¯È°¡ ÀÖ´Ù. ÀÚ¹Ù 1.0 ¹öÀüÀ¸·Î ÀÛ¼ºµÈ Äڵ带 1.1 ¹öÀüÀÇ ÄÄÆÄÀÏ·¯·Î ÄÄÆÄÀÏÇÏ´õ¶óµµ ½ÇÇàÀ» ÇÒ ¼ö´Â ÀÖ´Ù. ȣȯ¼ºÀ» À§ÇØ ÀÌÀü ¹öÀü¿¡¸¸ ÀÖ´Â µµ±¸³ª Ŭ·¡½ºµéÀ» ±×´ë·Î Áö¿øÇÏÁö¸¸ API ¹®¼¸¦ º¸¸é deprecated¶ó´Â ű׸¦ ÀÌ¿ëÇÏ¿© °¡±ÞÀû »ç¿ëÀ» ÇÇÇϵµ·Ï À¯µµÇÏ°í ÀÖ´Ù. ƯÈ÷ java.awt ÆÐÅ°Áö´Â ±âº»ÀûÀÎ À̺¥Æ® ¸ðµ¨ÀÇ º¯È¸¦ ºñ·ÔÇÏ¿© ¼ö¸¹Àº º¯È°¡ ÀÖ¾ú°í »õ·Î¿î ¸ðµ¨À» µû¸¦ ¶§¿¡¸¸ ¼Óµµ °³¼±°ú ¾ÈÁ¤¼º, ¶Ç ´Ù±¹¾î Áö¿ø µîÀ» º¸Àå¹ÞÀ» ¼ö ÀÖ´Ù.
¸ÕÀú °¢ µµ±¸ À̸§ÀÌ »ç¼ÒÇÏ°Ô º¯°æµÇ¾ú´Ù. ÀÌ°ÍÀº Á»´õ ÅëÀÏÀûÀÌ°í Á¤È®ÇÑ °³³äÀ» Ç¥ÇöÇϱâ À§ÇÑ °ÍÀ¸·Î ¿¹¸¦ µéÀÚ¸é java.awt.Component Ŭ·¡½ºÀÇ move() µµ±¸°¡ setLocation()·ÎÀÇ º¯°æÀÌ ÀÌ·± °æ¿ìÀÌ´Ù. ÀÌ·¯ÇÑ À̸§ º¯°æÀº ´Ü¼øÇÏ°Ô À̸§À» ¸ðµÎ º¯°æÇÔÀ¸·Î½á °íÄ¥ ¼ö ÀÖ´Ù. ÇÏÁö¸¸ ÀÌ À̸§ º¯°æÀº awt µµ±¸¿¡¼¸¸ À¯È¿ÇÔÀ» ¸í½ÉÇÏÀÚ. ½ÉÁö¾î ÀϺΠawt µµ±¸´Â º¯°æµÇÁö ¾ÊÀº °æ¿ìµµ ÀÖ´Ù. °á±¹ API ¹®¼¿Í ºñ±³Çغ¸´Â °ÍÀÌ °¡Àå È®½ÇÇÑ ¹æ¹ýÀÌ´Ù.
<±×¸²> awt µµ±¸ À̸§µé º¯È¯ Ç¥
1.0¿¡¼ÀÇ µµ±¸ À̸§ |
1.1¿¡¼ ¹Ù²ï µµ±¸ À̸§ |
addItem |
add |
appendText |
append |
inside |
contains |
layout |
doLayout |
getPageIncrement |
getBlockIncrement |
bounds getBoundingBox |
getBounds |
getClipRect |
getClipBound |
locate |
getComponentAt |
countComponents |
getComponentCount |
insets |
getInsets |
countItems |
getItemCount |
location |
getLocation |
countMenus |
getMenuCount |
minimumSize |
getMinimumSize |
preferredSize |
getPreferredSize |
getCurrent |
getSelectedCheckbox |
size |
getSize ¡Ø sed ½ºÅ©¸³Æ®¿¡¼ ¼öÇàÇÏÁö ¾Ê½À´Ï´Ù. |
getLineIncrement |
getUnitIncrement |
getVisible |
getVisibleAmount |
insertText |
insert |
isSelected |
isIndexSelected |
allowsMultipleSelections |
isMultipleMode |
delItem |
remove |
clear |
removeAll |
replaceText |
replaceRange |
setPageIncrement |
setBlockIncrement |
reshape |
setBounds |
setEchoCharacter |
setEchoChar |
disable |
setEnabled(false) |
enable |
setEnabled(true) |
enable( ½Ä ) |
setEnabled( ½Ä ) |
move |
setLocation |
setMultipleSelections |
setMultipleMode |
setCurrent |
setSelectedCheckbox |
resize |
setSize |
setLineIncrement |
setUnitIncrement |
hide |
setVisible(false) |
show() |
setVisible(true) |
show( ½Ä ) |
setVisible( ½Ä ) |
nextFocus |
transferFocus |
À§ÀÓÇü À̺¥Æ® ¸ðµ¨(Delegation Event Model)
µµ±¸ À̸§ÀÇ º¯È ¿Ü¿¡µµ ScrollPane(½ºÅ©·Ñ Á¶ÀýÀ» ½±°Ô ÇÒ ¼ö ÀÖ´Â À©µµ¿ì)À̳ª PopupMenu(¸Þ´º ¹Ù°¡ ¾ø´Â ¸Þ´º)¿Í °°Àº »õ·Î¿î GUI ÄÄÆÛ³ÍÆ®°¡ Ãß°¡µÇ¾ú´Ù. ÇÏÁö¸¸, AWT¿¡¼ °¡Àå Å« º¯È´Â ¿ª½Ã À̺¥Æ® ¸ðµ¨ÀÇ º¯ÈÀÌ´Ù. ±âÁ¸ÀÇ À̺¥Æ® ¸ðµ¨Àº Ŭ·¡½º »ó¼Ó¿¡ ±â¹ÝÇÑ À̺¥Æ® ¸ðµ¨·Î¼ ºÎ¸ðÀÇ À̺¥Æ® Çڵ鷯¸¦ »ó¼Ó¹Þ´Â °ÍÀÌ ÁÖµÈ °ñ°ÝÀ̾ú´Ù. ÇÏÁö¸¸ 1.1 ¹öÀüÀÇ À̺¥Æ® ¸ðµ¨Àº À§ÀÓÇü À̺¥Æ® ¸ðµ¨(delegation event model)ÀÌ´Ù. ¼·Î ´Ù¸¥ À̺¥Æ® ¸ðµ¨À» °°Àº ÇÁ·Î±×·¥ ³»¿¡ ¼¯¾î¼ ±¸ÇöÇÑ´Ù¸é ¿¹±âÄ¡ ¸øÇÑ °á°ú¸¦ ¾ß±âÇÒ ¼ö ÀÖ´Ù.
1.1ÀÇ À§ÀÓÇü À̺¥Æ® ¸ðµ¨¿¡ ±â¹ÝÇÑ ÇÁ·Î±×·¥ ¼Ò½º¸¦ ±âÁ¸ 1.0 ¹öÀüÀÇ handleEvent() µµ±¸³ª action() µµ±¸¿¡ Àͼ÷ÇØÁ® ÀÖ´ø µ¶ÀÚµéÀÌ º¸¸é ¾î¸®µÕÀýÇÒ °ÍÀÌ´Ù.
1.0 ¹öÀüÀº ¸ðµç awt À̺¥Æ®¸¦ handleEvent() µµ±¸¿¡¼ ó¸®Çϵµ·Ï ¼³°èµÇ¾ú´Ù. ±×¸®°í °¢ ÄÄÆÛ³ÍÆ®º°·Î Ưº°ÇÑ Àǹ̸¦ °¡Áø À̺¥Æ®´Â action() µµ±¸¿¡¼µµ ó¸®ÇÒ ¼ö ÀÖ¾ú´Ù. °¢°¢ÀÇ À̺¥Æ®´Â ÁÖ¾îÁö´Â À̺¥Æ® °´Ã¼ÀÇ id °ª¿¡ ÀÇÇØ ½Äº°µÇ¾ú´Ù. ÇÏÁö¸¸ 1.1 ¹öÀü¿¡¼´Â ´õ ÀÌ»ó µ¿ÀÏÇÑ Event Ŭ·¡½º·Î ÁÖ¾îÁöÁö ¾Ê´Â´Ù. 1.1 ¹öÀüÀÇ À̺¥Æ® À¯ÇüµéÀº java.util.EventObject¸¦ ·çÆ® µð·ºÅ丮·Î Çϴ Ŭ·¡½º »ó¼Ó ±¸Á¶¿¡ ĸ½¶ÈµÇ¾î ÀÖ´Ù. 1.1 ¹öÀüÀÇ À§ÀÓÇü À̺¥Æ® ¸ðµ¨¿¡¼´Â ÇϳªÀÇ À̺¥Æ®´Â À̺¥Æ®¸¦ ¹ß»ý½ÃÅ°´Â ¿øõ(source) °´Ã¼·ÎºÎÅÍ À̺¥Æ®¸¦ ±â´Ù¸®´Â ó¸®(listener) °´Ã¼¿¡·Î Àü´ÞµÈ´Ù. Áï, ¿øõ °´Ã¼¿¡¼ À̺¥Æ®°¡ ¹ß»ýµÇ¸é ó¸® °´Ã¼ÀÇ ÁöÁ¤µÈ µµ±¸¸¦ È£ÃâÇÑ´Ù. À̶§, ¹ß»ýµÈ À̺¥Æ® À¯ÇüÀ» Á¤ÀÇÇÏ´Â À̺¥Æ® ÇÏÀ§ Ŭ·¡½ºÀÇ ÀνºÅϽº°¡ ÀÎÀÚ·Î ³Ñ°ÜÁø´Ù.
ó¸® °´Ã¼´Â ÀϹÝÀûÀÎ java.util.EventListener·ÎºÎÅÍ »ó¼ÓµÈ ƯÁ¤ÇÑ EventListener¸¦ ±¸ÇöÇÏ´Â ¾î¶² °´Ã¼ÀÌ´Ù. EventListener ÀÎÅÍÆäÀ̽º´Â ó¸®ÇÒ Æ¯Á¤ÇÑ À̺¥Æ® À¯Çü¿¡ ´ëÀÀÇÏ¿© À̺¥Æ® ¿øõ °´Ã¼¿¡ ÀÇÇØ È£ÃâµÉ ¸î °³ÀÇ µµ±¸µéÀ» Á¤ÀÇÇÑ´Ù.
¿øõ °´Ã¼´Â À̺¥Æ®¸¦ ÀÏÀ¸Å°°Å³ª ¸¸µé¾îº¸³»´Â °´Ã¼ÀÌ´Ù. ¿øõ °´Ã¼´Â °¢ À̺¥Æ®µé¿¡ ÇØ´çÇϴ ƯÁ¤ÇÑ Ã³¸® °´Ã¼¸¦ µî·ÏÇÏ´Â µ¥ »ç¿ëµÇ´Â set<À̺¥Æ® À¯Çü>Listener³ª add<À̺¥Æ® À¯Çü>Listener µµ±¸¸¦ Á¦°øÇÏ¿© ³»º¸³¾ À̺¥Æ® ÁýÇÕÀ» Á¤ÀÇÇÑ´Ù.
awt ÇÁ·Î±×·¥¿¡¼ ÀüÇüÀûÀ¸·Î ¿øõ °´Ã¼´Â º¸Åë GUI ±¸¼º¿ä¼ÒÀÌ°í ó¸® °´Ã¼´Â ÇÁ·Î±×·¥ÀÌ À̺¥Æ®ÀÇ È帧À̳ª 󸮸¦ Á¦¾îÇϱâ À§ÇØ ÀûÀýÇÑ Listener¸¦ ±¸ÇöÇÏ´Â °´Ã¼ÀÌ´Ù.
À̺¥Æ®´Â 1.0 ¹öÀü¿¡¼´Â java.awt.Event¿Í °°ÀÌ ÇϳªÀÇ Event Ŭ·¡½º¿¡ ÀÇÇØ Ç¥ÇöµÇ¾ú°í ±¸Ã¼ÀûÀÎ À̺¥Æ® À¯ÇüÀº id °ª¿¡ ÀÇÇØ ±¸º°µÇ¾úÁö¸¸, 1.1 ¹öÀü¿¡¼´Â À̺¥Æ® À¯ÇüÀ̳ª °ü°èÀÖ´Â À̺¥Æ® À¯Çü ±×·ìÀ» ³ªÅ¸³»´Â À̺¥Æ® Ŭ·¡½ºµéÀÇ °èÃþ ±¸Á¶·Î ÀÌ·ç¾îÁø´Ù. µû¶ó¼ ÇÁ·Î±×·¥µéÀº java.util.EventObject³ª ´Ù¸¥ AWT À̺¥Æ® Ŭ·¡½º ÁßÀÇ Çϳª·ÎºÎÅÍ ÆÄ»ýµÈ ÀڽŸ¸ÀÇ À̺¥Æ® À¯ÇüÀ» ÀÚÀ¯·Ó°Ô Á¤ÀÇÇÒ ¼öµµ ÀÖ´Ù.
<±×¸² 17-6> À̺¥Æ® º¯È¯ Ç¥
1.0 ¹öÀü |
1.1 ¹öÀü |
||
À̺¥Æ®/µµ±¸ |
À̺¥Æ® ¹ß»ý ÄÄÆÛ³ÍÆ® |
ÀÎÅÍÆäÀ̽º |
ÀÎÅÍÆäÀ̽ºÀÇ µµ±¸µé |
ACTION_EVENT/action |
Button, List, MenuItem, TextField |
ActionListener |
actionPerformed(ActionEvent) |
Checkbox, CheckboxMenuItem, Choice |
ItemListener |
itemStateChanged(ItemEvent) |
|
WINDOW_DESTROY WINDOW_EXPOSE WINDOW_ICONIFY WINDOW_DEICONIFY |
Dialog, Frame |
WindowListener |
windowClosing(WindowEvent) windowOpened(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) windowClosed(WindowEvent) (no 1.0 equivalent) windowActivated(WindowEvent) (no 1.0 equivalent) windowDeactivated(WindowEvent) (no 1.0 equivalent) |
WINDOW_MOVED |
Dialog, Frame |
ComponentListener |
componentMoved(ComponentEvent) componentHidden(ComponentEvent) (no 1.0 equivalent) componentResized(ComponentEvent) (no 1.0 equivalent) componentShown(ComponentEvent) (no 1.0 equivalent) |
SCROLL_LINE_UP SCROLL_LINE_DOWN SCROLL_PAGE_UP SCROLL_PAGE_DOWN SCROLL_ABSOLUTE SCROLL_BEGIN SCROLL_END |
Scrollbar |
AdjustmentListener (ȤÀº »õ·Î Ãß°¡µÈ ScrollPane Ŭ·¡½º¸¦ »ç¿ëÇÒ ¼öµµ ÀÖ½À´Ï´Ù.) |
adjustmentValueChanged(AdjustmentEvent) |
LIST_SELECT LIST_DESELECT |
Checkbox, CheckBoxMenuItem, Choice, List |
ItemListener |
itemStateChanged(ItemEvent) |
MOUSE_DRAG/mouseDrag MOUSE_MOVE/mouseMove |
Canvas, Dialog, Frame, Panel, Window |
MouseMotionListener |
mouseDragged(MouseEvent) mouseMoved(MouseEvent) |
MOUSE_DOWN/mouseDown MOUSE_UP/mouseUp MOUSE_ENTER/mouseEnter MOUSE_EXIT/mouseExit |
Canvas, Dialog, Frame, Panel, Window |
MouseListener |
mousePressed(MouseEvent) mouseReleased(MouseEvent) mouseEntered(MouseEvent) mouseExited(MouseEvent) mouseClicked(MouseEvent) (no 1.0 equivalent) |
KEY_PRESS/keyDown KEY_RELEASE/keyUp KEY_ACTION/keyDown KEY_ACTION_RELEASE/keyUp |
Component |
KeyListener |
keyPressed(KeyEvent) keyReleased(KeyEvent) keyTyped(KeyEvent) (no 1.0 equivalent) |
GOT_FOCUS/gotFocus LOST_FOCUS/lostFocus |
Component |
FocusListener |
focusGained(FocusEvent) focusLost(FocusEvent) |
1.0¿¡´Â ¾ø´Â À̺¥Æ®µéÀÔ´Ï´Ù. |
ContainerListener |
componentAdded(ContainerEvent) componentRemoved(ContainerEvent) |
|
1.0¿¡´Â ¾ø´Â À̺¥Æ®µéÀÔ´Ï´Ù. |
TextListener |
textValueChanged(TextEvent) |
ÄÚµå ¿¹Á¦
½ÇÁ¦·Î 1.0 ¹öÀüÀ¸·Î ÀÛ¼ºµÈ awt ¿¹Á¦ ¼Ò½º¸¦ 1.1 ¹öÀüÀ¸·Î °»½ÅÇغ¸ÀÚ.
ÀÌ ÇÁ·Î±×·¥Àº ¸ÕÀú ÇÁ·¹ÀÓ À©µµ¿ì¸¦ »ý¼ºÇÑ ÈÄ ¹öÆ°À» ´©¸£¸é ´ëÈ»óÀÚ À©µµ¿ì¸¦ ³ªÅ¸³»´Â ÀÚ¹Ù ÀÀ¿ë ÇÁ·Î±×·¥ÀÌ´Ù. ´ëÈ»óÀÚ À©µµ¿ìÀÇ ÅؽºÆ® Çʵ忡 ±ÛÀ» ÀÔ·ÂÇÑ ÈÄ set ¹öÆ°À» ´©¸£¸é ±Û ³»¿ëÀÌ ÇÁ·¹ÀÓ À©µµ¿ìÀÇ ÅؽºÆ® ¿µ¿ª¿¡ ³ªÅ¸³´Ù.
¸ÕÀú sed ½ºÅ©¸³Æ®¸¦ »ç¿ëÇÏ¿© ¹Ù²ï µµ±¸ À̸§µéÀ» 1.1 ¹öÀü¿¡ ¸Â°Ô °íÄ£´Ù. ÀÌ Äڵ忡¼´Â resize() µµ±¸¿Í TextArea Ŭ·¡½ºÀÇ appendText() µµ±¸°¡ º¸ÀδÙ. °¢°¢ setSize()¿Í append()·Î º¯°æÇÑ´Ù. sed ½ºÅ©¸³Æ®ÀÎ updateAWT¸¦ »ç¿ëÇÏ´Â ¹æ¹ýÀº ´ÙÀ½°ú °°´Ù.
À¯´Ð½º ȯ°æÀ̶ó¸é
updateAWT DialogWindow.java > temp.java
À©µµ¿ì 95/NT ȯ°æÀ̶ó¸é
sed.exe -f updateAWT DialogWindow.java > temp.java
¿©±â¿¡¼ »ý¼ºµÈ temp.java°¡ µµ±¸ À̸§À» º¯°æÇÑ »õ·Î¿î DialogWindow.javaÀÌ´Ù. µû¶ó¼ temp.java¸¦ ´Ù¸¥ µð·ºÅ丮·Î º¹»çÇÑ ´ÙÀ½, À̸§À» ´Ù½Ã DialogWindow.java·Î º¹¿øÇÏ¸é µÈ´Ù.
ÇÏÁö¸¸ ¿ª½Ã º¯°æÀÌ ¸¹ÀÌ µÉ ºÎºÐÀº À̺¥Æ®¸¦ ó¸®ÇÏ´Â ·çƾÀÌ´Ù.
handleEvent() µµ±¸¿Í action() µµ±¸°¡ ±âÁ¸ÀÇ »ó¼ÓÇü À̺¥Æ® ¸ðµ¨¿¡¼ À̺¥Æ®¸¦ ó¸®ÇÏ´Â ´ëÇ¥ÀûÀÎ Àå¼ÒÀÌ´Ù. ÀÌ ºÎºÐÀ» »õ·Î¿î À̺¥Æ® ¸ðµ¨·Î ¿ÏÀüÈ÷ ¹Ù²Ù¾îÁÖ¾î¾ß ÇÑ´Ù.
/* DialogWindow.java */
import java.awt.*;
public class DialogWindow extends Frame {
private boolean inAnApplet = true;
private SimpleDialog dialog;
private TextArea textArea;
public DialogWindow() {
textArea = new TextArea(5, 40);
textArea.setEditable(false);
add("Center", textArea);
Button button = new Button("Click to bring up dialog");
Panel panel = new Panel();
panel.add(button);
add("South", panel);
}
public boolean handleEvent(Event event) { // »ó¼ÓÇü ¸ðµ¨ÀÇ À̺¥Æ® ó¸® ·çƾÀÌ´Ù.
if (event.id == Event.WINDOW_DESTROY) {
if (inAnApplet) {
dispose();
} else {
System.exit(0);
}
}
return super.handleEvent(event);
}
public boolean action(Event event, Object arg) { // »ó¼ÓÇü ¸ðµ¨ÀÇ À̺¥Æ® ó¸® ·çƾÀÌ´Ù.
if (dialog == null) {
dialog = new SimpleDialog(this, "A Simple Dialog");
}
dialog.show();
return true;
}
public void setText(String text) {
textArea.appendText(text + "\n"); // µµ±¸ À̸§ÀÌ 1.1 ¹öÀü¿¡¼ append()·Î º¯°æµÈ´Ù.
}
public static void main(String args[]) {
DialogWindow window = new DialogWindow();
window.inAnApplet = false;
window.setTitle("DialogWindow Application");
window.pack();
window.show();
}
}
class SimpleDialog extends Dialog {
TextField field;
DialogWindow parent;
Button setButton;
SimpleDialog(Frame dw, String title) {
super(dw, title, false);
resize(500, 200); // µµ±¸ À̸§ÀÌ 1.1 ¹öÀü¿¡¼ setSize·Î º¯°æµÈ´Ù.
parent = (DialogWindow)dw;
// ´ëÈ »óÀÚÀÇ °¡¿îµ¥ ºÎºÐÀ» ¸¸µç´Ù.
Panel p1 = new Panel();
Label label = new Label("Enter random text here:");
p1.add(label);
field = new TextField(40);
p1.add(field);
add("Center", p1);
// ´ëÈ »óÀÚÀÇ ¾Æ·§ÂÊ ºÎºÐÀ» ¸¸µç´Ù.
Panel p2 = new Panel();
p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
Button b = new Button("Cancel");
setButton = new Button("Set");
p2.add(b);
p2.add(setButton);
add("South", p2);
// ´ëÈ »óÀÚÀÇ °¢ ±¸¼º ¿ä¼Ò¸¦ ¿ì¼± Å©±â·Î º¯°æÇÏ¿© Á¤·ÄÇÑ´Ù.
pack();
}
public boolean action(Event event, Object arg) { // ¹öÆ° À̺¥Æ®¸¦ ó¸®ÇÑ´Ù.
// »ó¼ÓÇü ¸ðµ¨ÀÇ À̺¥Æ® ó¸® ·çƾÀÌ´Ù.
if ( (event.target == setButton)
| (event.target == field)) {
parent.setText(field.getText());
}
field.selectAll();
hide();
return true;
}
}
¹«¾ùº¸´Ù ¾Õ¼ ¸ÕÀú À̺¥Æ® Ŭ·¡½º°¡ ¼¼ºÐÇÏ¿© ºÐ·ùµÇ±â ¶§¹®¿¡ »õ·Ó°Ô ºÐ·ùµÈ À̺¥Æ® Ŭ·¡½ºµéÀ» Æ÷ÇÔÇÏ°í ÀÖ´Â java.awt.event ÆÐÅ°Áö¸¦ µé¿©¿À´Â °ÍÀÌ ¼ø¼ÀÌ´Ù. µû¶ó¼ ¸Ç ¾Õ¿¡ ´ÙÀ½ ¹®ÀåÀ» Ãß°¡ÇÑ´Ù.
import java.awt.event.*;
¶Ç, »õ·Î¿î À§ÀÓÇü À̺¥Æ® ¸ðµ¨¿¡¼´Â ¾Õ¿¡¼ ¼³¸íÇÑ ¹Ù¿Í °°ÀÌ À̺¥Æ®¸¦ ¹ß»ý½ÃÅ°´Â ¿øõ °´Ã¼¿Í À̺¥Æ®¸¦ ±â´Ù¸®°í Àִ ó¸® °´Ã¼°¡ Á¸ÀçÇϹǷΠ¿øõ °´Ã¼¿¡ À̺¥Æ®¸¦ ó¸®ÇÒ Ã³¸® °´Ã¼¸¦ µî·ÏÇÏ°í ¶Ç °¢ À̺¥Æ® ó¸® ·çƾÀ» ó¸® °´Ã¼ Ŭ·¡½º¿¡ ±¸ÇöÇÏ¿©¾ß ÇÑ´Ù.
ó¸® °´Ã¼·Î ÀÚ°ÝÀÌ ÀÖÀ¸·Á¸é ÇØ´ç À̺¥Æ®¸¦ ±â´Ù¸®°í ÀÖ¾î¾ß ÇÑ´Ù. <À̺¥Æ® Á¾·ù>Listener¶ó´Â ÀÎÅÍÆäÀ̽º¸¦ ±¸ÇöÇÔÀ¸·Î½á ±× ÀÚ°ÝÀ» ¾òÀ» ¼ö ÀÖ´Ù. DialogWindow Ŭ·¡½º´Â ActionEvent¿Í WindowEvent µÎ °¡Áö À̺¥Æ®ÀÇ Ã³¸® °´Ã¼ ÀÚ°ÝÀ» ¾ò±â À§ÇØ ´ÙÀ½°ú °°ÀÌ ÀÎÅÍÆäÀ̽º¸¦ ±¸ÇöÇÑ´Ù.
public class DialogWindow extends Frame
implements WindowListener, ActionListener {
¿øõ °´Ã¼¿¡ À̺¥Æ®¸¦ µî·ÏÇÏ´Â °ÍÀº add<À̺¥Æ® Á¾·ù>Listener(<ó¸® °´Ã¼>) µµ±¸¿¡ ÀÇÇؼ ¼öÇàµÈ´Ù. DialogWindow Ŭ·¡½º´Â ³»ºÎÀÇ ¹öÆ°(¿øõ °´Ã¼)ÀÌ ¹ß»ý½ÃÅ°´Â Action À̺¥Æ®¸¦ ó¸®ÇÏ°íÀÚ ÇϹǷΠ»ý¼ºÀÚ¿¡ ´ÙÀ½ ¹®ÀåÀ» Ãß°¡ÇÑ´Ù.
button.addActionListener(this);
Ŭ·¡½º »ý¼ºÀÚ³ª ¾ÖÇø´ÀÇ init() µµ±¸´Â À̺¥Æ®°¡ ¹ß»ýÇϱâ Àü¿¡ ¼öÇàµÇ¹Ç·Î À̺¥Æ® ó¸® °´Ã¼¸¦ µî·ÏÇÏ´Â µ¥ °¡Àå ÀûÇÕÇÑ Àå¼ÒÀÌ´Ù.
¸¶Âù°¡Áö·Î DialogWindow Ŭ·¡½º´Â ÀÚ½ÅÀÇ À©µµ¿ì Á¾·á À̺¥Æ®¸¦ ó¸®Çϱâ À§ÇØ ÀÚ½ÅÀ» ó¸® °´Ã¼·Î ÁöÁ¤ÇÏ°í ¶Ç SimpleDialog Ŭ·¡½ºµµ ÅؽºÆ® ÇʵåÀÇ ³»¿ë º¯°æ À̺¥Æ®¿Í °¢ ¹öÆ° ´¸² À̺¥Æ®¸¦ ó¸®ÇÑ´Ù.
ÀÌÁ¦ ¸¶Áö¸·À¸·Î handleEvent¿Í action¿¡ ÇØ´çÇÏ´Â À̺¥Æ® ó¸® ·çƾÀ» ±¸ÇöÇÒ Â÷·ÊÀÌ´Ù. À§ÀÓÇü À̺¥Æ® ¸ðµ¨¿¡¼´Â °¢ ó¸® °´Ã¼°¡ ±¸ÇöÇÑ ÀÎÅÍÆäÀ̽º°¡ ¾î¶² À̺¥Æ® ó¸® ·çƾÀ» ±¸ÇöÇØ¾ß ÇÏ´ÂÁö ¸»ÇØÁØ´Ù. ¿¹¸¦ µé¾î, À©µµ¿ì Á¾·á À̺¥Æ®¸¦ ó¸®ÇÏ·Á¸é WindowEvent¶ó´Â À̺¥Æ® Ŭ·¡½º¸¦ ó¸®ÇØ¾ß Çϴµ¥, ÀÌ À̺¥Æ®¸¦ ±â´Ù¸®´Â ÀÚ°ÝÀ» ÁÖ´Â ÀÎÅÍÆäÀ̽º´Â WindowListenerÀÌ´Ù. WindowListener ÀÎÅÍÆäÀ̽º´Â ¾ÕÀÇ Ç¥¿¡¼¿Í °°ÀÌ ¸ðµÎ ÀÏ°ö °³ÀÇ µµ±¸¸¦ ±¸ÇöÇϵµ·Ï °Á¦ÇÑ´Ù. ½ÇÁ¦·Î À©µµ¿ì Á¾·á À̺¥Æ®¸¦ ó¸®ÇÏ´Â µµ±¸´Â windowClosing µµ±¸ Çϳª»ÓÀÌÁö¸¸ ³ª¸ÓÁö ¿©¼¸ °³ÀÇ µµ±¸µµ ¹Ýµå½Ã ±¸ÇöÇØ¾ß ÇÑ´Ù. DialowWindow Ŭ·¡½º¿¡¼´Â ¸ðµÎ µµ±¸ ¸öü°¡ ¾ø´Â ³Î µµ±¸·Î ±¸ÇöÇß´Ù.
±×¸®°í ±¸Ã¼ÀûÀÎ À̺¥Æ® ó¸® ³»¿ëÀº ³»¿ë¿¡ µû¶ó º¯°æÇÏ´Â ¹æ¹ý¹Û¿¡ ¾ø´Ù.
´ÙÀ½Àº º¯°æÀ» ¿Ï·áÇÑ ÄÚµå ³»¿ëÀÌ´Ù. º¼µåü·Î Ç¥½ÃµÈ ºÎºÐÀÌ º¯°æµÈ ºÎºÐÀÌ´Ù.
/* DialogWindow.java */
import java.awt.*;
import java.awt.event.*;
public class DialogWindow extends Frame
implements WindowListener,
ActionListener {
private boolean inAnApplet = true;
private SimpleDialog dialog;
private TextArea textArea;
public DialogWindow() {
textArea = new TextArea(5, 40);
textArea.setEditable(false);
add("Center", textArea);
Button button = new Button("Click to bring up dialog");
button.addActionListener(this);
Panel panel = new Panel();
panel.add(button);
add("South", panel);
addWindowListener(this);
}
public void windowClosed(WindowEvent event) {
}
public void windowDeiconified(WindowEvent event) {
}
public void windowIconified(WindowEvent event) {
}
public void windowActivated(WindowEvent event) {
}
public void windowDeactivated(WindowEvent event) {
}
public void windowOpened(WindowEvent event) {
}
public void windowClosing(WindowEvent event) {
if (inAnApplet) {
dispose();
} else {
System.exit(0);
}
}
public void actionPerformed(ActionEvent event) {
if (dialog == null) {
dialog = new SimpleDialog(this, "A Simple Dialog");
}
dialog.show();
}
public void setText(String text) {
textArea.append(text + "\n"); // µµ±¸ À̸§ÀÌ º¯°æµÇ¾ú´Ù.
}
public static void main(String args[]) {
DialogWindow window = new DialogWindow();
window.inAnApplet = false;
window.setTitle("DialogWindow Application");
window.pack();
window.show();
}
}
class SimpleDialog extends Dialog implements ActionListener {
TextField field;
DialogWindow parent;
Button setButton;
SimpleDialog(Frame dw, String title) {
super(dw, title, false);
setSize(500, 200); // µµ±¸ À̸§ÀÌ º¯°æµÇ¾ú´Ù.
parent = (DialogWindow)dw;
// ´ëÈ »óÀÚÀÇ °¡¿îµ¥ ºÎºÐÀ» ¸¸µç´Ù.
Panel p1 = new Panel();
Label label = new Label("Enter random text here:");
p1.add(label);
field = new TextField(40);
field.addActionListener(this);
p1.add(field);
add("Center", p1);
// ´ëÈ »óÀÚÀÇ ¾Æ·§ÂÊ ºÎºÐÀ» ¸¸µç´Ù.
Panel p2 = new Panel();
p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
Button b = new Button("Cancel");
b.addActionListener(this);
setButton = new Button("Set");
setButton.addActionListener(this);
p2.add(b);
p2.add(setButton);
add("South", p2);
// ´ëÈ »óÀÚÀÇ °¢ ±¸¼º ¿ä¼Ò¸¦ ¿ì¼± Å©±â·Î º¯°æÇÏ¿© Á¤·ÄÇÑ´Ù.
pack();
}
public void actionPerformed(ActionEvent event) { // ¹öÆ° À̺¥Æ®¸¦ ó¸®ÇÑ´Ù.
Object source = event.getSource(); // ActionEvent¸¦ ¹ß»ý½ÃŲ °´Ã¼¸¦ ã´Â´Ù.
if ( (source == setButton)
| (source == field)) {
parent.setText(field.getText());
}
field.selectAll();
hide();
}
}