// You may study, use, modify, and distribute this example for any purpose. // This example is provided WITHOUT WARRANTY either expressed or implied. import java.awt.*; import java.applet.Applet; /** This simulates a simple interaction with Text in the center and an OK Button underneath. */ public class TextDemo extends Applet { /** A piece of text identifying the last click*/ protected TextArea response; /** An array of buttons */ protected Button stimuli[]; /** The place in the Applet where the Buttons are put. */ protected Panel buttons; /** Layout and display text and buttons. Set the background of text to white and put the label in the center, create a grey panel underneath. Create and add buttons to this panel */ public TextDemo() { this.setBackground(Color.black); this.setLayout(new BorderLayout(5,5)); response=new TextArea(); resetResponse(); response.setEditable( true ); add("Center", response); buttons=new Panel(); buttons.setBackground(Color.lightGray); buttons.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); stimuli=new Button[2];//until now the individual buttons //did not exist stimuli[0]=new Button("OK"); buttons.add("OK", stimuli[0]); stimuli[1]=new Button("No"); buttons.add("No", stimuli[1]); this.add("South", buttons); }//constructor protected void resetResponse(){ response.setForeground(Color.black); response.setBackground(Color.white); response.setText( "This is a test of adding a TextArea\n"); response.append("That you can Edit!!\n"); /* was in JDK 1.0 response.appendText("That you can Edit!!\n"); */ }//resetResponse /** Changes color to yellow, does something to content, and if OK displays result in Green.
Else displays an exception in pink.

Note. The something done to the text field is determined by the actual class of this object, not the default TextDemo.something(...). */ protected void responseDoSomething(){ response.setBackground(Color.yellow); response.setVisible( true); try{ response.setText( this.something(response.getText())); response.setBackground(Color.green); response.setVisible( true); }catch (Exception e){ response.setBackground(Color.pink); response.setVisible( true); response.setText(e.toString()); }//try }//resetResponse protected String something(String text) throws Exception { return text.replace('e','E'); }//something /** Reacts to OK by doing something special to text and to No by resetting text. @deprecated */ public boolean action(Event event, Object arg) { if (event.target instanceof Button) { if(event.target == stimuli[0]) responseDoSomething(); else resetResponse(); response.setVisible( true); return true; } else { response.setText("Clunk!");//This doesn't happen.....why? response.setBackground(Color.pink); response.setVisible( true); return false; } }//action /**Creates a frame in which the Applet can run, for debugging purposes. */ public static void main(String args[]) { TextDemo textTest = new TextDemo(); textTest.init(); textTest.start(); Frame f = new Frame("textTest"); f.setBackground(Color.white); f.add("Center", textTest); f.setSize(500, 500); f.setVisible( true); }//main }//TextDemo