/* This applet demonstrates several GUI components that are available in Java 1.1. Note that the class is defined to implement the interfaces ActionListener and ItemListener so that it can handle events generated by the various components. Last modified May 14, 2002. David Eck */ import java.awt.*; // make GUI compnent classes available import java.awt.event.*; // make GUI event classes available import java.applet.Applet; // make the Applet class available public class GUIDemo extends Applet implements ItemListener, ActionListener{ private TextArea transcript; // a message will be posted to this text area // each time an event is generated by some // some user action public void init() { // set up the applet setBackground(new Color(180,180,255)); setLayout(new GridLayout(1,2,5,5)); // I will put the transcript area in the right half of the // applet. The left half will be occupied by a grid of // four lines. Each line contains a component and // a label for that component. transcript = new TextArea(); transcript.setEditable(false); transcript.setBackground(Color.white); Panel left = new Panel(); left.setLayout(new GridLayout(4,2,3,3)); add(left); add(transcript); Label lab = new Label("Push Button: ", Label.RIGHT); lab.setBackground(Color.white); left.add(lab); Button b = new Button("Click Me!"); b.setBackground(Color.white); b.addActionListener(this); left.add(b); lab = new Label("Checkbox: ", Label.RIGHT); lab.setBackground(Color.white); left.add(lab); Checkbox c = new Checkbox("Click me!"); c.setBackground(Color.white); c.addItemListener(this); left.add(c); lab = new Label("Text Field: ", Label.RIGHT); lab.setBackground(Color.white); left.add(lab); TextField t = new TextField("Type here!"); t.setBackground(Color.white); t.addActionListener(this); left.add(t); lab = new Label("Pop-up Menu: ", Label.RIGHT); lab.setBackground(Color.white); left.add(lab); Choice m = new Choice(); m.setBackground(Color.white); m.addItem("First Option"); m.addItem("Second Option"); m.addItem("Third Option"); m.addItem("Fourth Option"); m.addItemListener(this); left.add(m); } // end init() private void post(String message) { // add a line to the transcript transcript.append(message + "\n"); } public Insets getInsets() { // Allow 3 pixels space around the edges of the applet return new Insets(3,3,3,3); } public void actionPerformed(ActionEvent evt) { // respond to an action event; this method is part of the // ActionListener interface. Object target = evt.getSource(); // which component produced this event? if (target instanceof Button) post("Button was clicked."); else if (target instanceof TextField) post("Pressed return in TextField\n with contents:\n " + evt.getActionCommand()); } public void itemStateChanged(ItemEvent evt) { // respond to an item event; this method is part of the // ItemListener interface. Object target = evt.getSource(); if (target instanceof Checkbox) { if (evt.getStateChange() == ItemEvent.SELECTED) post("Checkbox was turned on."); else post("Checkbox was turned off."); } else { post("Item \"" + evt.getItem() + "\" selected from pop-up menu."); } } } // end class GUIDemo