/* DialogWindow.java */ import java.awt.*; import java.awt.event.*; public class DialogWindow extends Frame implements 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); /* WindowListenerÀÇ ¸¹Àº µµ±¸µéÀ» °£´ÜÇÏ°Ô ±¸ÇöÇϱâ À§ÇØ WindowAdapter Ŭ·¡½º¸¦ »ó¼ÓÇÏ¿´´Ù */ addWindowListener(new WindowAdapter() { 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(); setVisible(false); } }