import java.awt.*; import java.applet.Applet; import java.awt.event.*; public class TrivialApplet extends Applet { public void init() { setBackground (Color.white); add(inText); add(nameButton); add(inInt); add(ageButton); inText.addActionListener(nameButton); inInt.addActionListener(ageButton); setVisible(true); } TextField inText = new TextField(25), inInt = new TextField("0"); String nameS, ageS; ResponseButton nameButton = new ResponseButton("Name", inText, this), ageButton = new ResponseButton("Age", inInt, this); public void paint (Graphics g) { g.drawString ("Name and age",1,80); if(nameButton.replyAvailable()) { String nameS = nameButton.getReply(); g.drawString ("Your name is " + nameS,10,110); } if(ageButton.replyAvailable()) { String ageS = ageButton.getReply(); int age = Integer.parseInt (ageS); g.drawString ("Your age is " + age,10,140); } } } public class ResponseButton extends Button implements ActionListener { public ResponseButton(String name, TextField controls, Applet who) { super(name); addActionListener(this); this.controls = controls; this.who = who; } public void actionPerformed(ActionEvent e) { reply = controls.getText(); who.repaint(); } public String getReply(){return reply;} public boolean replyAvailable(){return !reply.equals("");} private TextField controls; private String reply = ""; private Applet who; }