import java.applet.Applet; import java.awt.*; import java.awt.event.*; // This appears in Core Web Programming from // Prentice Hall Publishers, and may be freely used // or adapted. 1997 Marty Hall, hall@apl.jhu.edu. /** A listener used to send identical text to * two text areas. Used in the Mirror class below. */ class MirrorListener extends KeyAdapter { private TextArea area1, area2; public MirrorListener(TextArea area1, TextArea area2) { this.area1 = area1; this.area2 = area2; } public void keyPressed(KeyEvent event) { String key = String.valueOf(event.getKeyChar()); area1.append(key); area2.append(key); event.consume(); } } /** An applet that creates two text areas and * attaches the same KeyListener to both. */ public class Mirror extends Applet { public void init() { setLayout(new GridLayout(1, 2)); TextArea area1 = new TextArea(); TextArea area2 = new TextArea(); MirrorListener mirror = new MirrorListener(area1, area2); area1.addKeyListener(mirror); area2.addKeyListener(mirror); add(area1); add(area2); } }