/* * Gary Cornell and Cay S. Horstmann, Core Java (Book/CD-ROM) * Published By SunSoft Press/Prentice-Hall * Copyright (C) 1996 Sun Microsystems Inc. * All Rights Reserved. ISBN 0-13-565755-5 * * Permission to use, copy, modify, and distribute this * software and its documentation for NON-COMMERCIAL purposes * and without fee is hereby granted provided that this * copyright notice appears in all copies. * * THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR * WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS * AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */ /** * @version 1.00 07 Feb 1996 * @author Cay Horstmann */ import java.awt.*; public class DataExchangeTest extends Frame implements ResultProcessor { public DataExchangeTest() { setTitle("DataExchangeTest"); Panel p = new Panel(); p.setLayout(new FlowLayout(FlowLayout.LEFT)); p.add(new Button("Connect")); p.add(new Button("Close")); add("North", p); } public boolean action(Event evt, Object arg) { if (arg.equals("Connect")) { ConnectInfo in = new ConnectInfo("yourname", ""); ConnectDialog pd = new ConnectDialog(this, in); pd.show(); } else if(arg.equals("Close")) System.exit(0); else return super.action(evt, arg); return true; } public boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY && evt.target == this) System.exit(0); else return super.handleEvent(evt); return true; } public void processResult(Dialog source, Object result) { if (source instanceof ConnectDialog) { ConnectInfo info = (ConnectInfo)result; System.out.println(info.username + " " + info.password); } } public static void main(String args[]) { Frame f = new DataExchangeTest(); f.resize(300, 200); f.show(); } } interface ResultProcessor { public void processResult(Dialog source, Object obj); } class ConnectInfo { public String username; public String password; public ConnectInfo(String u, String p) { username = u; password = p; } } class ConnectDialog extends Dialog { public ConnectDialog(DataExchangeTest parent, ConnectInfo u) { super(parent, "Connect", true); Panel p1 = new Panel(); p1.setLayout(new GridLayout(2, 2)); p1.add(new Label("User name:")); p1.add(username = new TextField(u.username, 8)); p1.add(new Label("Password:")); p1.add(password = new TextField(u.password, 8)); add("Center", p1); Panel p2 = new Panel(); p2.add(new Button("Ok")); p2.add(new Button("Cancel")); add("South", p2); resize(240, 120); } public boolean action(Event evt, Object arg) { if(arg.equals("Ok")) { dispose(); ((ResultProcessor)getParent()).processResult(this, new ConnectInfo(username.getText(), password.getText())); } else if (arg.equals("Cancel")) dispose(); else return super.action(evt, arg); return true; } public boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY) dispose(); else return super.handleEvent(evt); return true; } private TextField username; private TextField password; }