import java.applet.*;
import java.awt.*;
import java.awt.event.*;


public class Widgets extends Applet
{
  // Declare components here to avoid access problems.

    Label saySomething = new Label ("Let's use components!");
    Button clickMe = new Button ("Click me!");
    Checkbox onOrOff = new Checkbox ("Single Checkbox");
    Choice suit = new Choice ();
    List language = new List (3); // 3 rows visible
    Scrollbar scrolly = new Scrollbar (Scrollbar.HORIZONTAL, 50, 10, 0, 110);
                                  // orientation, value, bubblesize, min, max
    TextField oneLine = new TextField ("This is a TextField.", 20);
    TextArea manyLines = new TextArea ("TextArea\nOne\nTwo\nThree", 5, 20,
				       TextArea.SCROLLBARS_NONE);
    CheckboxGroup direction = new CheckboxGroup ();
      Checkbox north = new Checkbox ("North", true,  direction);
      Checkbox south = new Checkbox ("South", false, direction);
      Checkbox east =  new Checkbox ("East",  false, direction);
      Checkbox west =  new Checkbox ("West",  false, direction);
    Button changeThings = new Button ("Change things");
    Button changeLayout = new Button ("Use BorderLayout");

  public void init ()
  {
    /* This code is broken into three sections: creating and/or
       modifying the widgets, laying out the widgets, and assigning
       actions to the widgets.  It might seem to make more sense to
       organize the code widget by widget, but that turns out to make
       layout much harder. */
    
    // Create and/or modify widgets.  Executable code (mostly "add")
    // can't be done in the declarations, so it has to go here.
    
    suit.add ("Clubs");
    suit.add ("Diamonds");
    suit.add ("Hearts");
    suit.add ("Spades");
    
    language.add ("English");
    language.add ("Chinese");
    language.add ("Japanese");
    language.add ("German");
    
    // Lay out widgets, using default FlowLayout manager.

    add (saySomething);
    add (clickMe);
    add (onOrOff);
    add (suit);
    add (language);
    add (scrolly);
    add (oneLine);
    add (manyLines);
    add (changeThings);
    add (north); add (south); add (east); add (west);
                           // Note:  NOT add (direction);
    add (changeLayout);
    
    // Assign actions to widgets.

    // Button
    clickMe.addActionListener (new ActionListener () {
      public void actionPerformed (ActionEvent event)
        {
 	  showStatus ("Button clicked.");
        }
    });

    // Checkbox
    onOrOff.addItemListener (new ItemListener () {
      public void itemStateChanged (ItemEvent event)
        {
          showStatus ("Checkbox status is now " +
            (event.getStateChange () == ItemEvent.SELECTED));
        }
    });

    // Choice (drop-down list)
    suit.addItemListener (new ItemListener () {
      public void itemStateChanged (ItemEvent event)
        {
          showStatus ("Choice: " + suit.getSelectedItem ());
        }
    });

    // List
    language.addItemListener (new ItemListener () {
      public void itemStateChanged (ItemEvent event)
        {
          showStatus ("List: " + language.getSelectedItem ());
        }
    });
    
    // Scrollbar
    scrolly.addAdjustmentListener (new AdjustmentListener () {
      public void adjustmentValueChanged (AdjustmentEvent event)
        {
          showStatus ("Scrollbar: " + scrolly.getValue ());
        }
    });

    // TextField
    oneLine.addActionListener (new ActionListener () {
      public void actionPerformed (ActionEvent event)
        {
          showStatus ("TextField (ActionListener): " + oneLine.getText ());
        }
    });
    oneLine.addTextListener (new TextListener () {
      public void textValueChanged (TextEvent event)
        {
          showStatus ("TextField (TextListener): " + oneLine.getText ());
        }
    });
    
    // TextArea
    manyLines.addTextListener (new TextListener () {
      public void textValueChanged (TextEvent event)
        {
          showStatus ("TextArea: " + manyLines.getText ());
        }
    });

    // Button -- to demonstrate changing values programmatically
    changeThings.addActionListener (new ActionListener () {
      public void actionPerformed (ActionEvent event)
        {
          saySomething.setText ("I've been changed!");
          oneLine.setText ("TextField");
          manyLines.setText ("one\ntwo\nthree");

          changeThings.setLabel ("More changes");

          onOrOff.setLabel ("Destroy world");
          onOrOff.setState (true);

          suit.select ("Spades");
          language.select (0);

          scrolly.setValue (100 - scrolly.getValue ());
          south.setState (true);
			  
          showStatus ("Changing a few things....");
        }
    });

    // Checkboxs in a CheckboxGroup
    MyGroupListener pickDirection = new MyGroupListener (); // see below
    north.addItemListener (pickDirection);
    south.addItemListener (pickDirection);
    east.addItemListener (pickDirection);
    west.addItemListener (pickDirection);
  
    // Button
    changeLayout.addActionListener (new ActionListener () {
      public void actionPerformed (ActionEvent event)
        {
          changeLayout ();
        }
    });
  }

  class MyGroupListener implements ItemListener
  {
    public void itemStateChanged (ItemEvent event) {
      Checkbox d = direction.getSelectedCheckbox ();
      showStatus ("CheckboxGroup: " + d.getLabel ());
    }
  }
  
  void changeLayout () {
  
    removeAll (); // because we want to rearrange our Components
    setLayout (new BorderLayout ());
    setBackground (new Color (200, 200, 255));
  	
    add (BorderLayout.NORTH, saySomething);
  	
    // Each area (NORTH, SOUTH, EAST, WEST, CENTER) of a BorderLayout
    // can hold only one component. If we want more than one thing, we
    // add a Panel component and put multiple things in it.

    Panel westPanel = new Panel ();
    westPanel.setLayout (new GridLayout (3, 1));
    westPanel.setBackground (new Color (255, 200, 200));
    add (BorderLayout.WEST, westPanel);
    westPanel.add (clickMe);
    westPanel.add (suit);
   	
    Panel centerPanel = new Panel ();
      centerPanel.setBackground (new Color (255, 255, 200));
    add (BorderLayout.CENTER, centerPanel);
    centerPanel.add (language);
    centerPanel.add (oneLine);
    centerPanel.add (manyLines);
   	
    Panel eastPanel = new Panel ();
      eastPanel.setLayout (new GridLayout (8, 1));
      eastPanel.setBackground (new Color (200, 255, 200));
    add (BorderLayout.EAST, eastPanel);
    eastPanel.add (onOrOff);
    Button invisibleButton = new Button ("sneaky trick");
    invisibleButton.setVisible (false);
    eastPanel.add (invisibleButton);
    eastPanel.add (north);
    eastPanel.add (south);
    eastPanel.add (east);
    eastPanel.add (west);
    eastPanel.add (changeThings);
   	
    add (BorderLayout.SOUTH, scrolly);
    validate ();
  }
}